import { jsx, jsxs } from 'react/jsx-runtime';
import React__default, { useMemo, useCallback } from 'react';
import { paginationTheme } from './Pagination.theme.js';
import { cn } from '../../../utils/cn.js';
import { ChevronDoubleLeftIcon, ChevronLeftIcon, ChevronRightIcon, ChevronDoubleRightIcon } from '@heroicons/react/24/outline';

/**
 * Internal PaginationItem component for page numbers and ellipsis
 */
const PaginationItem = React__default.forwardRef(({ isActive, isDisabled, size = "md", variant = "page", className, children, ...props }, ref) => {
    // Use appropriate style based on variant
    const baseStyle = variant === "ellipsis"
        ? paginationTheme.ellipsisStyle
        : paginationTheme.itemStyle;
    const sizeStyle = variant === "ellipsis"
        ? paginationTheme.itemSizes[size]
        : paginationTheme.itemSizes[size];
    return (jsx("button", { ref: ref, type: "button", disabled: isDisabled, "data-active": isActive, "data-disabled": isDisabled, className: cn(baseStyle, sizeStyle, className), "aria-current": isActive ? "page" : undefined, ...props, children: children }));
});
PaginationItem.displayName = "PaginationItem";
/**
 * Generate pagination range with ellipsis
 */
const usePaginationRange = (currentPage, totalPages, siblingCount = 1) => {
    return useMemo(() => {
        const DOTS = "...";
        // Total page numbers to show: current + siblings + first + last + 2 dots
        const totalPageNumbers = siblingCount + 5;
        // If total pages is less than total page numbers, show all pages
        if (totalPageNumbers >= totalPages) {
            return Array.from({ length: totalPages }, (_, i) => i + 1);
        }
        const leftSiblingIndex = Math.max(currentPage - siblingCount, 1);
        const rightSiblingIndex = Math.min(currentPage + siblingCount, totalPages);
        const shouldShowLeftDots = leftSiblingIndex > 2;
        const shouldShowRightDots = rightSiblingIndex < totalPages - 2;
        const firstPageIndex = 1;
        const lastPageIndex = totalPages;
        // No left dots, but right dots
        if (!shouldShowLeftDots && shouldShowRightDots) {
            const leftItemCount = 3 + 2 * siblingCount;
            const leftRange = Array.from({ length: leftItemCount }, (_, i) => i + 1);
            return [...leftRange, DOTS, totalPages];
        }
        // Left dots, but no right dots
        if (shouldShowLeftDots && !shouldShowRightDots) {
            const rightItemCount = 3 + 2 * siblingCount;
            const rightRange = Array.from({ length: rightItemCount }, (_, i) => totalPages - rightItemCount + i + 1);
            return [firstPageIndex, DOTS, ...rightRange];
        }
        // Both left and right dots
        if (shouldShowLeftDots && shouldShowRightDots) {
            const middleRange = Array.from({ length: rightSiblingIndex - leftSiblingIndex + 1 }, (_, i) => leftSiblingIndex + i);
            return [firstPageIndex, DOTS, ...middleRange, DOTS, lastPageIndex];
        }
        return [];
    }, [currentPage, totalPages, siblingCount]);
};
/**
 * Pagination component for navigating through pages
 */
const Pagination = React__default.forwardRef(({ currentPage, totalPages, onPageChange, size = "md", showFirstLast = false, showPrevNext = true, siblingCount = 1, className, theme: customTheme = {}, ...props }, ref) => {
    // Merge theme with custom overrides
    const theme = useMemo(() => ({
        ...paginationTheme,
        ...customTheme,
    }), [customTheme]);
    const paginationRange = usePaginationRange(currentPage, totalPages, siblingCount);
    const handlePageChange = useCallback((page) => {
        if (page >= 1 && page <= totalPages && page !== currentPage) {
            onPageChange(page);
        }
    }, [totalPages, currentPage, onPageChange]);
    // Don't render if there's only one page
    if (totalPages <= 1) {
        return null;
    }
    const isFirstPage = currentPage === 1;
    const isLastPage = currentPage === totalPages;
    return (jsxs("nav", { ref: ref, role: "navigation", "aria-label": "Pagination Navigation", className: cn(theme.baseStyle, className), ...props, children: [showFirstLast && (jsxs(PaginationItem, { size: size, isDisabled: isFirstPage, onClick: () => handlePageChange(1), "aria-label": "Go to first page", children: [jsx(ChevronDoubleLeftIcon, { className: "w-4 h-4" }), jsx("span", { className: "sr-only", children: "First" })] })), showPrevNext && (jsxs(PaginationItem, { size: size, isDisabled: isFirstPage, onClick: () => handlePageChange(currentPage - 1), "aria-label": `Go to previous page (${currentPage - 1})`, children: [jsx(ChevronLeftIcon, { className: "w-4 h-4" }), jsx("span", { className: "sr-only", children: "Previous" })] })), paginationRange.map((pageNumber, index) => {
                if (pageNumber === "...") {
                    return (jsxs(PaginationItem, { size: size, variant: "ellipsis", isDisabled: true, "aria-label": "More pages", role: "separator", children: [jsx("span", { "aria-hidden": "true", children: "\u2026" }), jsx("span", { className: "sr-only", children: "More pages" })] }, `ellipsis-${index}`));
                }
                const page = pageNumber;
                const isActive = page === currentPage;
                return (jsxs(PaginationItem, { size: size, variant: "page", isActive: isActive, onClick: () => handlePageChange(page), "aria-label": isActive ? `Page ${page}, current page` : `Go to page ${page}`, children: [page, isActive && jsx("span", { className: "sr-only", children: "(current)" })] }, page));
            }), showPrevNext && (jsxs(PaginationItem, { size: size, isDisabled: isLastPage, onClick: () => handlePageChange(currentPage + 1), "aria-label": `Go to next page (${currentPage + 1})`, children: [jsx(ChevronRightIcon, { className: "w-4 h-4" }), jsx("span", { className: "sr-only", children: "Next" })] })), showFirstLast && (jsxs(PaginationItem, { size: size, isDisabled: isLastPage, onClick: () => handlePageChange(totalPages), "aria-label": "Go to last page", children: [jsx(ChevronDoubleRightIcon, { className: "w-4 h-4" }), jsx("span", { className: "sr-only", children: "Last" })] }))] }));
});
Pagination.displayName = "Pagination";

export { Pagination };
