/* Maple & Co. demo site — Arrow scaffolds wiring.
   Renders the photo gallery (Swiper + PhotoSwipe) and the events calendar
   (vkurko Event Calendar) from local JSON. No third-party network calls. */
(function () {
  "use strict";

  /* ---- Highlight today's row in the hours table ---- */
  var todayIdx = new Date().getDay(); // 0=Sun..6=Sat
  var rows = document.querySelectorAll(".hours-table tr[data-day]");
  rows.forEach(function (tr) {
    if (parseInt(tr.getAttribute("data-day"), 10) === todayIdx) {
      tr.classList.add("today");
    }
  });

  /* ---- Gallery: Swiper carousel + PhotoSwipe fullscreen ---- */
  function initGallery() {
    var track = document.getElementById("gallery-track");
    if (!track || typeof Swiper === "undefined") return;

    fetch("data/gallery.json", { cache: "no-store" })
      .then(function (r) { return r.json(); })
      .then(function (items) {
        if (!Array.isArray(items) || !items.length) return;
        track.innerHTML = items.map(function (it) {
          var src = it.src;
          var w = it.w || 1200, h = it.h || 900;
          var cap = (it.caption || "").replace(/"/g, "&quot;");
          return (
            '<div class="swiper-slide">' +
              '<a href="' + src + '" data-pswp-width="' + w + '" data-pswp-height="' + h +
                '" target="_blank" rel="noreferrer">' +
                '<img src="' + src + '" alt="' + cap + '" loading="lazy" width="' + w + '" height="' + h + '">' +
              "</a>" +
            "</div>"
          );
        }).join("");

        /* eslint-disable no-new */
        new Swiper(".gallery-swiper", {
          slidesPerView: 1,
          spaceBetween: 12,
          loop: items.length > 2,
          pagination: { el: ".swiper-pagination", clickable: true },
          navigation: { nextEl: ".swiper-button-next", prevEl: ".swiper-button-prev" },
          breakpoints: {
            700: { slidesPerView: 2 },
            1000: { slidesPerView: 3 }
          }
        });

        // PhotoSwipe (ESM) for click-to-fullscreen
        import("../vendor/photoswipe-lightbox.esm.min.js").then(function (mod) {
          var PhotoSwipeLightbox = mod.default;
          var lightbox = new PhotoSwipeLightbox({
            gallery: ".gallery-swiper",
            children: "a",
            pswpModule: function () { return import("../vendor/photoswipe.esm.min.js"); }
          });
          lightbox.init();
        }).catch(function () { /* fullscreen optional */ });
      })
      .catch(function () { /* gallery optional */ });
  }

  /* ---- Calendar: vkurko Event Calendar from events.json ---- */
  function initCalendar() {
    var el = document.getElementById("calendar");
    if (!el || typeof EventCalendar === "undefined") return;

    fetch("data/events.json", { cache: "no-store" })
      .then(function (r) { return r.json(); })
      .then(function (events) {
        if (!Array.isArray(events)) events = [];
        // Render an upcoming-events list view.
        EventCalendar.create(el, {
          view: "listMonth",
          headerToolbar: {
            start: "title",
            center: "",
            end: "dayGridMonth,listMonth prev,next today"
          },
          buttonText: { dayGridMonth: "Month", listMonth: "List", today: "Today" },
          events: events.map(function (e) {
            return {
              id: e.id,
              title: e.title + (e.location ? "  ·  " + e.location : ""),
              start: e.start,
              end: e.end || e.start
            };
          }),
          eventClick: function (info) {
            var ev = events.filter(function (x) { return x.id === info.event.id; })[0];
            if (ev && ev.description) { alert(ev.title + "\n\n" + ev.description); }
          }
        });
      })
      .catch(function () {
        el.innerHTML = '<p class="events-fallback">Events are loading… check back soon.</p>';
      });
  }

  initGallery();
  initCalendar();
})();
