import { useCallback, useEffect, useRef } from "react";

import { Job } from "@enact/core/util";

export default function useScrollReset(scrollTo, animate) {
  const jobRef = useRef(
    new Job((func) => {
      func();
    }, 0)
  );

  const handleScrollReset = useCallback(() => {
    if (scrollTo) {
      jobRef.current.start(() => scrollTo({ animate: animate }));
    }
  }, [scrollTo]);

  const handleStopScrolling = useCallback(() => {
    jobRef.current.stop();
  }, []);

  useEffect(() => {
    return () => {
      jobRef.current.stop();
    };
  }, []);

  return { handleScrollReset, handleStopScrolling };
}
