/*!
 * KoliBri - The accessible HTML-Standard
 */
import locale_de from "../locales/de";
import locale_en from "../locales/en";
const mapLocaleKeys = (locale) => Object.keys(locale).reduce((a, c) => ((a[`${'kol'}-${c}`] = locale[c]), a), {});
const TRANSLATIONS = new Set([
    (t) => t('en', mapLocaleKeys(locale_en)),
    (t) => t('de', mapLocaleKeys(locale_de)),
]);
const replaceAll = function (text, search, replacement) {
    return text.split(search).join(replacement);
};
export class I18nService {
    constructor(initialLanguage) {
        this.resourceMap = new Map();
        this.language = initialLanguage !== null && initialLanguage !== void 0 ? initialLanguage : 'de';
    }
    addTranslations(translations) {
        if (Array.isArray(translations)) {
            translations = new Set(translations);
        }
        else if (typeof translations === 'function') {
            translations = new Set([translations]);
        }
        if (translations !== undefined) {
            translations.forEach((t) => t((l, t) => {
                let resources = this.resourceMap.get(l);
                if (!resources) {
                    resources = new Map();
                    this.resourceMap.set(l, resources);
                }
                Object.entries(t).forEach(([k, v]) => {
                    if (v) {
                        resources.set(k, v);
                    }
                });
                return l;
            }));
        }
    }
    setLanguage(lng) {
        this.language = lng;
    }
    translate(key, options) {
        var _a;
        let result = (_a = this.resourceMap.get(this.language)) === null || _a === void 0 ? void 0 : _a.get(key);
        if (result) {
            if (options === null || options === void 0 ? void 0 : options.placeholders) {
                Object.entries(options.placeholders).forEach(([k, v]) => {
                    result = replaceAll(result, `{{${k}}}`, v);
                });
            }
        }
        else {
            result = key;
        }
        return result;
    }
}
let i18n;
export const getI18nInstance = () => {
    return i18n;
};
export const initializeI18n = (lng, translations = TRANSLATIONS) => {
    i18n = new I18nService(lng);
    i18n.addTranslations(translations);
    return i18n;
};
//# sourceMappingURL=i18n.js.map
