// import type { Word } from '../interfaces/index';

const useInfiniteScroll = (loadList, pageSize = 30) => {
  let current = 0
  let total = 0
  let data = []
  const size = pageSize

  function setData(list) {
    data = list
    total = data.length
    current = 1
    loadList.value = []
    load()
  }

  function load() {
    if (!data.length) {
      loadList.value = []
    } else {
      if (loadList.value.length >= data.length) return
      const newLoad = data.slice(0, current * size)
      loadList.value = newLoad
      if (current * size <= total) {
        current++
      }
    }
  }

  function handleScroll() {
    const scrollTop = document.getElementsByClassName('level__page')[0].scrollTop
    if (scrollTop < 10) {
      load()
    }
  }

  return {
    setData,
    load,
    handleScroll,
  }
}

export default useInfiniteScroll
