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());
    });
};
import { i18n } from '../i18n/index';
import { Util } from '../util/index';
export class Translate {
    constructor() {
        this.i18n = 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 = Util.getGlobal()['libTranslate']; // fixme: does not work in node.js
        Object.keys(i18n).forEach((k) => {
            this.supportedLanguages.push(k);
        });
        this.translations = Util.copy(i18n);
        this.translations['nl-NL'] = Util.copy(this.translations['nl']);
        this.translations['en-US'] = Util.copy(this.translations['en']);
        this.translations['en-BE'] = 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.
     */
    get(translationKey, ...params) {
        return this.t(translationKey, ...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.
     */
    set(translationKey, value) {
        this.translations[this.language][translationKey] = value;
    }
    add(translationsObject) {
        Object.keys(translationsObject).forEach((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.
     */
    use(language, reloadRemote = false) {
        return __awaiter(this, void 0, void 0, function* () {
            this.language = language;
            if (!this.translations[language] || reloadRemote) {
                yield this.loadTranslation(language);
            }
            this.t = this.libTranslate.getTranslationFunction(this.translations[language], [this.options]);
            return;
        });
    }
    loadTranslation(language) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.translations[language])
                this.translations[language] = {};
            let loadedTranslation = yield Util.fetchJson(this.translationsLocation + '/' + language + '.json');
            Object.keys(loadedTranslation).forEach((k) => {
                this.translations[language][k] = loadedTranslation[k];
            });
            return;
        });
    }
    updateTranslateComponents() {
        let components = Array.from(document.getElementsByTagName('ema-translate'));
        components.forEach((el) => {
            el['update']();
        });
    }
    initTranslateJs() {
        /*! 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); };
            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; }; } };
        }();
    }
}
//# sourceMappingURL=translate.js.map