"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const detect_node_1 = __importDefault(require("detect-node"));
const i18next_1 = __importDefault(require("i18next"));
const i18next_xhr_backend_1 = __importDefault(require("i18next-xhr-backend"));
const i18next_browser_languagedetector_1 = __importDefault(require("i18next-browser-languagedetector"));
exports.default = (config) => {
    var _a, _b, _c;
    let initPromise = null;
    if (!i18next_1.default.isInitialized) {
        if (detect_node_1.default) {
            const i18nextNodeBackend = eval("require('i18next-node-fs-backend')");
            const i18nextMiddleware = eval("require('i18next-express-middleware')");
            i18next_1.default.use(i18nextNodeBackend);
            if (config.serverLanguageDetection) {
                const serverDetectors = new i18nextMiddleware.LanguageDetector();
                (_a = config.customDetectors) === null || _a === void 0 ? void 0 : _a.forEach(detector => serverDetectors.addDetector(detector));
                i18next_1.default.use(serverDetectors);
            }
        }
        else {
            i18next_1.default.use(i18next_xhr_backend_1.default);
            if (config.browserLanguageDetection) {
                const browserDetectors = new i18next_browser_languagedetector_1.default();
                (_b = config.customDetectors) === null || _b === void 0 ? void 0 : _b.forEach(detector => browserDetectors.addDetector(detector));
                i18next_1.default.use(browserDetectors);
            }
        }
        (_c = config.use) === null || _c === void 0 ? void 0 : _c.forEach(x => i18next_1.default.use(x));
        initPromise = i18next_1.default.init(config);
    }
    return { i18n: i18next_1.default, initPromise };
};
