"use strict";
const jsxRuntime = require("react/jsx-runtime");
const assert = require("assert");
const styles_lib_components_Box_Box_cjs = require("../Box/Box.cjs");
const styles_lib_components_Box_BackgroundContext_cjs = require("../Box/BackgroundContext.cjs");
const clsx = require("clsx");
const React = require("react");
const styles_lib_css_atoms_atoms_cjs = require("../../css/atoms/atoms.cjs");
const sideEffects_lib_components_BraidProvider_BraidProvider_cjs = require("../../../../side-effects/lib/components/BraidProvider/BraidProvider.cjs");
const styles_lib_components_Accordion_Accordion_cjs = require("../Accordion/Accordion.cjs");
const styles_lib_components_Accordion_AccordionItem_cjs = require("../Accordion/AccordionItem.cjs");
const styles_lib_components_Pagination_Pagination_css_cjs = require("./Pagination.css.cjs");
const styles_lib_components_icons_IconChevron_IconChevron_cjs = require("../icons/IconChevron/IconChevron.cjs");
const _interopDefaultCompat = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const assert__default = /* @__PURE__ */ _interopDefaultCompat(assert);
const clsx__default = /* @__PURE__ */ _interopDefaultCompat(clsx);
const Link = React.forwardRef(
  ({ href, className, data, ...restProps }, ref) => {
    const LinkComponent = sideEffects_lib_components_BraidProvider_BraidProvider_cjs.useLinkComponent(ref);
    return /* @__PURE__ */ jsxRuntime.jsx(
      LinkComponent,
      {
        ref,
        href,
        className: clsx__default.default(styles_lib_css_atoms_atoms_cjs.atoms({ reset: "a" }), className),
        ...restProps,
        ...styles_lib_components_Accordion_Accordion_cjs.buildDataAttributes({ data, validateRestProps: false })
      }
    );
  }
);
Link.displayName = "Link";
const paginate = ({
  page,
  total,
  maxPages
}) => {
  const half = (maxPages - 1) / 2;
  const smallerHalf = Math.floor(half);
  const largerHalf = Math.ceil(half);
  const pageCount = Math.min(maxPages, total);
  let minPage = page - smallerHalf;
  if (page - smallerHalf <= 1) {
    minPage = 1;
  } else if (page + largerHalf >= total) {
    minPage = Math.max(1, total - maxPages + 1);
  }
  return Array.from(Array(pageCount).keys()).map((p) => p + minPage);
};
const borderRadius = "standard";
const PageNav = ({
  label,
  direction
}) => {
  const isPrevious = direction === "prev";
  const isNext = direction === "next";
  return /* @__PURE__ */ jsxRuntime.jsxs(
    styles_lib_components_Box_Box_cjs.Box,
    {
      component: "span",
      className: styles_lib_components_Pagination_Pagination_css_cjs.hover,
      position: "relative",
      display: "flex",
      alignItems: "center",
      height: "touchable",
      paddingLeft: isNext ? "small" : "xsmall",
      paddingRight: isPrevious ? "small" : "xsmall",
      children: [
        /* @__PURE__ */ jsxRuntime.jsx(
          styles_lib_components_Accordion_AccordionItem_cjs.Overlay,
          {
            component: "span",
            background: "formAccentSoft",
            borderRadius,
            transition: "fast",
            className: styles_lib_components_Pagination_Pagination_css_cjs.background
          }
        ),
        /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Box_Box_cjs.Box, { component: "span", zIndex: 1, userSelect: "none", children: /* @__PURE__ */ jsxRuntime.jsxs(styles_lib_components_Accordion_AccordionItem_cjs.Text, { children: [
          isPrevious ? /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_icons_IconChevron_IconChevron_cjs.IconChevron, { direction: "left" }) : null,
          /* @__PURE__ */ jsxRuntime.jsx(
            styles_lib_components_Box_Box_cjs.Box,
            {
              display: isPrevious ? ["none", "inline"] : void 0,
              component: "span",
              marginLeft: isPrevious ? "xsmall" : void 0,
              marginRight: isNext ? "xsmall" : void 0,
              children: label
            }
          ),
          isNext ? /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_icons_IconChevron_IconChevron_cjs.IconChevron, { direction: "right" }) : null
        ] }) })
      ]
    }
  );
};
const tabletButtonSpacing = "xxsmall";
const Page = ({ number, current }) => {
  const parentBackground = styles_lib_components_Box_BackgroundContext_cjs.useBackground();
  const isLegacyTheme = sideEffects_lib_components_BraidProvider_BraidProvider_cjs.useBraidTheme().legacy;
  return /* @__PURE__ */ jsxRuntime.jsxs(
    styles_lib_components_Box_Box_cjs.Box,
    {
      component: "span",
      display: "flex",
      alignItems: "center",
      justifyContent: "center",
      height: "touchable",
      width: "touchable",
      position: "relative",
      className: styles_lib_components_Pagination_Pagination_css_cjs.hover,
      children: [
        /* @__PURE__ */ jsxRuntime.jsx(
          styles_lib_components_Accordion_AccordionItem_cjs.Overlay,
          {
            component: "span",
            background: current ? "formAccent" : "formAccentSoft",
            transition: current ? void 0 : "fast",
            borderRadius,
            className: [styles_lib_components_Pagination_Pagination_css_cjs.background, current ? styles_lib_components_Pagination_Pagination_css_cjs.current : void 0]
          }
        ),
        /* @__PURE__ */ jsxRuntime.jsx(
          styles_lib_components_Accordion_AccordionItem_cjs.Overlay,
          {
            component: "span",
            borderRadius,
            boxShadow: "borderFormAccent",
            className: {
              [styles_lib_components_Pagination_Pagination_css_cjs.lightModeCurrentKeyline]: isLegacyTheme && parentBackground.lightMode !== "surface" && current,
              [styles_lib_components_Pagination_Pagination_css_cjs.darkModeCurrentKeyline]: current
            }
          }
        ),
        /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_Box_Box_cjs.Box, { component: "span", zIndex: 1, userSelect: "none", children: /* @__PURE__ */ jsxRuntime.jsx(
          styles_lib_components_Accordion_AccordionItem_cjs.Text,
          {
            baseline: false,
            align: "center",
            weight: current ? "medium" : void 0,
            tone: current ? "formAccent" : void 0,
            children: number
          }
        ) })
      ]
    }
  );
};
const defaultPageLimit = 7;
const Pagination = ({
  page,
  total,
  linkProps,
  label,
  pageLabel = (p) => `Go to page ${p}`,
  nextLabel = "Next",
  previousLabel = "Previous",
  pageLimit = defaultPageLimit,
  data,
  ...restProps
}) => {
  assert__default.default(total >= 1, `\`total\` must be at least 1`);
  assert__default.default(page >= 1 && page <= total, `\`page\` must be between 1 and ${total}`);
  assert__default.default(
    pageLimit >= 1 && pageLimit <= defaultPageLimit,
    `\`pageLimit\` must be between 1 and ${defaultPageLimit}`
  );
  const pages = paginate({ page, total, maxPages: pageLimit });
  const showPrevious = page > 1;
  const showNext = page < total;
  return /* @__PURE__ */ jsxRuntime.jsx(
    styles_lib_components_Box_Box_cjs.Box,
    {
      component: "nav",
      "aria-label": label,
      ...styles_lib_components_Accordion_Accordion_cjs.buildDataAttributes({ data, validateRestProps: restProps }),
      children: /* @__PURE__ */ jsxRuntime.jsxs(styles_lib_components_Box_Box_cjs.Box, { component: "ul", display: "flex", justifyContent: "center", children: [
        /* @__PURE__ */ jsxRuntime.jsx(
          styles_lib_components_Box_Box_cjs.Box,
          {
            component: "li",
            paddingRight: {
              mobile: "medium",
              tablet: pageLimit > 2 ? tabletButtonSpacing : void 0
            },
            transition: "fast",
            opacity: !showPrevious ? 0 : void 0,
            pointerEvents: !showPrevious ? "none" : void 0,
            children: /* @__PURE__ */ jsxRuntime.jsx(
              Link,
              {
                ...linkProps({ page: page - 1, type: "previous" }),
                rel: "prev",
                "aria-label": previousLabel,
                title: previousLabel,
                "aria-hidden": !showPrevious,
                tabIndex: !showPrevious ? -1 : void 0,
                children: /* @__PURE__ */ jsxRuntime.jsx(PageNav, { label: previousLabel, direction: "prev" })
              }
            )
          }
        ),
        pages.map((pageNumber, index) => {
          const current = page === pageNumber;
          const isNotLast = pages.length - 1 !== index;
          return /* @__PURE__ */ jsxRuntime.jsx(
            styles_lib_components_Box_Box_cjs.Box,
            {
              component: "li",
              display: !current ? { mobile: "none", tablet: "block" } : void 0,
              paddingRight: pageLimit > 2 && isNotLast ? {
                tablet: tabletButtonSpacing
              } : void 0,
              children: /* @__PURE__ */ jsxRuntime.jsx(
                Link,
                {
                  ...linkProps({ page: pageNumber, type: "pageNumber" }),
                  "aria-label": pageLabel(pageNumber),
                  "aria-current": current ? "page" : void 0,
                  title: pageLabel(pageNumber),
                  children: /* @__PURE__ */ jsxRuntime.jsx(Page, { number: pageNumber, current })
                }
              )
            },
            pageNumber
          );
        }),
        /* @__PURE__ */ jsxRuntime.jsx(
          styles_lib_components_Box_Box_cjs.Box,
          {
            component: "li",
            paddingLeft: {
              mobile: "medium",
              tablet: pageLimit > 2 ? tabletButtonSpacing : void 0
            },
            transition: "fast",
            opacity: !showNext ? 0 : void 0,
            pointerEvents: !showNext ? "none" : void 0,
            children: /* @__PURE__ */ jsxRuntime.jsx(
              Link,
              {
                ...linkProps({ page: page + 1, type: "next" }),
                rel: "next",
                "aria-label": nextLabel,
                title: nextLabel,
                "aria-hidden": !showNext,
                tabIndex: !showNext ? -1 : void 0,
                children: /* @__PURE__ */ jsxRuntime.jsx(PageNav, { label: nextLabel, direction: "next" })
              }
            )
          }
        )
      ] })
    }
  );
};
exports.Link = Link;
exports.Pagination = Pagination;
exports.defaultPageLimit = defaultPageLimit;
