import { jsx, jsxs } from 'react/jsx-runtime';
import { useMemo, forwardRef } from 'react';
import { c as cn } from './utils-DdHUxIdC.js';
import { Pagination as Pagination$1, PaginationContent, PaginationItem, PaginationPrevious, PaginationEllipsis, PaginationLink, PaginationNext } from './primitives/Pagination.js';

const DOTS = "...";

function range(start, end) {
    return Array.from({ length: end - start + 1 }, (_, i) => i + start);
}

function usePaginationRange({ count, pageSize, siblingCount = 1, pageNo, }) {
    return useMemo(() => {
        const totalPageCount = Math.ceil(count / pageSize);
        const totalPageNumbers = siblingCount + 5;
        if (totalPageNumbers >= totalPageCount) {
            return range(1, totalPageCount);
        }
        const leftSiblingIndex = Math.max(pageNo - siblingCount, 1);
        const rightSiblingIndex = Math.min(pageNo + siblingCount, totalPageCount);
        const shouldShowLeftDots = leftSiblingIndex > 2;
        const shouldShowRightDots = rightSiblingIndex < totalPageCount - 2;
        if (!shouldShowLeftDots && shouldShowRightDots) {
            const leftRange = range(1, 3 + 2 * siblingCount);
            return [...leftRange, DOTS, totalPageCount];
        }
        if (shouldShowLeftDots && !shouldShowRightDots) {
            const rightRange = range(totalPageCount - (3 + 2 * siblingCount) + 1, totalPageCount);
            return [1, DOTS, ...rightRange];
        }
        if (shouldShowLeftDots && shouldShowRightDots) {
            const middleRange = range(leftSiblingIndex, rightSiblingIndex);
            return [1, DOTS, ...middleRange, DOTS, totalPageCount];
        }
        return range(1, totalPageCount);
    }, [count, pageSize, siblingCount, pageNo]);
}

const Pagination = forwardRef(({ count, pageNo, pageSize, siblingCount = 1, navigate, className, ...otherProps }, ref) => {
    const paginationRange = usePaginationRange({
        count,
        pageSize,
        siblingCount,
        pageNo,
    });
    const lastPage = paginationRange[paginationRange.length - 1];
    const isFirstPage = pageNo === 1;
    const isLastPage = pageNo === lastPage;
    // Don't render if only 1 page or no pages
    if (pageNo === 0 || paginationRange.length < 2) {
        return null;
    }
    const handleNavigate = (page) => {
        navigate?.(page);
    };
    return (jsx(Pagination$1, { ref: ref, className: cn(className), ...otherProps, children: jsxs(PaginationContent, { children: [jsx(PaginationItem, { children: jsx(PaginationPrevious, { onClick: () => !isFirstPage && handleNavigate(pageNo - 1), "aria-disabled": isFirstPage, className: cn(isFirstPage && "pointer-events-none opacity-50") }) }), paginationRange.map((page, index) => (jsx(PaginationItem, { children: page === DOTS ? (jsx(PaginationEllipsis, {})) : (jsx(PaginationLink, { isActive: page === pageNo, onClick: () => handleNavigate(page), children: page })) }, `${page}-${index}`))), jsx(PaginationItem, { children: jsx(PaginationNext, { onClick: () => !isLastPage && handleNavigate(pageNo + 1), "aria-disabled": isLastPage, className: cn(isLastPage && "pointer-events-none opacity-50") }) })] }) }));
});
Pagination.displayName = "Pagination";

export { Pagination as P };
//# sourceMappingURL=Pagination-DSc9yXPy.js.map
