// source --> https://solarssk.com/wp-content/themes/ein-des-ein/template-parts/blocks/faq-new/block.js?ver=6.8.2 
document.addEventListener("DOMContentLoaded", function() {
    // Весь ваш существующий код теперь находится внутри этой функции
    document.querySelectorAll(".faq-questions").forEach((btn) => {
        btn.addEventListener("click", () => {
            const item = btn.closest(".faq-items");
            const answer = item.querySelector(".faq-answers");

            // Снимаем обработчик transitionend с других блоков, если они есть
            document.querySelectorAll(".faq-items.active .faq-answers").forEach(openAnswer => {
                if (openAnswer !== answer) {
                    openAnswer.style.maxHeight = "0";
                    openAnswer.closest(".faq-items").classList.remove("active");
                }
            });

            if (item.classList.contains("active")) {
                // Этот блок кода немного упрощен для более плавной анимации закрытия
                answer.style.maxHeight = answer.scrollHeight + "px";
                setTimeout(() => {
                    answer.style.maxHeight = "0";
                }, 10); // Небольшая задержка для срабатывания transition
                item.classList.remove("active");
            } else {
                answer.style.maxHeight = answer.scrollHeight + "px";
                item.classList.add("active");

                answer.addEventListener(
                    "transitionend",
                    () => {
                        if (item.classList.contains("active")) {
                            answer.style.maxHeight = "none";
                        }
                    },
                    { once: true }
                );
            }
        });
    });
});
// source --> https://solarssk.com/wp-content/themes/ein-des-ein/template-parts/blocks/form-contact/block.js?ver=6.8.2 
document.addEventListener("DOMContentLoaded", function () {
  const input = document.querySelector("#form_contact-phone");

  if (!input) return;

  if (input.dataset.itiInit === "true") return;
  input.dataset.itiInit = "true";

  if (typeof window.intlTelInput !== "function") return;

  const lang = document.documentElement.lang || "uk";

  function getCountryByLang(lang) {
    lang = lang.toLowerCase();

    if (lang.includes("uk") || lang.includes("ua")) return "ua";
    if (lang.includes("pl")) return "pl";
    if (lang.includes("en")) return "us";
    if (lang.includes("de")) return "de";

    return "ua";
  }

  const country = getCountryByLang(lang);

  const iti = window.intlTelInput(input, {
    initialCountry: country,
    separateDialCode: true,
    preferredCountries: ["ua", "pl", "us", "gb", "de"],
    utilsScript:
      "https://cdn.jsdelivr.net/npm/intl-tel-input@18.1.1/build/js/utils.js",
  });

  setTimeout(() => {
    iti.setCountry(country);
  }, 100);
});

document.addEventListener("DOMContentLoaded", function () {
  const fields = document.querySelectorAll(".form__field");

  fields.forEach((field) => {
    const input = field.querySelector("input, textarea");

    if (!input) return;

    function check() {
      if (input.value.trim() !== "") {
        field.classList.add("filled");
      } else {
        field.classList.remove("filled");
      }
    }

    input.addEventListener("focus", () => {
      field.classList.add("focused");
    });

    input.addEventListener("blur", () => {
      field.classList.remove("focused");
      check();
    });

    input.addEventListener("input", check);

    check();
  });
});