import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import { useLangStore } from "./stores/langStore";
import en from "../locales/en/common.json";
import am from "../locales/am/common.json";
import fr from "../locales/fr/common.json";
import ti from "../locales/ti/common.json";
import om from "../locales/om/common.json";

const resources = {
  en: { translation: en },
  am: { translation: am },
  fr: { translation: fr },
  ti: { translation: ti },
  om: { translation: om },
};

const initialLanguage = useLangStore.getState().language;

i18n.use(initReactI18next).init({
  resources,
  lng: initialLanguage,
  fallbackLng: "en",
  interpolation: {
    escapeValue: false,
  },
});

useLangStore.subscribe((state) => {
  i18n.changeLanguage(state.language);
});

export default i18n;
