'use client'

import {
  useLocation,
  useNavigate,
  useParams as useRouteParams,
  useRouter as useTanStackRouter,
} from '@tanstack/react-router'

type RouteParams = Record<string, string | string[] | undefined>

export const useRouter = () => {
  const navigate = useNavigate()
  const router = useTanStackRouter()

  return {
    push: (to: string) => navigate({ to }),
    replace: (to: string) => navigate({ to, replace: true }),
    refresh: () => router.invalidate(),
  }
}

export const usePathname = () => useLocation({ select: (location) => location.pathname })

export const useSearchParams = () =>
  useLocation({
    select: (location) => new URLSearchParams(location.searchStr),
  })

export const useParams = <T extends RouteParams = RouteParams>(): T => {
  const params = useRouteParams({ strict: false })
  return params as T
}
