const DEFAULT_LANGUAGE = 'en';
const TRANSLATIONS = {
    en: {
        new: "NEW"
    }
};
export const getTranslations = (url) => {
    return new Promise((resolve) => {
        fetch(url)
            .then((res) => res.json())
            .then((data) => {
            Object.keys(data).forEach((lang) => {
                if (!TRANSLATIONS[lang]) {
                    TRANSLATIONS[lang] = {};
                }
                for (let key in data[lang]) {
                    TRANSLATIONS[lang][key] = data[lang][key];
                }
            });
            resolve(true);
        });
    });
};
export const translate = (key, customLang, values) => {
    const lang = customLang;
    let translation = TRANSLATIONS[(lang !== undefined) && (lang in TRANSLATIONS) ? lang : DEFAULT_LANGUAGE][key];
    if (values !== undefined) {
        for (const [key, value] of Object.entries(values.values)) {
            const regex = new RegExp(`{${key}}`, 'g');
            translation = translation.replace(regex, value);
        }
    }
    return translation;
};
