(function () {
  "use strict";

  var SLIDES = [
    { src: "masi-assets/monumento1.webp", alt: "Monumento 1" },
    { src: "masi-assets/monumento2.webp", alt: "Monumento 2" },
    { src: "masi-assets/monumento3.webp", alt: "Monumento 3" },
    { src: "masi-assets/audio.webp", alt: "Audio display" },
    { src: "masi-assets/edsa.webp", alt: "EDSA" },
    { src: "masi-assets/valenzuela.webp", alt: "Valenzuela", fit: "contain" },
    { src: "masi-assets/malabon.webp", alt: "Malabon" },
    { src: "masi-assets/manila.webp", alt: "Manila" },
    { src: "masi-assets/sync.webp", alt: "Sync display" },
    { src: "masi-assets/4sync.webp", alt: "4Sync location", fit: "contain" },
    { src: "masi-assets/masilogo.webp", alt: "MASI logo", fit: "contain" },
  ];

  function initCarousel(root) {
    var track = root.querySelector("[data-carousel-track]");
    var prevBtn = root.querySelector("[data-carousel-prev]");
    var nextBtn = root.querySelector("[data-carousel-next]");
    var dotsRoot = root.querySelector("[data-carousel-dots]");
    if (!track || !prevBtn || !nextBtn || !dotsRoot) return;

    var index = 0;
    var autoplayMs = 5500;
    var timer = null;

    track.innerHTML = "";
    SLIDES.forEach(function (slide, i) {
      var article = document.createElement("article");
      article.className = "hero-carousel__slide";
      if (slide.fit === "contain") {
        article.classList.add("hero-carousel__slide--contain");
      }
      article.setAttribute("role", "group");
      article.setAttribute("aria-roledescription", "slide");
      article.setAttribute("aria-label", i + 1 + " of " + SLIDES.length);

      var img = document.createElement("img");
      img.src = slide.src;
      img.alt = slide.alt;
      img.loading = i === 0 ? "eager" : "lazy";
      img.decoding = "async";
      article.appendChild(img);
      track.appendChild(article);
    });

    var dots = SLIDES.map(function (_, i) {
      var b = document.createElement("button");
      b.type = "button";
      b.className = "hero-carousel__dot";
      b.setAttribute("role", "tab");
      b.setAttribute("aria-selected", i === 0 ? "true" : "false");
      b.setAttribute("aria-label", "Go to slide " + (i + 1));
      b.addEventListener("click", function () {
        goTo(i);
      });
      dotsRoot.appendChild(b);
      return b;
    });

    function setAria() {
      root.setAttribute("aria-label", "Featured inventory, slide " + (index + 1) + " of " + SLIDES.length);
      dots.forEach(function (d, i) {
        d.setAttribute("aria-selected", i === index ? "true" : "false");
      });
    }

    function applyTransform() {
      var pct = -index * 100;
      track.style.transform = "translateX(" + pct + "%)";
      setAria();
    }

    function goTo(i) {
      index = (i + SLIDES.length) % SLIDES.length;
      applyTransform();
      restartAutoplay();
    }

    function next() {
      goTo(index + 1);
    }

    function prev() {
      goTo(index - 1);
    }

    function clearAutoplay() {
      if (timer) {
        clearInterval(timer);
        timer = null;
      }
    }

    function restartAutoplay() {
      clearAutoplay();
      timer = window.setInterval(next, autoplayMs);
    }

    prevBtn.addEventListener("click", function () {
      prev();
    });
    nextBtn.addEventListener("click", function () {
      next();
    });

    root.addEventListener("keydown", function (e) {
      if (e.key === "ArrowLeft") {
        e.preventDefault();
        prev();
      } else if (e.key === "ArrowRight") {
        e.preventDefault();
        next();
      }
    });

    root.tabIndex = 0;

    root.addEventListener("mouseenter", clearAutoplay);
    root.addEventListener("mouseleave", restartAutoplay);
    root.addEventListener("focusin", clearAutoplay);
    root.addEventListener("focusout", function (e) {
      if (!root.contains(e.relatedTarget)) restartAutoplay();
    });

    applyTransform();
    restartAutoplay();

    var mqReduce = window.matchMedia("(prefers-reduced-motion: reduce)");
    function onReduceMotionChange() {
      clearAutoplay();
      restartAutoplay();
    }
    if (mqReduce.addEventListener) {
      mqReduce.addEventListener("change", onReduceMotionChange);
    } else if (mqReduce.addListener) {
      mqReduce.addListener(onReduceMotionChange);
    }
  }

  function initMobileNav() {
    var toggle = document.querySelector(".nav-toggle");
    var nav = document.querySelector("#site-nav");
    if (!toggle || !nav) return;

    function setOpen(open) {
      nav.classList.toggle("is-open", open);
      toggle.setAttribute("aria-expanded", open ? "true" : "false");
      toggle.setAttribute("aria-label", open ? "Close menu" : "Open menu");
    }

    toggle.addEventListener("click", function () {
      setOpen(!nav.classList.contains("is-open"));
    });

    nav.querySelectorAll("a").forEach(function (link) {
      link.addEventListener("click", function () {
        if (window.matchMedia("(max-width: 900px)").matches) {
          setOpen(false);
        }
      });
    });
  }

  function initNavDropdowns() {
    var items = document.querySelectorAll(".site-nav__item--has-dropdown");
    if (!items.length) return;

    function closeAll() {
      items.forEach(function (item) {
        var trigger = item.querySelector(".site-nav__dropdown-trigger");
        var menu = item.querySelector(".site-nav__dropdown");
        if (trigger) trigger.setAttribute("aria-expanded", "false");
        if (menu) menu.hidden = true;
      });
    }

    items.forEach(function (item) {
      var trigger = item.querySelector(".site-nav__dropdown-trigger");
      var menu = item.querySelector(".site-nav__dropdown");
      if (!trigger || !menu) return;

      trigger.addEventListener("click", function (e) {
        e.stopPropagation();
        var isOpen = trigger.getAttribute("aria-expanded") === "true";
        closeAll();
        if (!isOpen) {
          trigger.setAttribute("aria-expanded", "true");
          menu.hidden = false;
        }
      });

      menu.querySelectorAll("a").forEach(function (link) {
        link.addEventListener("click", function () {
          closeAll();
        });
      });
    });

    document.addEventListener("click", closeAll);
    document.addEventListener("keydown", function (e) {
      if (e.key === "Escape") closeAll();
    });
  }

  document.addEventListener("DOMContentLoaded", function () {
    var carousel = document.querySelector("[data-carousel]");
    if (carousel) initCarousel(carousel);
    initMobileNav();
    initNavDropdowns();
  });
})();
