import { jsx, jsxs } from "react/jsx-runtime";
import { forwardRef, useMemo } from "react";
import { S as StChevronLeft } from "../../StChevronLeft-D60BtMQy.js";
import { S as StChevronRight } from "../../StChevronRight-BuahImu8.js";
import { cn } from "../utils/cn.js";
const ELLIPSIS = "…";
function generatePageNumbers(current, total, maxButtons) {
  if (total <= maxButtons) {
    return Array.from({ length: total }, (_, i) => i + 1);
  }
  const sideButtons = Math.floor((maxButtons - 3) / 2);
  const pages = [];
  pages.push(1);
  const rangeStart = Math.max(2, current - sideButtons);
  const rangeEnd = Math.min(total - 1, current + sideButtons);
  if (rangeStart > 2) {
    pages.push(ELLIPSIS);
  }
  for (let i = rangeStart; i <= rangeEnd; i++) {
    pages.push(i);
  }
  if (rangeEnd < total - 1) {
    pages.push(ELLIPSIS);
  }
  if (total > 1) {
    pages.push(total);
  }
  return pages;
}
const PaginationButton = forwardRef(({ className, isActive, children, ...props }, ref) => /* @__PURE__ */ jsx(
  "button",
  {
    ref,
    className: cn(
      "inline-flex items-center justify-center min-w-[36px] min-h-[36px] h-9 px-3 rounded-lg font-medium transition-colors cursor-pointer",
      "focus:outline-none focus-visible:ring-2 focus-visible:ring-action focus-visible:ring-offset-2",
      "disabled:opacity-30 disabled:pointer-events-none disabled:cursor-not-allowed",
      isActive ? "bg-action/10 text-action hover:bg-action/20" : "hover:bg-muted/50 text-primary",
      className
    ),
    ...props,
    children
  }
));
PaginationButton.displayName = "PaginationButton";
const Pagination = forwardRef(
  ({
    className,
    currentPage,
    totalPages,
    onPageChange,
    variant = "default",
    showPageInfo = false,
    maxPageButtons = 7,
    ...props
  }, ref) => {
    const safePage = Math.max(1, Math.min(currentPage, totalPages || 1));
    const pageNumbers = useMemo(
      () => generatePageNumbers(safePage, totalPages, maxPageButtons),
      [safePage, totalPages, maxPageButtons]
    );
    const handlePrevious = () => {
      if (safePage > 1) {
        onPageChange(safePage - 1);
      }
    };
    const handleNext = () => {
      if (safePage < totalPages) {
        onPageChange(safePage + 1);
      }
    };
    const handlePageClick = (page) => {
      if (page !== safePage) {
        onPageChange(page);
      }
    };
    if (totalPages === 0) {
      return /* @__PURE__ */ jsxs(
        "nav",
        {
          ref,
          "aria-label": "Pagination",
          className: cn("flex items-center justify-center gap-1", className),
          ...props,
          children: [
            /* @__PURE__ */ jsx(PaginationButton, { disabled: true, "aria-label": "Go to previous page", children: /* @__PURE__ */ jsx(StChevronLeft, { width: 16, height: 16 }) }),
            /* @__PURE__ */ jsx(PaginationButton, { disabled: true, "aria-label": "Go to next page", children: /* @__PURE__ */ jsx(StChevronRight, { width: 16, height: 16 }) })
          ]
        }
      );
    }
    if (variant === "simple") {
      return /* @__PURE__ */ jsxs(
        "nav",
        {
          ref,
          "aria-label": "Pagination",
          className: cn("flex items-center justify-center gap-2", className),
          ...props,
          children: [
            /* @__PURE__ */ jsx(
              PaginationButton,
              {
                onClick: handlePrevious,
                disabled: safePage === 1,
                "aria-label": "Go to previous page",
                children: /* @__PURE__ */ jsx(StChevronLeft, { width: 16, height: 16 })
              }
            ),
            showPageInfo && /* @__PURE__ */ jsxs("span", { className: "text-secondary px-2", children: [
              "Page ",
              safePage,
              " of ",
              totalPages
            ] }),
            /* @__PURE__ */ jsx(
              PaginationButton,
              {
                onClick: handleNext,
                disabled: safePage === totalPages,
                "aria-label": "Go to next page",
                children: /* @__PURE__ */ jsx(StChevronRight, { width: 16, height: 16 })
              }
            )
          ]
        }
      );
    }
    return /* @__PURE__ */ jsxs(
      "nav",
      {
        ref,
        "aria-label": "Pagination",
        className: cn("flex items-center justify-center gap-1", className),
        ...props,
        children: [
          /* @__PURE__ */ jsx(
            PaginationButton,
            {
              onClick: handlePrevious,
              disabled: safePage === 1,
              "aria-label": "Go to previous page",
              children: /* @__PURE__ */ jsx(StChevronLeft, { width: 16, height: 16 })
            }
          ),
          /* @__PURE__ */ jsx("div", { className: "flex items-center gap-1", children: pageNumbers.map((page, index) => {
            if (page === ELLIPSIS) {
              const prevPage = index > 0 ? pageNumbers[index - 1] : null;
              const nextPage = index < pageNumbers.length - 1 ? pageNumbers[index + 1] : null;
              const shouldHideOnMobile = (() => {
                if (totalPages <= 4) return true;
                if (typeof prevPage === "number" && typeof nextPage === "number") {
                  const isPrevHidden = prevPage !== 1 && prevPage !== totalPages && prevPage !== safePage && prevPage !== safePage - 1;
                  const isNextHidden = nextPage !== 1 && nextPage !== totalPages && nextPage !== safePage && nextPage !== safePage - 1;
                  if (isPrevHidden && isNextHidden) return true;
                }
                return false;
              })();
              return /* @__PURE__ */ jsx(
                "span",
                {
                  className: cn(
                    "inline-flex items-center justify-center min-w-[36px] min-h-[36px] h-9 px-3 text-secondary",
                    shouldHideOnMobile && "hidden sm:inline-flex"
                  ),
                  "aria-hidden": "true",
                  children: ELLIPSIS
                },
                `ellipsis-${index}`
              );
            }
            const pageNumber = page;
            const isCurrentPage = pageNumber === safePage;
            const isMobileVisible = (() => {
              if (pageNumber === 1 || pageNumber === totalPages) return true;
              if (isCurrentPage) return true;
              if (totalPages <= 4) return true;
              if (totalPages > 4) {
                if (safePage <= 2 && pageNumber === 2) return true;
                if (safePage >= totalPages - 1 && pageNumber === totalPages - 1)
                  return true;
                if (safePage > 2 && safePage < totalPages - 1) {
                  if (pageNumber === safePage - 1) return true;
                }
              }
              return false;
            })();
            return /* @__PURE__ */ jsx(
              PaginationButton,
              {
                onClick: () => handlePageClick(pageNumber),
                isActive: isCurrentPage,
                "aria-label": isCurrentPage ? `Page ${pageNumber}, current page` : `Go to page ${pageNumber}`,
                "aria-current": isCurrentPage ? "page" : void 0,
                className: cn(!isMobileVisible && "hidden sm:inline-flex"),
                children: pageNumber
              },
              pageNumber
            );
          }) }),
          /* @__PURE__ */ jsx(
            PaginationButton,
            {
              onClick: handleNext,
              disabled: safePage === totalPages,
              "aria-label": "Go to next page",
              children: /* @__PURE__ */ jsx(StChevronRight, { width: 16, height: 16 })
            }
          )
        ]
      }
    );
  }
);
Pagination.displayName = "Pagination";
export {
  Pagination
};
