import { translationsImportsMap } from './translationsImportsMap';
const resolveMessages = (messages) => typeof messages.default === 'object' ? messages.default : messages;
const importVisualE2eMessages = () => import(
/* 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[locale];
    if (factory == null) {
        throw new Error(`@wix/cairo: Failed to find translations for locale '${locale}'`);
    }
    return resolveMessages(await factory());
};
export const messagesEnImportGlobal = () => importLocale('en');
messagesEnImportGlobal(); // prefetch en translations
export const fetchTranslations = ({ errorMonitor }) => async (locale, { messagesEn } = {}) => {
    try {
        if (messagesEn != null && locale === 'en') {
            return messagesEn;
        }
        return await importLocale(locale);
    }
    catch (e) {
        const message = 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
    }
};
export const prefetchTranslationsWithoutBundling = (params) => (locale) => {
    return fetchTranslations(params)(locale);
};
//# sourceMappingURL=fetchTranslations.js.map