import { merge, cloneDeep } from 'lodash';
import * as config from './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;
}
function setStrings(strs) {
    strings = strs;
}
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;
    });
}
/**
 * @return LANG_STRINGS
 */
function getStrings() {
    if (!strings) {
        throw new Error('Strings have not been loaded yet, check startup sequence');
    }
    return strings;
}
function trueClone(thing) {
    return cloneDeep(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;
            }
            merge(defaultStrings, defaultStringsClone, newStrings);
            moment && moment.locale(momentLocalesMap[locale] || 'en');
            setStrings(defaultStrings);
            return defaultStrings;
        });
    };
}
export { setStringsLoader, setStrings, loadStrings, getStrings, createStringsLoader };
//# sourceMappingURL=i18n.js.map