"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var lodash_1 = require("lodash");
var config = __importStar(require("./config"));
var strings;
var loader;
var momentLocalesMap = {
    de_DE: 'de',
    en_AU: 'en-au',
    en_US: null,
    en_GB: 'en-gb',
    en_ZZ: 'x-pseudo',
    es_419: 'es',
    es_ES: 'es',
    fr_CA: 'fr-ca',
    fr_FR: 'fr',
    it_IT: 'it',
    ja_JP: 'ja',
    pt_BR: 'pt-br',
    zh_CN: 'zh-cn',
    zh_HK: 'zh-hk',
    zh_TW: 'zh-tw'
};
function setStringsLoader(l) {
    loader = l;
}
exports.setStringsLoader = setStringsLoader;
function setStrings(strs) {
    strings = strs;
}
exports.setStrings = setStrings;
function loadStrings(loc) {
    //TODO Catch locale load error and load default
    return new Promise(function (resolve, reject) {
        if (!loader) {
            throw new Error('Strings loader have not been configured yet, check startup sequence');
        }
        //if (!config.getConfig().userLanguage) throw new Error('Locale has not been configured');
        if (undefined !== loc) {
            config.getConfig().userLanguage = loc;
        }
        resolve(loader(config.getConfig().userLanguage));
    }).then(function (strs) {
        setStrings(strs);
        return strs;
    });
}
exports.loadStrings = loadStrings;
/**
 * @return LANG_STRINGS
 */
function getStrings() {
    if (!strings) {
        throw new Error('Strings have not been loaded yet, check startup sequence');
    }
    return strings;
}
exports.getStrings = getStrings;
function trueClone(thing) {
    return lodash_1.cloneDeep(lodash_1.merge({}, thing));
}
function createStringsLoader(_a) {
    var loader = _a.loader, defaultStrings = _a.defaultStrings, moment = _a.moment;
    var defaultStringsClone = trueClone(defaultStrings); // JS Modules cannot be cloned directly because keys are getters
    return function (locale) {
        return loader(locale).then(function (newStrings) {
            // loader returns null if locale is default
            if (!newStrings) {
                newStrings = defaultStringsClone;
            }
            lodash_1.merge(defaultStrings, defaultStringsClone, newStrings);
            moment && moment.locale(momentLocalesMap[locale] || 'en');
            setStrings(defaultStrings);
            return defaultStrings;
        });
    };
}
exports.createStringsLoader = createStringsLoader;
//# sourceMappingURL=i18n.js.map