import { default as i18nNext } from 'i18next';
import LanguageDetector from "i18next-browser-languagedetector";
import translations from "./translations";
import { ELanguages } from './elanguages';
import { ApplicationContext, LangUtils } from '@sankhyalabs/core';
const KEY_I18N = "__EZ_UI_I18N_INSTANCE__";
export function getI18n() {
  let i18nInstance = ApplicationContext.getContextValue(KEY_I18N);
  return i18nInstance;
}
const initI18n = async (forceLang) => {
  let i18nTFunction = getI18n();
  if (!i18nTFunction) {
    const i18nInstance = i18nNext.createInstance();
    const i18nextOptions = {
      resources: translations,
      fallbackLng: ELanguages.PT_BR
    };
    await i18nInstance
      .use(LanguageDetector)
      .init(i18nextOptions);
    const documentLanguage = LangUtils.getLanguage();
    const lang = forceLang !== null && forceLang !== void 0 ? forceLang : documentLanguage;
    await i18nInstance.changeLanguage(lang);
    ApplicationContext.setContextValue(KEY_I18N, i18nInstance.t);
    i18nTFunction = i18nInstance.t;
  }
  return i18nTFunction;
};
export default initI18n;
