"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
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 format_message_1 = __importDefault(require("format-message"));
var config = __importStar(require("./config"));
var constants = __importStar(require("./constants"));
var defaultBrandId = '0';
var icuTemplates = {
    en_US: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
    en_ZZ: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
    en_GB: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
    en_AU: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
    fr_CA: "=0 '{'{one}'}' =1'{'{one}'}' other'{'{other}'}'",
    fr_FR: "=0 '{'{one}'}' =1'{'{one}'}' other'{'{other}'}'",
    de_DE: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
    es_ES: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
    it_IT: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
    es_419: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
    ja_JP: "=0 '{'{one}'}' =1'{'{one}'}' other'{'{one}'}'",
    pt_BR: "=0 '{'{one}'}' =1'{'{one}'}' other'{'{other}'}'",
    zh_CN: "=0 '{'{other}'}' =1'{'{other}'}' other'{'{other}'}'",
    zh_TW: "=0 '{'{other}'}' =1'{'{other}'}' other'{'{other}'}'",
    zh_HK: "=0 '{'{other}'}' =1'{'{other}'}' other'{'{other}'}'"
};
function isValidKeysByReg(reg, obj) {
    var keys = Object.keys(obj);
    var hasInvalidKey = Object.keys(obj).some(function (key) {
        return !reg.test(key);
    });
    return keys.length > 0 && !hasInvalidKey;
}
function isBrandedMessageResource(obj) {
    return isValidKeysByReg(/^0$|^\d{4}$|^\d{2}$/, obj);
}
function isPluralMessageResource(obj) {
    return isValidKeysByReg(/^one$|^other$|^many$/, obj);
}
function covertMessageResourceToICUString(obj) {
    var lang = config.isConfigInitialized() ? config.getConfig().userLanguage : null;
    var icuMsgFormat = (lang && icuTemplates[lang]) || icuTemplates['en_US'];
    var msg = format_message_1.default(icuMsgFormat, obj);
    return '{count, plural, ' + msg + '}';
}
/**
 * TODO rename. actually is formatMessage not a translate
 * @param {string|object} str
 * @param {object} [args]
 * @return {string}
 */
function translate(str, args) {
    if (!str) {
        return constants.notTranslatedPrefix;
    }
    if (typeof str === 'object') {
        if (isBrandedMessageResource(str)) {
            var brandId = config.isConfigInitialized()
                ? config.getConfig().brandId
                : defaultBrandId;
            str = str[brandId] || str[defaultBrandId];
        }
        else if (isPluralMessageResource(str)) {
            //TODO Support both brand and plural at the same time
            var errorMessage = 'You are using translate() to handle plural strings, args should be an object like {count:1} or a number.';
            if (!args && args !== 0) {
                throw new Error(errorMessage);
            }
            if (!isNaN(args)) {
                args = { count: args };
            }
            if (!args.hasOwnProperty('count')) {
                throw new Error(errorMessage);
            }
            str = covertMessageResourceToICUString(str);
        }
        else {
            throw new Error('Object ' + JSON.stringify(str) + ' is not a valid message resource.');
        }
    }
    if (typeof str === 'string' && str.indexOf(constants.notTranslatedPrefix + ':') === 0) {
        str = str.replace(constants.notTranslatedPrefix + ':', '');
    }
    return format_message_1.default(str, args);
}
exports.translate = translate;
exports.default = translate;
//# sourceMappingURL=translate.js.map