import {
  type NativeScrollEvent,
  type NativeSyntheticEvent,
} from 'react-native'

export const isCloseToBottom = (
  {contentOffset, layoutMeasurement, contentSize}: NativeScrollEvent,
  threshold = 120,
): boolean =>
  contentOffset.y + layoutMeasurement.height >=
  contentSize.height - threshold

// scroll props for a ScrollView that pages a non-virtualized list:
// fires onEndReached as the bottom nears. spread onto Layout.ModalContent
export const infiniteScroll = (onEndReached: () => void) => ({
  scrollEventThrottle: 16,
  onScroll: (e: NativeSyntheticEvent<NativeScrollEvent>) => {
    if (isCloseToBottom(e.nativeEvent)) onEndReached()
  },
})
