"use client"

import { Locale } from "@/i18n/config"
import { useRouter } from "next/navigation"

export function useChangeLocale() {
  const router = useRouter()

  const changeLocale = (locale: Locale) => {
    document.cookie = `NEXT_LOCALE=${locale}; path=/; max-age=31536000`
    router.refresh()
  }

  return changeLocale
}

export function getLocaleFromCookie(): Locale {
  if (typeof document === "undefined") return "uz"

  const cookie = document.cookie.split("; ").find((row) => row.startsWith("NEXT_LOCALE="))

  return (cookie?.split("=")[1] as Locale) || "uz"
}
