var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import i18next from 'i18next';
import ChainedBackend from 'i18next-chained-backend';
import HttpBackend from 'i18next-http-backend';
import LocalStorageBackend from 'i18next-localstorage-backend';
import { getRoute } from '../services/requests';
import state from '../state';
export const i18nInstance = i18next.createInstance();
export const init = ({ language }) => __awaiter(void 0, void 0, void 0, function* () {
    const customerLng = state.customer.language;
    const customerNS = state.customer.slug;
    const lng = language || customerLng || 'en';
    const route = getRoute();
    // const nsFileExists = await fileExists(
    //   `${route}/locales/${lng}/${customerNS}.json`
    // )
    const nsFileExists = false;
    const defaultNS = nsFileExists ? customerNS : 'translation';
    const ns = ['translation', ...(nsFileExists ? [customerNS] : [])];
    return new Promise((resolve, reject) => {
        i18nInstance.use(ChainedBackend).init({
            debug: process.env.NODE_ENV === 'development',
            react: {
                useSuspense: false,
            },
            lng,
            fallbackLng: false,
            supportedLngs: ['pt', 'en', 'es'],
            ns,
            defaultNS,
            fallbackNS: 'translation',
            load: 'currentOnly',
            preload: [lng],
            interpolation: {
                escapeValue: false,
            },
            backend: {
                backends: [LocalStorageBackend, HttpBackend],
                backendOptions: [
                    {
                        prefix: 'i18next_res_r2u_sdk_',
                        enabled: process.env.NODE_ENV !== 'development',
                        expirationTime: 7 * 24 * 60 * 60 * 1000,
                    },
                    {
                        loadPath: `${route}/locales/{{lng}}/{{ns}}.json`,
                        allowMultiloading: true,
                    },
                ],
            },
        }, (err) => {
            if (err)
                reject(new Error('R2U - Translation init failed'));
            resolve();
        });
    });
});
export default {
    init,
};
//# sourceMappingURL=i18n.js.map