(function () {
  // ===== Reveal on scroll (IntersectionObserver) =====
  const revealEls = document.querySelectorAll(".sns-reveal");
  if ("IntersectionObserver" in window) {
    const io = new IntersectionObserver((entries) => {
      entries.forEach((e) => {
        if (e.isIntersecting) {
          e.target.classList.add("is-in");
          io.unobserve(e.target);
        }
      });
    }, { threshold: 0.12 });
    revealEls.forEach(el => io.observe(el));
  } else {
    revealEls.forEach(el => el.classList.add("is-in"));
  }

  // ===== Quick View Modal =====
  const qv = document.getElementById("snsQv");
  const qvImg = document.getElementById("snsQvImg");
  const qvName = document.getElementById("snsQvName");
  const qvPrice = document.getElementById("snsQvPrice");
  const qvCompare = document.getElementById("snsQvCompare");
  const qvGsm = document.getElementById("snsQvGsm");
  const qvSize = document.getElementById("snsQvSize");
  const qvLink = document.getElementById("snsQvLink");

  function openQv(data) {
    if (!qv) return;
    qvImg.src = data.img;
    qvImg.alt = data.name;
    qvName.textContent = data.name;

    qvPrice.textContent = "৳ " + (data.price || "0.00");
    if (data.compare) {
      qvCompare.textContent = "৳ " + data.compare;
      qvCompare.style.display = "inline";
    } else {
      qvCompare.textContent = "";
      qvCompare.style.display = "none";
    }

    qvGsm.textContent = data.gsm || "—";
    qvSize.textContent = data.size || "Standard";
    qvLink.href = data.url || "#";

    qv.setAttribute("aria-hidden", "false");
    document.body.style.overflow = "hidden";
  }

  function closeQv() {
    if (!qv) return;
    qv.setAttribute("aria-hidden", "true");
    document.body.style.overflow = "";
  }

  document.addEventListener("click", (e) => {
    const btn = e.target.closest("[data-qv='1']");
    if (btn) {
      openQv({
        name: btn.dataset.name || "",
        img: btn.dataset.img || "",
        price: btn.dataset.price || "",
        compare: btn.dataset.compare || "",
        gsm: btn.dataset.gsm || "",
        size: btn.dataset.size || "",
        url: btn.dataset.url || "#",
      });
    }
    if (e.target.closest("[data-qv-close]")) closeQv();
  });

  document.addEventListener("keydown", (e) => {
    if (e.key === "Escape") closeQv();
  });

  // ===== Zoom Modal (works for quick view + product detail) =====
  const zoom = document.getElementById("snsZoom");
  const zoomImg = document.getElementById("snsZoomImg");

  function openZoom(src, alt) {
    if (!zoom || !zoomImg) return;
    zoomImg.src = src;
    zoomImg.alt = alt || "";
    zoom.setAttribute("aria-hidden", "false");
    document.body.style.overflow = "hidden";
  }

  function closeZoom() {
    if (!zoom) return;
    zoom.setAttribute("aria-hidden", "true");
    document.body.style.overflow = "";
  }

  const zoomBtn = document.getElementById("snsZoomBtn");
  if (zoomBtn) {
    zoomBtn.addEventListener("click", () => {
      // if quick view is open -> use snsQvImg else use snsZoomSource on detail page
      const source = document.getElementById("snsQvImg")?.src || document.getElementById("snsZoomSource")?.src;
      const alt = document.getElementById("snsQvImg")?.alt || document.getElementById("snsZoomSource")?.alt;
      if (source) openZoom(source, alt);
    });
  }

  document.addEventListener("click", (e) => {
    if (e.target.closest("[data-zoom-close]")) closeZoom();
  });

  document.addEventListener("keydown", (e) => {
    if (e.key === "Escape") closeZoom();
  });
})();