"use client";
import { useEffect } from "react";
import { usePathname } from "next/navigation";

const ScrollToTop = () => {
  const pathname = usePathname();

  useEffect(() => {
    // Aggressive scroll to top - run immediately and synchronously
    const scrollToTop = () => {
      window.scrollTo({ top: 0, left: 0, behavior: 'auto' });
      window.scrollTo(0, 0);
      document.documentElement.scrollTop = 0;
      document.body.scrollTop = 0;
    };

    // 1. Immediate execution
    scrollToTop();

    // 2. After microtask
    Promise.resolve().then(scrollToTop);

    // 3. After next frame
    requestAnimationFrame(scrollToTop);

    // 4. Multiple delayed attempts to override any smooth scroll libraries
    const timeouts = [0, 10, 50, 100, 150, 200].map(delay => 
      setTimeout(scrollToTop, delay)
    );

    return () => {
      timeouts.forEach(clearTimeout);
    };
  }, [pathname]);

  return null;
};

export default ScrollToTop;

