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

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

  function setData(list: Word[]) {
    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++
      }
    }
  }

  return {
    setData,
    load,
  }
}

export default useInfiniteScroll
