"use client";

import { useRouter, useSearchParams } from "next/navigation";
import { useLanguage } from "../context/LanguageContext";
import { getLocalizedUrl } from "../utils/urlHelper";

/**
 * Custom hook that wraps Next.js useRouter to automatically
 * include the current language and franchiseeId parameters in all navigation calls.
 */
export function useLocalizedNavigation() {
  const router = useRouter();
  const searchParams = useSearchParams();
  const { currentLanguage } = useLanguage();
  const currentFranchiseeId = searchParams.get("franchiseeId");

  const push = (href: string, options?: any, langOverride?: string) => {
    const localizedHref = getLocalizedUrl(href, langOverride || currentLanguage, currentFranchiseeId);
    router.push(localizedHref, options);
  };

  const replace = (href: string, options?: any, langOverride?: string) => {
    const localizedHref = getLocalizedUrl(href, langOverride || currentLanguage, currentFranchiseeId);
    router.replace(localizedHref, options);
  };

  const prefetch = (href: string, options?: any, langOverride?: string) => {
    const localizedHref = getLocalizedUrl(href, langOverride || currentLanguage, currentFranchiseeId);
    router.prefetch(localizedHref, options);
  };

  return {
    ...router,
    push,
    replace,
    prefetch,
  };
}
