'use strict';

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var utils = require('./utils-BhM0B89p.js');
var primitives_Pagination = require('./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 React.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 = React.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;
    if (pageNo === 0 || paginationRange.length < 2) {
      return null;
    }
    const handleNavigate = (page) => {
      navigate?.(page);
    };
    return /* @__PURE__ */ jsxRuntime.jsx(primitives_Pagination.Pagination, { ref, className: utils.cn(className), ...otherProps, children: /* @__PURE__ */ jsxRuntime.jsxs(primitives_Pagination.PaginationContent, { children: [
      /* @__PURE__ */ jsxRuntime.jsx(primitives_Pagination.PaginationItem, { children: /* @__PURE__ */ jsxRuntime.jsx(
        primitives_Pagination.PaginationPrevious,
        {
          onClick: () => !isFirstPage && handleNavigate(pageNo - 1),
          "aria-disabled": isFirstPage,
          className: utils.cn(isFirstPage && "pointer-events-none opacity-50")
        }
      ) }),
      paginationRange.map((page, index) => /* @__PURE__ */ jsxRuntime.jsx(primitives_Pagination.PaginationItem, { children: page === DOTS ? /* @__PURE__ */ jsxRuntime.jsx(primitives_Pagination.PaginationEllipsis, {}) : /* @__PURE__ */ jsxRuntime.jsx(
        primitives_Pagination.PaginationLink,
        {
          isActive: page === pageNo,
          onClick: () => handleNavigate(page),
          children: page
        }
      ) }, `${page}-${index}`)),
      /* @__PURE__ */ jsxRuntime.jsx(primitives_Pagination.PaginationItem, { children: /* @__PURE__ */ jsxRuntime.jsx(
        primitives_Pagination.PaginationNext,
        {
          onClick: () => !isLastPage && handleNavigate(pageNo + 1),
          "aria-disabled": isLastPage,
          className: utils.cn(isLastPage && "pointer-events-none opacity-50")
        }
      ) })
    ] }) });
  }
);
Pagination.displayName = "Pagination";

exports.Pagination = Pagination;
//# sourceMappingURL=Pagination-DeWfLAq5.js.map
