import { useCallback, useRef } from 'react';
import type { NativeSyntheticEvent } from 'react-native';

const useHandlePageScroll = () => {
  // Used as a flag to prevent calling onTabPress on initial render
  const hasScrolled = useRef(false);

  const onPageScrollStateChanged = useCallback(
    (
      e: NativeSyntheticEvent<
        Readonly<{
          pageScrollState: 'idle' | 'dragging' | 'settling';
        }>
      >
    ) => {
      if (
        !hasScrolled.current &&
        e.nativeEvent.pageScrollState === 'dragging'
      ) {
        hasScrolled.current = true;
      }
    },
    []
  );

  return {
    onPageScrollStateChanged,
    hasScrolled,
  };
};

export default useHandlePageScroll;
