import { ref, computed } from 'vue'

interface UsePaginationOptions {
  pageSize?: number
  fetchFn: (page: number) => Promise<void>
}

export function usePagination(opts: UsePaginationOptions) {
  const { fetchFn, pageSize: initialPageSize = 20 } = opts
  const page = ref(1)
  const pageSize = ref(initialPageSize)
  const total = ref(0)

  const totalPages = computed(() => Math.max(1, Math.ceil(total.value / pageSize.value)))
  const hasMore = computed(() => page.value < totalPages.value)

  async function goToPage(n: number) {
    page.value = n
    await fetchFn(n)
  }

  return { page, pageSize, total, totalPages, hasMore, goToPage }
}
