"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get DEFAULT_LANGUAGE_CODE () {
        return DEFAULT_LANGUAGE_CODE;
    },
    get dictionaryKeys () {
        return dictionaryKeys;
    },
    get getDefaultLanguageDictionary () {
        return getDefaultLanguageDictionary;
    },
    get getLanguageDictionary () {
        return getLanguageDictionary;
    },
    get getLanguagesDictionaries () {
        return getLanguagesDictionaries;
    },
    get getTranslatedPhrase () {
        return getTranslatedPhrase;
    },
    get getValidLanguageCode () {
        return getValidLanguageCode;
    },
    get hasLanguageDictionary () {
        return hasLanguageDictionary;
    },
    get registerLanguageDictionary () {
        return registerLanguageDictionary;
    }
});
const _object = require("../helpers/object");
const _mixed = require("../helpers/mixed");
const _utils = require("./utils");
const _staticRegister = require("../utils/staticRegister");
const _phraseFormatters = require("./phraseFormatters");
const _enUS = /*#__PURE__*/ _interop_require_default(require("./languages/en-US"));
const _constants = /*#__PURE__*/ _interop_require_wildcard(require("./constants"));
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const dictionaryKeys = _constants;
const DEFAULT_LANGUAGE_CODE = _enUS.default.languageCode;
const { register: registerGloballyLanguageDictionary, getItem: getGlobalLanguageDictionary, hasItem: hasGlobalLanguageDictionary, getValues: getGlobalLanguagesDictionaries } = (0, _staticRegister.staticRegister)('languagesDictionaries');
/**
 * Register automatically the default language dictionary.
 */ registerLanguageDictionary(_enUS.default);
function registerLanguageDictionary(languageCodeOrDictionary, dictionary) {
    let languageCode = languageCodeOrDictionary;
    let dictionaryObject = dictionary;
    // Dictionary passed as first argument.
    if ((0, _object.isObject)(languageCodeOrDictionary)) {
        dictionaryObject = languageCodeOrDictionary;
        languageCode = dictionaryObject.languageCode;
    }
    extendLanguageDictionary(languageCode, dictionaryObject);
    registerGloballyLanguageDictionary(languageCode, (0, _object.deepClone)(dictionaryObject));
    // We do not allow user to work with dictionary by reference, it can cause lot of bugs.
    return (0, _object.deepClone)(dictionaryObject);
}
/**
 * Extend handled dictionary by default language dictionary. As result, if any dictionary key isn't defined for specific language, it will be filled with default language value ("dictionary gaps" are supplemented).
 *
 * @private
 * @param {string} languageCode Language code.
 * @param {object} dictionary Dictionary which is extended.
 */ function extendLanguageDictionary(languageCode, dictionary) {
    if (languageCode !== DEFAULT_LANGUAGE_CODE) {
        (0, _utils.extendNotExistingKeys)(dictionary, getGlobalLanguageDictionary(DEFAULT_LANGUAGE_CODE));
    }
}
function getLanguageDictionary(languageCode) {
    if (!hasLanguageDictionary(languageCode)) {
        return null;
    }
    return (0, _object.deepClone)(getGlobalLanguageDictionary(languageCode));
}
function hasLanguageDictionary(languageCode) {
    return hasGlobalLanguageDictionary(languageCode);
}
function getDefaultLanguageDictionary() {
    return _enUS.default;
}
function getLanguagesDictionaries() {
    return getGlobalLanguagesDictionaries();
}
function getTranslatedPhrase(languageCode, dictionaryKey, argumentsForFormatters) {
    const languageDictionary = getLanguageDictionary(languageCode);
    if (languageDictionary === null) {
        return null;
    }
    const phrasePropositions = languageDictionary[dictionaryKey];
    if ((0, _mixed.isUndefined)(phrasePropositions)) {
        return null;
    }
    const formattedPhrase = getFormattedPhrase(phrasePropositions, argumentsForFormatters);
    if (Array.isArray(formattedPhrase)) {
        return formattedPhrase[0];
    }
    return formattedPhrase;
}
/**
 * Get formatted phrase from phrases propositions for specified dictionary key.
 *
 * @private
 * @param {Array|string} phrasePropositions List of phrase propositions.
 * @param {*} argumentsForFormatters Arguments which will be handled by formatters.
 *
 * @returns {Array|string}
 */ function getFormattedPhrase(phrasePropositions, argumentsForFormatters) {
    return (0, _phraseFormatters.getPhraseFormatters)().reduce((phrase, formatter)=>{
        return formatter(phrase, argumentsForFormatters);
    }, phrasePropositions);
}
function getValidLanguageCode(languageCode) {
    let normalizedLanguageCode = (0, _utils.normalizeLanguageCode)(languageCode);
    if (!hasLanguageDictionary(normalizedLanguageCode)) {
        normalizedLanguageCode = DEFAULT_LANGUAGE_CODE;
        (0, _utils.warnUserAboutLanguageRegistration)(languageCode);
    }
    return normalizedLanguageCode;
}
