(function () {
    document.addEventListener("DOMContentLoaded", function () {
      let timerStarted = false;
      let timerInterval;
      let minutes = 20;
      let seconds = 0;
      let hours = 0;

      // Get existing timer elements
      const hoursElement = document.querySelector(".hours");
      const minutesElement = document.querySelector(".minutes");
      const secondsElement = document.querySelector(".seconds");

      function updateTimer() {
        // Format time display with leading zeros
        hoursElement.textContent = String(hours).padStart(2, "0");
        minutesElement.textContent = String(minutes).padStart(2, "0");
        secondsElement.textContent = String(seconds).padStart(2, "0");

        // Check if timer has expired
        if (hours === 0 && minutes === 0 && seconds === 0) {
          clearInterval(timerInterval);
          return;
        }

        // Update time
        if (seconds === 0) {
          if (minutes === 0) {
            if (hours > 0) {
              hours--;
              minutes = 59;
              seconds = 59;
            }
          } else {
            minutes--;
            seconds = 59;
          }
        } else {
          seconds--;
        }
      }

      function startTimer() {
        if (!timerStarted) {
          timerStarted = true;
          updateTimer();
          timerInterval = setInterval(updateTimer, 1000);
        }
      }

      // Create an Intersection Observer to watch the form
      const form = document.querySelector("form");
      const observer = new IntersectionObserver(
        (entries) => {
          entries.forEach((entry) => {
            // When form becomes visible
            if (entry.isIntersecting && !timerStarted) {
              startTimer();
              // Stop observing once timer has started
              observer.disconnect();
            }
          });
        },
        {
          // Start timer when at least 50% of the form is visible
          threshold: 0.5,
        }
      );

      // Start observing the form
      observer.observe(form);
    });
  })();
