import * as RNLocalize from "react-native-localize";
import i18n from "i18n-js";
import memoize from "lodash.memoize";
import { I18nManager } from "react-native";
import Database from "../../../../../Components/common/DAsyncStorage";
const translationGetters = {
  en: () => require("./en.json"),
  hn: () => require("./hn.json"),
};

// export const translate = memoize(
//   (key, config) => {
//     const translatedValue = i18n.t(key, config);
//     if (translatedValue.includes("[missing")) return translatedValue;
//     else return translatedValue;
//   },
//   (key, config) => (config ? key + JSON.stringify(config) : key)
// );
export const translate = memoize(
  (key, config) => i18n.t(key, config),
  (key, config) => (config ? key + JSON.stringify(config) : key)
);
export function setI18nConfig(myLanguage) {
  // const setLang = async () => {
  const fallback = { languageTag: "en", isRTL: false };
  const { languageTag, isRTL } =
    RNLocalize.findBestAvailableLanguage(Object.keys(translationGetters)) ||
    fallback;
  let myLanguageTag = "en";

  var data = Database.get("SelectedLang");
  if (data?.PreferredLanguage) {
    console.log(
      ">>>>>>>>>>>>> DATA FROM REALM >>>>>>>>>",
      JSON.stringify(data?.PreferredLanguage)
    );
    myLanguageTag = data?.PreferredLanguage;
  }
  translate.cache.clear();
  i18n.translations = {
    [myLanguageTag]: translationGetters[myLanguageTag](),
  };
  I18nManager.forceRTL(isRTL);
  i18n.locale = myLanguageTag;
  // };
  // setLang();
}
