import { useEffect, useState } from 'react';

interface UsePaginationProps<T> {
  items: T[];
  itemsPerPage: number;
  dependencies?: any[];
}

interface UsePaginationReturn<T> {
  currentPage: number;
  setCurrentPage: (page: number) => void;
  paginatedItems: T[];
  totalPages: number;
}

export const usePagination = <T>({
  items,
  itemsPerPage,
  dependencies = [],
}: UsePaginationProps<T>): UsePaginationReturn<T> => {
  const [currentPage, setCurrentPage] = useState(1);
  const totalPages = Math.ceil(items.length / itemsPerPage);

  const paginatedItems = items.slice(
    (currentPage - 1) * itemsPerPage,
    currentPage * itemsPerPage
  );

  useEffect(() => {
    setCurrentPage(1);
  }, [...dependencies]);

  return {
    currentPage,
    setCurrentPage,
    paginatedItems,
    totalPages,
  };
};
