import { useLayoutEffect } from 'react';
import { useLocation } from 'react-router-dom';

interface ScrollToTopProps {
  scrollBehavior?: ScrollBehavior;
  element?: HTMLElement | null | Window;
}

export const useScrollToTop = ({
  scrollBehavior = 'auto',
  element = window
}: ScrollToTopProps = {}) => {
  const { pathname } = useLocation();

  useLayoutEffect(() => {
    if (!element) return;

    element.scrollTo({
      top: 0,
      left: 0,
      behavior: scrollBehavior
    });
  }, [pathname]);
};
