import { useState } from "react";

export const useApiPagination = (initialPage = 1, initialPerPage = 10) => {
  const [currentPage, setCurrentPage] = useState(initialPage);
  const [recordsPerPage, setRecordsPerPage] = useState(initialPerPage);

  const handlePreviousPage = () => {
    setCurrentPage((prev) => Math.max(prev - 1, 1));
  };

  const handleNextPage = (totalPages: number) => {
    setCurrentPage((prev) => Math.min(prev + 1, totalPages));
  };

  const handleRecordsPerPageChange = (value: number) => {
    setRecordsPerPage(value);
    setCurrentPage(1);
  };

  const handleResetPagination = () => {
    setCurrentPage(initialPage);
    setRecordsPerPage(initialPerPage);
  }

  return {
    currentPage,
    recordsPerPage,
    setCurrentPage,
    setRecordsPerPage,
    handlePreviousPage,
    handleNextPage,
    handleRecordsPerPageChange,
    handleResetPagination
  };
};
