"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [0, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var index_1 = require("../i18n/index");
var index_2 = require("../util/index");
var Translate = /** @class */ (function () {
    function Translate() {
        var _this = this;
        this.i18n = index_1.i18n;
        this.defaultLanguage = 'en-US';
        this.supportedLanguages = [
            'en-US',
            'nl-NL'
        ];
        this.translations = {};
        this.translationsLocation = 'assets/translations';
        this.options = {
            debug: false,
            namespaceSplitter: '::' //[String|RegExp]: You can customize the part which splits namespace and translationKeys.
        };
        this.initTranslateJs();
        this.libTranslate = index_2.Util.getGlobal()['libTranslate']; // fixme: does not work in node.js
        Object.keys(index_1.i18n).forEach(function (k) {
            _this.supportedLanguages.push(k);
        });
        this.translations = index_2.Util.copy(index_1.i18n);
        this.translations['nl-NL'] = index_2.Util.copy(this.translations['nl']);
        this.translations['en-US'] = index_2.Util.copy(this.translations['en']);
        this.translations['en-BE'] = index_2.Util.copy(this.translations['en']);
        this.use(this.defaultLanguage);
    }
    /**
     * Translates a string to the current language of the TranslateService.
     * @param {string} translationKey - The string to translate.
     * @param {string} params - The parameters to send to the translate function.
     */
    Translate.prototype.get = function (translationKey) {
        var params = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            params[_i - 1] = arguments[_i];
        }
        return this.t.apply(this, [translationKey].concat(params));
    };
    /**
     * Adds a translated string to the current language object.
     * @param {string} translationKey - The identifier of the translation.
     * @param {string} value - The value of the translation in the current selected language.
     */
    Translate.prototype.set = function (translationKey, value) {
        this.translations[this.language][translationKey] = value;
    };
    Translate.prototype.add = function (translationsObject) {
        var _this = this;
        Object.keys(translationsObject).forEach(function (k) {
            _this.set(k, translationsObject[k]);
        });
    };
    /**
     * Switch the current language of the translateService.
     * If the supplied language parameter is not yet present in the translations object,
     * the service will try to load a json file in the location specified in translationsLocation property.
     * example: use('fr'), will load 'assets/translations/fr.json' by default
     * @param {string} language - The language identifier.
     * @param {boolean} reloadRemote - Set this to true to reload the json translation file.
     */
    Translate.prototype.use = function (language, reloadRemote) {
        if (reloadRemote === void 0) { reloadRemote = false; }
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.language = language;
                        if (!(!this.translations[language] || reloadRemote)) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.loadTranslation(language)];
                    case 1:
                        _a.sent();
                        _a.label = 2;
                    case 2:
                        this.t = this.libTranslate.getTranslationFunction(this.translations[language], [this.options]);
                        return [2 /*return*/];
                }
            });
        });
    };
    Translate.prototype.loadTranslation = function (language) {
        return __awaiter(this, void 0, void 0, function () {
            var loadedTranslation;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!this.translations[language])
                            this.translations[language] = {};
                        return [4 /*yield*/, index_2.Util.fetchJson(this.translationsLocation + '/' + language + '.json')];
                    case 1:
                        loadedTranslation = _a.sent();
                        Object.keys(loadedTranslation).forEach(function (k) {
                            _this.translations[language][k] = loadedTranslation[k];
                        });
                        return [2 /*return*/];
                }
            });
        });
    };
    Translate.prototype.updateTranslateComponents = function () {
        var components = Array.from(document.getElementsByTagName('ema-translate'));
        components.forEach(function (el) {
            el['update']();
        });
    };
    Translate.prototype.initTranslateJs = function () {
        /*! translate.js - v1.1.0 - 2015-06-16
         *  https://github.com/musterknabe/translate.js
         *  Copyright (c) 2015 Jonas Girnatis
         *  Licensed under MIT license
         */
        !function () {
            "use strict";
            var a = function (a) { return !isNaN(parseFloat(a)) && isFinite(a); }, b = function (a) { return "object" == typeof a && null !== a; }, c = function (a) { return "[object String]" === Object.prototype.toString.call(a); };
            index_2.Util.getGlobal()['libTranslate'] = { getTranslationFunction: function (d, e) { function f(a) { if (d[a])
                    return d[a]; var b = a.split(j), c = b[0], e = b[1]; return d[c] && d[c][e] ? d[c][e] : null; } function g(a, c) { if (b(a)) {
                    var d, e = Object.keys(a);
                    if (0 === e.length)
                        return i && console.log("[Translation] No plural forms found."), null;
                    for (var f = 0; f < e.length; f++)
                        0 === e[f].indexOf("gt") && (d = parseInt(e[f].replace("gt", ""), 10));
                    a[c] ? a = a[c] : c > d ? a = a["gt" + d] : a.n ? a = a.n : (i && console.log('[Translation] No plural forms found for count:"' + c + '" in', a), a = a[Object.keys(a).reverse()[0]]);
                } return a; } function h(a, b) { return c(a) ? a.replace(/\{(\w*)\}/g, function (a, c) { return b.hasOwnProperty(c) ? b.hasOwnProperty(c) ? b[c] : c : (i && console.log('Could not find replacement "' + c + '" in provided replacements object:', b), "{" + c + "}"); }) : a; } e = b(e) ? e : {}; var i = e.debug, j = e.namespaceSplitter || "::"; return function (c) { var d = b(arguments[1]) ? arguments[1] : b(arguments[2]) ? arguments[2] : {}, e = a(arguments[1]) ? arguments[1] : a(arguments[2]) ? arguments[2] : null, j = f(c); return null !== e && (d.n = d.n ? d.n : e, j = g(j, e)), j = h(j, d), null === j && (j = i ? "@@" + c + "@@" : c, i && console.log('Translation for "' + c + '" not found.')), j; }; } };
        }();
    };
    return Translate;
}());
exports.Translate = Translate;
//# sourceMappingURL=translate.js.map