"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tr = exports.fmt = exports.registerStringBundle = exports.DEFAULT_LOCALE = void 0;
var en_1 = require("../strings/en");
var logger_1 = require("../utils/logger");
exports.DEFAULT_LOCALE = "en";
var STRINGS = {
    "en": en_1.STRINGS_EN
};
/**
 * Registers a string bundle for the given locale
 *
 * @export
 * @param {string} locale
 * @param {ILocalizedMessages} bundle
 */
function registerStringBundle(locale, bundle) {
    STRINGS[locale] = bundle;
}
exports.registerStringBundle = registerStringBundle;
/**
 * Formats the specified string and substitutes any placeholders (enclosed in {})
 * with the specified arguments
 *
 * @export
 * @param {string} format
 * @param {*} [args]
 * @returns {string}
 */
function fmt(format, args) {
    var str = format;
    if (args != null) {
        for (var p in args) {
            //str = str.replace(new RegExp(`\{${p}\}`, "g"), `${args[p]}`);
            str = str.split("{".concat(p, "}")).join(args[p]);
        }
        return str;
    }
    ;
    return str;
}
exports.fmt = fmt;
/**
 * Returns the localized string for the given key
 *
 * @export
 * @param {keyof ILocalizedMessages} key Any property name of {@link ILocalizedMessages}
 * @param {string} [locale="en"]
 * @param {*} [args] If a localized string has placeholders, they will be replaced with the values defined here
 * @returns {string}
 */
function tr(key, locale, args) {
    if (locale === void 0) { locale = exports.DEFAULT_LOCALE; }
    var bundle = STRINGS[locale];
    if (!bundle) {
        (0, logger_1.warn)("No such string bundle for locale: ".concat(locale));
        return key;
    }
    else {
        var str = bundle[key];
        if (!str) {
            (0, logger_1.warn)("String bundle for locale (".concat(locale, ") is missing localized string for key: ").concat(key));
            return key;
        }
        else {
            if (args != null) {
                return fmt(str, args);
            }
            return str;
        }
    }
}
exports.tr = tr;
//# sourceMappingURL=i18n.js.map