document.addEventListener("DOMContentLoaded", () => {
    var dropdownItems = document.querySelectorAll(".edlangs-dropdown .dropdown-item");
    
    dropdownItems.forEach(item => {
        var href = item.getAttribute("href");
        var hreflang = item.getAttribute("hreflang");
        
        if (href && hreflang) {
            let updatedHref = href.replace(/&?edlanguage=[^&]*/g, "");
            updatedHref = updatedHref.replace("/" + hreflang + "/", "/");
            var newHref = `${updatedHref}${updatedHref.includes("?") ? "&" : "?"}edlanguage=${hreflang}`;
            item.setAttribute("href", newHref);
        }
    });
});
