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

const DOTS = "...";

const PAGE_PARAM = "page";
const usePaginationQueryParams = () => {
  const history = useHistory();
  const updatePageInQueryParam = (page) => {
    const params = new URLSearchParams(window.location.search);
    params.set(PAGE_PARAM, String(page));
    history.push(`${window.location.pathname}?${params.toString()}`);
  };
  return { updatePageInQueryParam };
};

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 { updatePageInQueryParam } = usePaginationQueryParams();
    const lastPage = paginationRange[paginationRange.length - 1];
    const isFirstPage = pageNo === 1;
    const isLastPage = pageNo === lastPage;
    if (pageNo === 0 || paginationRange.length < 2) {
      return null;
    }
    const handleNavigate = navigate ?? updatePageInQueryParam;
    return /* @__PURE__ */ jsx(Pagination$1, { ref, className: cn(className), ...otherProps, children: /* @__PURE__ */ jsxs(PaginationContent, { children: [
      /* @__PURE__ */ jsx(PaginationItem, { children: /* @__PURE__ */ jsx(
        PaginationPrevious,
        {
          onClick: () => !isFirstPage && handleNavigate(pageNo - 1),
          "aria-disabled": isFirstPage,
          className: cn(isFirstPage && "pointer-events-none opacity-50"),
          "data-testid": "left-navigate-button"
        }
      ) }),
      paginationRange.map((page, index) => /* @__PURE__ */ jsx(PaginationItem, { children: page === DOTS ? /* @__PURE__ */ jsx(PaginationEllipsis, { "data-testid": "dots" }) : /* @__PURE__ */ jsx(
        PaginationLink,
        {
          isActive: page === pageNo,
          onClick: () => handleNavigate(page),
          children: page
        }
      ) }, `${page}-${index}`)),
      /* @__PURE__ */ jsx(PaginationItem, { children: /* @__PURE__ */ jsx(
        PaginationNext,
        {
          onClick: () => !isLastPage && handleNavigate(pageNo + 1),
          "aria-disabled": isLastPage,
          className: cn(isLastPage && "pointer-events-none opacity-50"),
          "data-testid": "right-navigate-button"
        }
      ) })
    ] }) });
  }
);
Pagination.displayName = "Pagination";

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