// source --> https://solarssk.com/wp-content/plugins/sitepress-multilingual-cms/res/js/cookies/language-cookie.js?ver=492100 
document.addEventListener('DOMContentLoaded', function() {
	for(var cookieName in wpml_cookies) {
		var cookieData = wpml_cookies[cookieName];
		document.cookie = cookieName + '=' + cookieData.value + ';expires=' + cookieData.expires + '; path=' + cookieData.path + '; SameSite=Lax';
	}
});
// source --> https://solarssk.com/wp-content/themes/ein-des-ein/template-parts/blocks/application-section/block.js?ver=6.8.2 
document.addEventListener("DOMContentLoaded", function () {
  // ===============================
  // Language phone
  // ===============================
  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";
  }

  // ===============================
  // PHONE INPUT
  // ===============================
  var input = document.querySelector(".phone-input");

  if (input) {
    var iti = window.intlTelInput(input, {
      initialCountry: getCountryByLang(lang),
      separateDialCode: true,
      preferredCountries: ["ua", "pl", "gb", "us"],
      utilsScript:
        "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/utils.js",
    });

    setTimeout(() => {
      iti.setCountry(getCountryByLang(lang));
    }, 100);

    // ===============================
    // PADDING
    // ===============================
    const forcePadding = () => {
      let currentPadding = input.style.paddingLeft;
      if (currentPadding && !currentPadding.includes("important")) {
        input.style.setProperty("padding-left", currentPadding, "important");
      }
    };

    const observer = new MutationObserver((mutations) => {
      mutations.forEach((mutation) => {
        if (mutation.attributeName === "style") forcePadding();
      });
    });

    observer.observe(input, { attributes: true });

    // ===============================
    // LABEL ANIMATION
    // ===============================
    const handleLabel = () => {
      input.value.trim() !== ""
        ? input.classList.add("has-content")
        : input.classList.remove("has-content");
    };

    input.addEventListener("countrychange", handleLabel);
    input.addEventListener("input", handleLabel);

    setTimeout(() => {
      forcePadding();
      handleLabel();
    }, 200);
  }

  // ===============================
  // CHECKBOX → RADIO BEHAVIOR
  // ===============================
  const names = ["own-brigades", "warehouse-choice", "promote", "training"];

  names.forEach((name) => {
    const inputs = document.querySelectorAll(
      `.wpcf7-checkbox input[name="${name}[]"]`,
    );

    inputs.forEach((input) => {
      const wrapper = input.closest(".wpcf7-checkbox");

      if (wrapper) {
        wrapper.classList.add("radio-like");
      }

      // logic like radio
      input.addEventListener("change", () => {
        if (input.checked) {
          inputs.forEach((other) => {
            if (other !== input) {
              other.checked = false;
            }
          });
        }
      });
    });
  });

  // ===============================
  // BUTTON REPLACE
  // ===============================
  const submitInput = document.querySelector(".application-form .wpcf7-submit");

  if (submitInput && submitInput.tagName === "INPUT") {
    const btnValue = submitInput.value;
    const btnClasses = submitInput.className;

    const newBtn = document.createElement("button");
    newBtn.type = "submit";

    newBtn.className = btnClasses;
    newBtn.innerHTML = `
      <span class="me-2">${btnValue}</span>
      <span class="arrow">→</span>
      <span class="bubbles">
          <span></span><span></span><span></span><span></span>
          <span></span><span></span><span></span><span></span>
      </span>
      <span class="wpcf7-spinner"></span>
    `;

    submitInput.parentNode.replaceChild(newBtn, submitInput);
  }
});

document.addEventListener(
  "wpcf7invalid",
  function (event) {
    setTimeout(() => {
      const firstError = document.querySelector(".wpcf7-not-valid");
      if (firstError) {
        const parentDetails = firstError.closest("details");
        if (parentDetails) {
          parentDetails.open = true;

          firstError.scrollIntoView({ behavior: "smooth", block: "center" });
        }
      }
    }, 50);
  },
  false,
);

document.addEventListener(
  "wpcf7invalid",
  function (event) {
    setTimeout(() => {
      document.querySelectorAll(".wpcf7-form-control-wrap").forEach((wrap) => {
        if (!wrap.querySelector(".wpcf7-not-valid-tip")) {
          const badge = wrap.querySelector(".wpcf7-not-valid");
          if (badge) badge.classList.remove("wpcf7-not-valid");
        }
      });
    }, 100);
  },
  false,
);

// Checkbox like radio
document.addEventListener("DOMContentLoaded", function () {
  const names = ["own-brigades", "warehouse-choice", "promote", "training"];

  names.forEach((name) => {
    const inputs = document.querySelectorAll(
      `.wpcf7-checkbox input[name="${name}[]"]`,
    );

    inputs.forEach((input) => {
      const wrapper = input.closest(".wpcf7-checkbox");

      if (wrapper) {
        wrapper.classList.add("radio-like");
      }

      input.addEventListener("change", () => {
        if (input.checked) {
          inputs.forEach((other) => {
            if (other !== input) {
              other.checked = false;
            }
          });
        }
      });
    });
  });
});