"use client";

import { useEffect } from "react";
import { usePathname, useSearchParams } from "next/navigation";
import { useLenis } from "lenis/react";
import { sendGTMEvent } from "@next/third-parties/google";
import useShouldTrackAnalytics from "@/hooks/useShouldTrackAnalytics";

const NavigationEvent = () => {
  const shouldTrackAnalytics = useShouldTrackAnalytics();
  const pathname = usePathname();
  const searchParams = useSearchParams();
  const lenis = useLenis();

  useEffect(() => {
    const url = `${pathname}?${searchParams}`;

    // @ts-ignore
    document?.lazyLoadInstance?.update();

    if (shouldTrackAnalytics) {
      sendGTMEvent({
        event: "send_page_view",
        value: true,
        page: window.location.href,
      });
    }

    lenis?.scrollTo(0, {
      immediate: true,
    });
  }, [pathname, searchParams]);

  return null;
};

export default NavigationEvent;
