(function () {
  function initToggle(wrapper) {
    var input = wrapper.querySelector(".input-toggle__input");
    var btn = wrapper.querySelector(".input-toggle__btn");
    if (!input || !btn || btn.dataset.bound === "1") return;

    btn.dataset.bound = "1";
    var icon = btn.querySelector("i");
    var hiddenLabel = btn.getAttribute("aria-label") || "Mostra";
    var visibleLabel = btn.getAttribute("data-label-hide") || "Nascondi";

    function setVisible(visible) {
      input.type = visible ? input.dataset.visibleType || "text" : "password";
      btn.setAttribute("aria-label", visible ? visibleLabel : hiddenLabel);
      btn.setAttribute("aria-pressed", visible ? "true" : "false");
      if (icon) {
        icon.classList.toggle("fa-eye", !visible);
        icon.classList.toggle("fa-eye-slash", visible);
      }
    }

    setVisible(false);
    btn.addEventListener("click", function () {
      setVisible(input.type === "password");
    });
  }

  function initAll() {
    document.querySelectorAll("[data-input-toggle]").forEach(initToggle);
  }

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