import {postJson} from "./Request.js";

const langSelected = (options, htmlLang) => {

    options.forEach(option => {
        if (option.dataset.lang === htmlLang) {
            option.selected = true
        }
    })

}
const langRequest = (selects, htmlLang) => {
    selects.addEventListener("change", (event) => {
        let langCode = event.target.options[selects.selectedIndex].dataset.lang;
            postJson("api/LangChanges", {
                lang: langCode.toLowerCase()
            }).then(() => {
                window.location.reload()
            })

    });
}

export const languagesChanges = () => {
    const selects = document.querySelector(".flag_selector");
    const htmlLang = document.querySelector("html").lang;
    const options = [...selects.options];


    langSelected(options, htmlLang)

    langRequest(selects, htmlLang)

}
