import { jsx, jsxs } from "react/jsx-runtime";
import assert from "assert";
import { Box } from "../Box/Box.mjs";
import { useBackground } from "../Box/BackgroundContext.mjs";
import clsx from "clsx";
import { forwardRef } from "react";
import { atoms } from "../../css/atoms/atoms.mjs";
import { useLinkComponent, useBraidTheme } from "../../../../side-effects/lib/components/BraidProvider/BraidProvider.mjs";
import { buildDataAttributes } from "../Accordion/Accordion.mjs";
import { Overlay, Text } from "../Accordion/AccordionItem.mjs";
import { hover, background, current, lightModeCurrentKeyline, darkModeCurrentKeyline } from "./Pagination.css.mjs";
import { IconChevron } from "../icons/IconChevron/IconChevron.mjs";
const Link = forwardRef(
  ({ href, className, data, ...restProps }, ref) => {
    const LinkComponent = useLinkComponent(ref);
    return /* @__PURE__ */ jsx(
      LinkComponent,
      {
        ref,
        href,
        className: clsx(atoms({ reset: "a" }), className),
        ...restProps,
        ...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__ */ jsxs(
    Box,
    {
      component: "span",
      className: hover,
      position: "relative",
      display: "flex",
      alignItems: "center",
      height: "touchable",
      paddingLeft: isNext ? "small" : "xsmall",
      paddingRight: isPrevious ? "small" : "xsmall",
      children: [
        /* @__PURE__ */ jsx(
          Overlay,
          {
            component: "span",
            background: "formAccentSoft",
            borderRadius,
            transition: "fast",
            className: background
          }
        ),
        /* @__PURE__ */ jsx(Box, { component: "span", zIndex: 1, userSelect: "none", children: /* @__PURE__ */ jsxs(Text, { children: [
          isPrevious ? /* @__PURE__ */ jsx(IconChevron, { direction: "left" }) : null,
          /* @__PURE__ */ jsx(
            Box,
            {
              display: isPrevious ? ["none", "inline"] : void 0,
              component: "span",
              marginLeft: isPrevious ? "xsmall" : void 0,
              marginRight: isNext ? "xsmall" : void 0,
              children: label
            }
          ),
          isNext ? /* @__PURE__ */ jsx(IconChevron, { direction: "right" }) : null
        ] }) })
      ]
    }
  );
};
const tabletButtonSpacing = "xxsmall";
const Page = ({ number, current: current$1 }) => {
  const parentBackground = useBackground();
  const isLegacyTheme = useBraidTheme().legacy;
  return /* @__PURE__ */ jsxs(
    Box,
    {
      component: "span",
      display: "flex",
      alignItems: "center",
      justifyContent: "center",
      height: "touchable",
      width: "touchable",
      position: "relative",
      className: hover,
      children: [
        /* @__PURE__ */ jsx(
          Overlay,
          {
            component: "span",
            background: current$1 ? "formAccent" : "formAccentSoft",
            transition: current$1 ? void 0 : "fast",
            borderRadius,
            className: [background, current$1 ? current : void 0]
          }
        ),
        /* @__PURE__ */ jsx(
          Overlay,
          {
            component: "span",
            borderRadius,
            boxShadow: "borderFormAccent",
            className: {
              [lightModeCurrentKeyline]: isLegacyTheme && parentBackground.lightMode !== "surface" && current$1,
              [darkModeCurrentKeyline]: current$1
            }
          }
        ),
        /* @__PURE__ */ jsx(Box, { component: "span", zIndex: 1, userSelect: "none", children: /* @__PURE__ */ jsx(
          Text,
          {
            baseline: false,
            align: "center",
            weight: current$1 ? "medium" : void 0,
            tone: current$1 ? "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(total >= 1, `\`total\` must be at least 1`);
  assert(page >= 1 && page <= total, `\`page\` must be between 1 and ${total}`);
  assert(
    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__ */ jsx(
    Box,
    {
      component: "nav",
      "aria-label": label,
      ...buildDataAttributes({ data, validateRestProps: restProps }),
      children: /* @__PURE__ */ jsxs(Box, { component: "ul", display: "flex", justifyContent: "center", children: [
        /* @__PURE__ */ jsx(
          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__ */ 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__ */ jsx(PageNav, { label: previousLabel, direction: "prev" })
              }
            )
          }
        ),
        pages.map((pageNumber, index) => {
          const current2 = page === pageNumber;
          const isNotLast = pages.length - 1 !== index;
          return /* @__PURE__ */ jsx(
            Box,
            {
              component: "li",
              display: !current2 ? { mobile: "none", tablet: "block" } : void 0,
              paddingRight: pageLimit > 2 && isNotLast ? {
                tablet: tabletButtonSpacing
              } : void 0,
              children: /* @__PURE__ */ jsx(
                Link,
                {
                  ...linkProps({ page: pageNumber, type: "pageNumber" }),
                  "aria-label": pageLabel(pageNumber),
                  "aria-current": current2 ? "page" : void 0,
                  title: pageLabel(pageNumber),
                  children: /* @__PURE__ */ jsx(Page, { number: pageNumber, current: current2 })
                }
              )
            },
            pageNumber
          );
        }),
        /* @__PURE__ */ jsx(
          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__ */ 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__ */ jsx(PageNav, { label: nextLabel, direction: "next" })
              }
            )
          }
        )
      ] })
    }
  );
};
export {
  Link,
  Pagination,
  defaultPageLimit
};
