import { useSearchParams } from 'react-router-dom'
import { useCallback, useMemo } from 'react'

type PageSize = number | 'all'

interface UsePaginationParamsOptions {
  defaultPageSize?: number
  allowedPageSizes?: PageSize[]
}

const DEFAULT_ALLOWED: PageSize[] = [10, 25, 50, 'all']

export function usePaginationParams(options: UsePaginationParamsOptions = {}) {
  const { defaultPageSize = 10, allowedPageSizes = DEFAULT_ALLOWED } = options

  const [searchParams, setSearchParams] = useSearchParams()

  const page = useMemo(() => {
    const raw = searchParams.get('page')
    const n = raw ? parseInt(raw, 10) : 1
    return Number.isFinite(n) && n >= 1 ? n : 1
  }, [searchParams])

  const pageSize = useMemo((): PageSize => {
    const raw = searchParams.get('size')
    if (raw === 'all' && allowedPageSizes.includes('all')) return 'all'
    const n = raw ? parseInt(raw, 10) : defaultPageSize
    if (Number.isFinite(n) && allowedPageSizes.includes(n)) return n
    return defaultPageSize
  }, [searchParams, defaultPageSize, allowedPageSizes])

  const setPage = useCallback(
    (newPage: number) => {
      setSearchParams((prev) => {
        const next = new URLSearchParams(prev)
        if (newPage <= 1) {
          next.delete('page')
        } else {
          next.set('page', String(newPage))
        }
        return next
      })
    },
    [setSearchParams]
  )

  const setPageSize = useCallback(
    (newSize: PageSize) => {
      setSearchParams((prev) => {
        const next = new URLSearchParams(prev)
        next.set('size', String(newSize))
        next.delete('page')
        return next
      })
    },
    [setSearchParams]
  )

  return { page, pageSize, setPage, setPageSize }
}
