// "use client";

// import { ReactNode, useRef, useEffect } from "react";
// import { ReactLenis } from "@studio-freight/react-lenis";
// import gsap from "gsap";
// import { ScrollTrigger } from "gsap/ScrollTrigger";
// import { useGSAP } from "@gsap/react";
// import { ScrollToPlugin } from "gsap/ScrollToPlugin";
// import { SplitText } from "gsap/SplitText";

// interface SmoothScrollProps {
//   children: ReactNode;
// }

// if (typeof window !== "undefined") {
//   gsap.registerPlugin(ScrollTrigger, useGSAP, ScrollToPlugin, SplitText);
// }

// export default function SmoothScrollProvider({ children }: SmoothScrollProps) {
//   // eslint-disable-next-line @typescript-eslint/no-explicit-any
//   const lenisRef = useRef<any>(null);

//   useEffect(() => {
//     // GSAP animation example that works with Lenis
//     const ctx = gsap.context(() => {
//       // Your GSAP animations here will work seamlessly with Lenis
//     });

//     return () => ctx.revert();
//   }, []);

//   const lenisOptions = {
//     lerp: 0.08,
//     duration: 1.5,
//     smoothWheel: true,
//     smoothTouch: false,
//     wheelMultiplier: 0.8,
//     touchMultiplier: 1.5,
//     normalizeWheel: true,
//     easing: (t: number) => 1 - Math.pow(1 - t, 3),
//   };

//   return (
//     <ReactLenis
//       root
//       ref={lenisRef}
//       options={lenisOptions}
//       autoRaf={true}
//       className="h-full"
//     >
//       <div className="lenis-content">{children}</div>
//     </ReactLenis>
//   );
// }

// "use client";

// import { ReactNode, useEffect, useLayoutEffect, useRef } from "react";
// import { ReactLenis, useLenis } from "@studio-freight/react-lenis";
// import gsap from "gsap";
// import { ScrollTrigger } from "gsap/ScrollTrigger";

// gsap.registerPlugin(ScrollTrigger);

// interface SmoothScrollProps {
//   children: ReactNode;
// }

// export default function SmoothScrollProvider({ children }: SmoothScrollProps) {
//   const containerRef = useRef(null);
//   const lenis = useLenis(({ scroll }) => {
//     ScrollTrigger.update();
//   });

//   useLayoutEffect(() => {
//     if (!lenis) return;

//     // Tell ScrollTrigger to use Lenis instead of default browser scroll
//     ScrollTrigger.scrollerProxy(lenis?.root, {
//       scrollTop(value) {
//         return arguments.length
//           ? lenis.scrollTo(value, { immediate: true })
//           : lenis.scroll;
//       },
//       getBoundingClientRect() {
//         return {
//           top: 0,
//           left: 0,
//           width: window.innerWidth,
//           height: window.innerHeight,
//         };
//       },
//     });

//     // Recalculate everything when Lenis updates
//     lenis.on("scroll", ScrollTrigger.update);

//     // Refresh ScrollTrigger after layout
//     ScrollTrigger.addEventListener("refresh", () => lenis?.resize());
//     ScrollTrigger.refresh();

//     return () => {
//       ScrollTrigger.removeEventListener("refresh", () => lenis?.resize());
//     };
//   }, [lenis]);

//   return (
//     <ReactLenis
//       root
//       options={{
//         lerp: 0.1,
//         duration: 1.0,
//         smoothWheel: true,
//         smoothTouch: true, // IMPORTANT for mobile smoothness
//         touchMultiplier: 1.2,
//       }}
//       ref={containerRef}
//     >
//       {children}
//     </ReactLenis>
//   );
// }

"use client";

import { ReactNode, useLayoutEffect, useRef, useEffect } from "react";
import { ReactLenis, useLenis } from "@studio-freight/react-lenis";
import gsap from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import { usePathname } from "next/navigation";

gsap.registerPlugin(ScrollTrigger);

interface SmoothScrollProps {
  children: ReactNode;
}

export default function SmoothScrollProvider({ children }: SmoothScrollProps) {
  const containerRef = useRef(null);
  const pathname = usePathname();

  const lenis = useLenis(() => {
    ScrollTrigger.update(); // keep GSAP in sync
  });

  useLayoutEffect(() => {
    if (!lenis || typeof window === "undefined") return;

    // --- Connect GSAP to LENIS Scroller ---
    // Use window as the scroller proxy target since Lenis manages window scroll
    ScrollTrigger.scrollerProxy(window, {
      scrollTop(value?: number) {
        if (arguments.length && value !== undefined) {
          lenis.scrollTo(value, { immediate: true });
          return;
        }
        return lenis.scroll ?? 0;
      },
      getBoundingClientRect() {
        return {
          top: 0,
          left: 0,
          width: window.innerWidth,
          height: window.innerHeight,
        };
      },
    });

    // All ScrollTriggers use window (Lenis) by default (important!)
    ScrollTrigger.defaults({ scroller: window });

    lenis.on("scroll", ScrollTrigger.update);

    ScrollTrigger.addEventListener("refresh", () => {
      lenis?.resize();
    });
    ScrollTrigger.refresh();

    // Run refresh again after fonts/layout complete (mobile smoothness booster)
    requestAnimationFrame(() => ScrollTrigger.refresh());

    return () => {
      lenis.off("scroll", ScrollTrigger.update);
      ScrollTrigger.removeEventListener("refresh", () => {
        lenis?.resize();
      });
    };
  }, [lenis]);

  // Refresh ScrollTrigger on route changes to handle page navigation
  useEffect(() => {
    // Delay to allow page content to render
    const timeoutId = setTimeout(() => {
      ScrollTrigger.refresh();
    }, 100);

    return () => {
      clearTimeout(timeoutId);
    };
  }, [pathname]);

  return (
    <ReactLenis
      root
      ref={containerRef}
      options={{
        lerp: 0.07,
      }}
    >
      {/* eslint-disable-next-line @typescript-eslint/no-explicit-any */}
      {children as any}
    </ReactLenis>
  );
}
