(function () {
  function initLocaleDropdown() {
    var localeRoot = document.getElementById("kc-locale");
    var toggleBtn = document.getElementById("kc-current-locale-link");
    var menu = document.getElementById("language-switch1");
    if (!localeRoot || !toggleBtn || !menu) return;

    toggleBtn.setAttribute("type", "button");

    function closeMenu() {
      localeRoot.classList.remove("ty-locale-open");
      toggleBtn.setAttribute("aria-expanded", "false");
    }

    toggleBtn.addEventListener("click", function (e) {
      e.preventDefault();
      e.stopPropagation();
      var isOpen = localeRoot.classList.toggle("ty-locale-open");
      toggleBtn.setAttribute("aria-expanded", isOpen ? "true" : "false");
    });

    document.addEventListener("click", function (e) {
      if (!localeRoot.contains(e.target)) {
        closeMenu();
      }
    });

    document.addEventListener("keydown", function (e) {
      if (e.key === "Escape") {
        closeMenu();
      }
    });
  }

  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", initLocaleDropdown);
  } else {
    initLocaleDropdown();
  }
})();
