var I18n = {

    definitions: {},

    lang: '',

    chosedLang: '',
    defaultLang: 'pt',

    addDictionary(lang, dictionary){
        
        if(!I18n.definitions[lang]) I18n.definitions[lang] = {};

        for(key in dictionary){

            I18n.definitions[lang][key] = dictionary[key];

        }

    },

    getDefinitions() {

        let langs = {};

        if (typeof languages == 'undefined') languages = ['pt', 'es', 'en'];

        languages.forEach(language => {

            for (code in language) {

                langs[code] = language[code];

            }

        });

        return langs[I18n.lang];

    },

    getLang(){

        if(!I18n.chosedLang) I18n.lang = navigator.language.split('-')[0];
        else I18n.lang = I18n.chosedLang;

        return I18n.lang;

    },

    // load(lang){

    //     if(!window.__) window.__ = I18n.get;

    //     if(!I18n.chosedLang) I18n.lang = navigator.language.split('-')[0];
    //     else I18n.lang = I18n.chosedLang;

    //     let definitions = I18n.getDefinitions();

    //     if(!definitions) definitions = {};

    //     I18n.definitions = definitions;

    // },

    get(text){

        let definitions = I18n.definitions;

        let lang = I18n.getLang();

        if(!I18n.definitions[lang]) return text;

        let definition = I18n.definitions[lang][text];

        if(definition) return definition;
        else{

            if(I18n.lang !== I18n.defaultLang) console.log('@info i18n-' + I18n.lang + ' undefined definition: ' + text);

            return text;

        }

    },

    parse(content){

        let elms = content.find('[i18n]');

        if(content.attr('i18n')){

            elms = elms.add(content);

        }

        elms.each(k => {

            let attribute = false;

            let elm = elms.eq(k);

            let val = elm.attr('i18n');

            if(val == 'i18n') val = false;

            if(elm.attr('i18n-attr')){
                val = elm.attr(elm.attr('i18n-attr'));
                attribute = true;
            }

            // Se não tiver o atributo, vamos usar o texto do elemento
            if(!val) val = elm.text();

            let translated = I18n.get(val);

            if(attribute) elm.attr(elm.attr('i18n-attr'), translated);
            else elm.text(translated);

            elm.removeAttr('i18n');

        });

        return content;

    }

}

if(!Config) var Config = {};

if(!window.__) window.__ = I18n.get;