export function useScrollEnd() {
  let callback: Function | null = null

  const handleScroll = () => {
    const scrollPosition = window.innerHeight + window.scrollY
    const pageHeight = document.documentElement.offsetHeight - 1 // 1 pixel offset to fix mobile browser

    if (scrollPosition >= pageHeight && typeof callback === 'function') {
      callback()
    }
  }

  const onScrollEnd = (cb: Function | null) => {
    callback = cb
  }

  onMounted(() => {
    window.addEventListener('scroll', handleScroll)
  })

  onUnmounted(() => {
    window.removeEventListener('scroll', handleScroll)
  })

  return { onScrollEnd }
}