"use client";
import { useState, useCallback } from "react";
import { useUserProfile } from "./useUserProfile";
import { setUserLocaleClient } from "../lib/locale-client";
import { I18N_CONFIG } from "../lib/config";
function useLocale() {
  const { profile, updateProfile } = useUserProfile();
  const [isChanging, setIsChanging] = useState(false);
  const currentLocale = (profile == null ? void 0 : profile.language) || I18N_CONFIG.defaultLocale;
  const changeLanguage = useCallback(async (newLocale) => {
    if (newLocale === currentLocale) return;
    setIsChanging(true);
    try {
      await updateProfile({ language: newLocale });
      setUserLocaleClient(newLocale);
      window.location.reload();
    } catch (error) {
      console.error("Error changing language:", error);
      if (typeof window !== "undefined") {
        const { toast } = await import("sonner");
        toast.error("Failed to change language. Please try again.");
      }
      throw error;
    } finally {
      setIsChanging(false);
    }
  }, [currentLocale, updateProfile]);
  return {
    locale: currentLocale,
    changeLanguage,
    isChanging,
    supportedLocales: I18N_CONFIG.supportedLocales
  };
}
export {
  useLocale
};
