const flyover = document.querySelector("[data-contact-flyover]");
const openButtons = document.querySelectorAll("[data-open-contact]");
const closeButtons = document.querySelectorAll("[data-close-contact]");
const copyButtons = document.querySelectorAll("[data-copy-value]");

const openFlyover = () => {
  if (!flyover) return;
  flyover.hidden = false;
  document.body.classList.add("is-scroll-locked");
};

const closeFlyover = () => {
  if (!flyover) return;
  flyover.hidden = true;
  document.body.classList.remove("is-scroll-locked");
};

openButtons.forEach((button) => {
  button.addEventListener("click", openFlyover);
});

closeButtons.forEach((button) => {
  button.addEventListener("click", closeFlyover);
});

document.addEventListener("keydown", (event) => {
  if (event.key === "Escape" && flyover && !flyover.hidden) {
    closeFlyover();
  }
});

copyButtons.forEach((button) => {
  button.addEventListener("click", async () => {
    const value = button.dataset.copyValue;
    if (!value) return;

    try {
      await navigator.clipboard.writeText(value);
      const label = button.querySelector("span");
      button.classList.add("is-copied");
      if (label) label.textContent = "Kopiert";

      window.setTimeout(() => {
        button.classList.remove("is-copied");
        if (label) label.textContent = "Kopieren";
      }, 1800);
    } catch (error) {
      console.error("Copy failed", error);
    }
  });
});
