import { useLenis } from "lenis/react";
import { useEffect } from "react";
import { useLocation } from "react-router-dom";

export const useScrollTop = (arg?: string | number) => {
  const lenis = useLenis();
  const { hash } = useLocation();

  useEffect(() => {
    if (!lenis) return;

    if (hash) {
      const el = document.querySelector(hash);
      if (el) {
        lenis.scrollTo(el as HTMLElement, {
          lerp: 0.3,
          duration: 0.8,
          offset: -140,
        });
        return;
      }
    }

    lenis.scrollTo(0, {
      lerp: 0.3,
      duration: 0.3,
    });
  }, [lenis, arg, hash]);
};
