"use client"

import { useRouter, useSearchParams } from "next/navigation"
import { useCallback } from "react"

/**
 * 分页 URL 状态管理 hook。
 * 统一审计模块三个列表页的分页逻辑，消除三处重复的 handlePageChange。
 */
export function useLogPagination(): (page: number) => void {
  const router = useRouter()
  const searchParams = useSearchParams()

  return useCallback(
    (newPage: number) => {
      const params = new URLSearchParams(searchParams.toString())
      if (newPage <= 1) {
        params.delete("page")
      } else {
        params.set("page", String(newPage))
      }
      const query = params.toString()
      router.push(query ? `?${query}` : "?")
    },
    [router, searchParams],
  )
}
