"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useTranslation = useTranslation;
var _reactI18next = require("react-i18next");
var _react = require("react");
var _usePrevious = require("../use-previous");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function defaultT(key, params) {
  if (!params) {
    return key;
  }
  const count = typeof params.count === 'number' ? params.count : 0;
  const isDefaultNeeded = count !== 1 && params.plural;
  const translateString = isDefaultNeeded ? String(params.plural) : key;
  return translateString.replace(/\{\{(\w+)\}\}/g, (_match, match) => String(params[match] ?? `{{${match}}}`));
}

// todo replace with design system version
const translationKey = `design-system-${Date.now()}`;
const setDefaultValues = data => {
  return Object.keys(data).reduce((acc, current) => {
    acc[current] = data[current] || current;
    return acc;
  }, {});
};
const LANGUAGE_CODES = {
  ENGLISH: 'en',
  SPANISH_LA: 'es-419',
  FRENCH: 'fr',
  JAPANESE: 'ja',
  KOREAN: 'ko',
  PORTUGUESE_BRAZIL: 'pt-BR'
};
const loadLocales = {
  [LANGUAGE_CODES.SPANISH_LA]: () => (specifier => new Promise(r => r(specifier)).then(s => _interopRequireWildcard(require(s))))(`../../../locales/es-419/translation.json`),
  [LANGUAGE_CODES.FRENCH]: () => (specifier => new Promise(r => r(specifier)).then(s => _interopRequireWildcard(require(s))))(`../../../locales/fr/translation.json`),
  [LANGUAGE_CODES.JAPANESE]: () => (specifier => new Promise(r => r(specifier)).then(s => _interopRequireWildcard(require(s))))(`../../../locales/ja/translation.json`),
  [LANGUAGE_CODES.KOREAN]: () => (specifier => new Promise(r => r(specifier)).then(s => _interopRequireWildcard(require(s))))(`../../../locales/ko/translation.json`),
  [LANGUAGE_CODES.PORTUGUESE_BRAZIL]: () => (specifier => new Promise(r => r(specifier)).then(s => _interopRequireWildcard(require(s))))(`../../../locales/pt-BR/translation.json`)
};
let prevLanguage = null;
function useTranslation() {
  const result = (0, _reactI18next.useTranslation)(translationKey);
  const {
    language,
    hasResourceBundle,
    isInitialized
  } = result.i18n;
  const isLoaded = hasResourceBundle?.(language, translationKey);
  const [isFetched, setIsFetched] = (0, _react.useState)(isLoaded);
  const isFirstLocaleLoaded = (0, _react.useRef)(isLoaded);
  const isEnglish = language === LANGUAGE_CODES.ENGLISH;
  const prevLanguageRef = (0, _usePrevious.usePrevious)(language);
  const t = (key, options = {}) => {
    const baseOptions = {
      fallbackLng: isEnglish ? LANGUAGE_CODES.ENGLISH : prevLanguage || language,
      defaultValue_other: options?.plural,
      ...options
    };
    return result.t(key, baseOptions);
  };
  (0, _react.useLayoutEffect)(() => {
    prevLanguage = prevLanguageRef;
    if (!isLoaded && loadLocales[language]) {
      setIsFetched(false);
      loadLocales[language]().then(data => {
        const translations = setDefaultValues(data.default);
        result.i18n.addResourceBundle(language, translationKey, translations);
        setIsFetched(true);
        isFirstLocaleLoaded.current = true;
      });
    }
  }, [language]);
  return {
    ...result,
    isLocaleFetched: isFetched || isEnglish,
    isLoaded: isFirstLocaleLoaded.current || isEnglish,
    t: isInitialized ? t : defaultT
  };
}