import i18next from "i18next";
import { initReactI18next } from "react-i18next";
import en from "../i18n/locals/en.json";
import hi from "../i18n/locals/hi.json";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { STORAGE_KEYS } from "../../constants/config";

export const languageResources = {
  en: { translation: en },
  hi: { translation: hi },
};

i18next.use(initReactI18next).init({
  compatibilityJSON: "v4",
  lng: "hi",
  fallbackLng: "en",
  resources: languageResources,
  debug: true,
});

i18next.on("initialized", () => {
  console.log("✅ i18next initialized with language:", i18next.language);
  console.log("Translation test (welcome):", i18next.t("welcome"));
});

export const setLanguage = async (language: string) => {
  await AsyncStorage.setItem(STORAGE_KEYS.LANGUAGE, language);
  i18next.changeLanguage(language);
};

export const getLanguage = async () => {
  const lang = await AsyncStorage.getItem(STORAGE_KEYS.LANGUAGE);

  return lang;
};

export default i18next;
