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

export default function useScrollTo() {
  const scrollTo = useRef();

  const scrollTop = useCallback(
    ({ y = 0, animate = true, duration = 300 } = {}) => {
      if (scrollTo && scrollTo.current) {
        scrollTo.current({ position: { y }, animate, duration });
      }
    },
    []
  );

  const scrollLeft = useCallback(({ x = 0, animate = false } = {}) => {
    if (scrollTo && scrollTo.current) {
      scrollTo.current({ position: { x }, animate });
    }
  }, []);

  const getScrollTo = useCallback((cbScrollTo) => {
    scrollTo.current = cbScrollTo;
  }, []);

  useEffect(() => {
    scrollTop();
  }, []);

  return { getScrollTo, scrollLeft, scrollTop, scrollToRef: scrollTo };
}
