"use strict";

exports.__esModule = true;
exports.prefetchTranslationsWithoutBundling = exports.messagesEnImportGlobal = exports.fetchTranslations = void 0;
var _translationsImportsMap = require("./translationsImportsMap");
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); }
const resolveMessages = messages => typeof messages.default === 'object' ? messages.default : messages;
const importVisualE2eMessages = () => Promise.resolve().then(() => _interopRequireWildcard(require(/* webpackChunkName: "cairo-locale-messages_visuale2e-json" */
'../assets/visual-e2e-locale/messages_visuale2e.json')));
const importLocale = async locale => {
  if (typeof process !== 'undefined' && process.env.STORYBOOK_VISUAL_E2E === 'true' && locale === 'en') {
    return resolveMessages(await importVisualE2eMessages());
  }
  const factory = _translationsImportsMap.translationsImportsMap[locale];
  if (factory == null) {
    throw new Error(`@wix/cairo: Failed to find translations for locale '${locale}'`);
  }
  return resolveMessages(await factory());
};
const messagesEnImportGlobal = () => importLocale('en');
exports.messagesEnImportGlobal = messagesEnImportGlobal;
messagesEnImportGlobal(); // prefetch en translations

const fetchTranslations = ({
  errorMonitor
}) => async (locale, {
  messagesEn
} = {}) => {
  try {
    if (messagesEn != null && locale === 'en') {
      return messagesEn;
    }
    return await importLocale(locale);
  } catch (e) {
    const message = e == null ? void 0 : e.message;
    // ignore missing translation files
    if (message === 'string' && /Cannot find module/.test(message)) {
      throw e;
    }
    errorMonitor.captureException(e);
    if (process.env.NODE_ENV === 'development') {
      console.error(`@wix/cairo: Failed to fetch translation for locale '${locale}', caused by:`);
      console.error(e);
    }
    throw e; // i18n will catch & fallback to default locale
  }
};
exports.fetchTranslations = fetchTranslations;
const prefetchTranslationsWithoutBundling = params => locale => {
  return fetchTranslations(params)(locale);
};
exports.prefetchTranslationsWithoutBundling = prefetchTranslationsWithoutBundling;
//# sourceMappingURL=fetchTranslations.js.map