import { useState, useMemo, useCallback } from "react";
const DOTS = { label: "..." };
const getPages = (currentPage, pageCount, displayedPagesCount) => {
    const isPageInStart = currentPage - 1 < displayedPagesCount - 2;
    const isPageInEnd = currentPage > pageCount - (displayedPagesCount - 2);
    const isPageInMiddle = !isPageInStart && !isPageInEnd;
    const pages = Array(pageCount)
        .fill(null)
        .map((_, i) => ({ className: "hoverable", label: (i + 1).toString() }));
    let partOfPages = [];
    if (pageCount <= displayedPagesCount) {
        return pages;
    }
    if (isPageInMiddle) {
        partOfPages = [
            DOTS,
            ...pages.slice(currentPage - 2, currentPage + 1),
            DOTS,
        ];
    }
    if (isPageInStart) {
        partOfPages = [...pages.slice(1, displayedPagesCount - 2), DOTS];
    }
    if (isPageInEnd) {
        partOfPages = [
            DOTS,
            ...pages.slice(pageCount - (displayedPagesCount - 2), pageCount - 1),
        ];
    }
    return [pages[0], ...partOfPages, pages[pages.length - 1]];
};
export const usePagination = (pageCount, displayedPagesCount = 7) => {
    const [currentPage, setCurrentPage] = useState(1);
    const pages = useMemo(() => {
        return getPages(currentPage, pageCount, displayedPagesCount);
    }, [currentPage, displayedPagesCount, pageCount]);
    const handlePrevPageClick = useCallback(() => {
        if (currentPage === 1)
            return;
        setCurrentPage(currentPage - 1);
    }, [currentPage]);
    const handleNextPageClick = useCallback(() => {
        if (currentPage === pageCount)
            return;
        setCurrentPage(currentPage + 1);
    }, [currentPage, pageCount]);
    return {
        pages,
        currentPage,
        setCurrentPage,
        handlePrevPageClick,
        handleNextPageClick,
    };
};
//# sourceMappingURL=use-pagination.js.map