"use client";

import { useEffect } from "react";

interface PageBootProps {
  inlineScripts: string[];
}

/**
 * Initialises Swiper, GLightbox, and Lucide icons for the lifted-and-shifted
 * page markup, then executes the page's original inline scripts (which expect
 * `Swiper`, `GLightbox`, `lucide` on `window`).
 */
export function PageBoot({ inlineScripts }: PageBootProps) {
  useEffect(() => {
    let cancelled = false;
    (async () => {
      const [{ default: Swiper }, modulesPkg, glightboxModule, lucide] = await Promise.all([
        import("swiper"),
        import("swiper/modules"),
        import("glightbox"),
        import("lucide"),
      ]);
      if (cancelled) return;
      const w = window as unknown as Record<string, unknown>;
      w.Swiper = Swiper;
      w.SwiperModules = modulesPkg;
      w.GLightbox = (glightboxModule as { default: unknown }).default;
      w.lucide = lucide;
      try {
        (lucide as { createIcons: () => void }).createIcons();
      } catch {
        // no-op if no [data-lucide] nodes present
      }
      for (const code of inlineScripts) {
        try {
          new Function(code)();
        } catch (err) {
          console.error("[PageBoot] inline script failed:", err);
        }
      }
    })();
    return () => {
      cancelled = true;
    };
  }, [inlineScripts]);

  return null;
}
