"use client";
import { jsx as a, jsxs as p, Fragment as E } from "react/jsx-runtime";
import { useState as T, useMemo as N, useEffect as B, createContext as D, useContext as R } from "react";
import { I as m } from "./input-group-bflx1m6ki55e99vf.js";
import { CaretDoubleLeftIcon as F, CaretLeftIcon as V, CaretRightIcon as U, CaretDoubleRightIcon as j } from "@phosphor-icons/react";
import { S as P } from "./select-p8adn2tnpom25hpl.js";
import { c as d } from "./vendor-styling-ofvnbcuht98q88oo.js";
const G = [25, 50, 100, 250], y = (n, l, o) => Math.min(Math.max(n, l), o), K = {
  navigation: "Pagination",
  firstPage: "First page",
  previousPage: "Previous page",
  nextPage: "Next page",
  lastPage: "Last page",
  pageNumber: "Page number",
  pageSize: "Page size"
}, x = {
  controls: "full"
}, v = D(null);
function C() {
  const n = R(v);
  if (!n)
    throw new Error(
      "Pagination compound components must be used within a Pagination component"
    );
  return n;
}
function k({ children: n, className: l }) {
  const { page: o, perPage: e, totalCount: i, pageShowingRange: r } = C(), s = n ? n({ page: o, perPage: e, totalCount: i, pageShowingRange: r }) : i && i > 0 ? /* @__PURE__ */ p(E, { children: [
    "Showing ",
    /* @__PURE__ */ a("span", { className: "tabular-nums", children: r }),
    " of",
    " ",
    /* @__PURE__ */ a("span", { className: "tabular-nums", children: i })
  ] }) : null;
  return /* @__PURE__ */ a(
    "div",
    {
      "data-slot": "pagination-info",
      className: d("text-sm text-kumo-subtle", l),
      children: s
    }
  );
}
k.displayName = "Pagination.Info";
function A({
  value: n,
  onChange: l,
  options: o = G,
  label: e = "Per page:",
  className: i
}) {
  const { labels: r } = C();
  return /* @__PURE__ */ p(
    "div",
    {
      "data-slot": "pagination-page-size",
      className: d("flex items-center gap-2", i),
      children: [
        e && /* @__PURE__ */ a("span", { className: "text-sm text-kumo-subtle", children: e }),
        /* @__PURE__ */ a(
          P,
          {
            "aria-label": r.pageSize,
            value: n,
            onValueChange: (s) => l(s),
            children: o.map((s) => /* @__PURE__ */ a(P.Option, { value: s, children: s }, s))
          }
        )
      ]
    }
  );
}
A.displayName = "Pagination.PageSize";
function S({
  controls: n = x.controls,
  pageSelector: l = "input",
  className: o
}) {
  const { page: e, maxPage: i, setPage: r, editingPage: s, setEditingPage: c, labels: g } = C();
  return /* @__PURE__ */ a(
    "div",
    {
      "data-slot": "pagination-controls",
      className: d("grow flex flex-col items-end", o),
      children: /* @__PURE__ */ a("nav", { "aria-label": g.navigation, children: /* @__PURE__ */ p(m, { children: [
        n === "full" && /* @__PURE__ */ a(
          m.Button,
          {
            variant: "secondary",
            "aria-label": g.firstPage,
            disabled: e <= 1,
            onClick: () => {
              r(1), c(1);
            },
            children: /* @__PURE__ */ a(F, { size: 16 })
          }
        ),
        /* @__PURE__ */ a(
          m.Button,
          {
            variant: "secondary",
            "aria-label": g.previousPage,
            disabled: e <= 1,
            onClick: () => {
              const t = Math.max(e - 1, 1);
              r(t), c(t);
            },
            children: /* @__PURE__ */ a(V, { size: 16 })
          }
        ),
        n === "full" && (l === "dropdown" ? /* @__PURE__ */ a(
          P,
          {
            "aria-label": g.pageNumber,
            className: "rounded-none ring-kumo-hairline",
            value: e,
            onValueChange: (t) => {
              const u = t;
              r(u), c(u);
            },
            children: Array.from({ length: i }, (t, u) => u + 1).map((t) => /* @__PURE__ */ a(P.Option, { value: t, children: t }, t))
          }
        ) : /* @__PURE__ */ a(
          m.Input,
          {
            style: { width: 50 },
            className: "text-center",
            "aria-label": g.pageNumber,
            value: s,
            onValueChange: (t) => {
              c(Number(t));
            },
            onBlur: () => {
              const t = y(s, 1, i);
              r(t), c(t);
            },
            onKeyDown: (t) => {
              if (t.key === "Enter") {
                const u = y(s, 1, i);
                r(u), c(u);
              }
            },
            autoComplete: "off",
            "data-1p-ignore": !0,
            "data-lpignore": "true",
            "data-form-type": "other"
          }
        )),
        /* @__PURE__ */ a(
          m.Button,
          {
            variant: "secondary",
            "aria-label": g.nextPage,
            disabled: e === i,
            onClick: () => {
              const t = Math.min(e + 1, i);
              r(t), c(t);
            },
            children: /* @__PURE__ */ a(U, { size: 16 })
          }
        ),
        n === "full" && /* @__PURE__ */ a(
          m.Button,
          {
            variant: "secondary",
            "aria-label": g.lastPage,
            disabled: e === i,
            onClick: () => {
              r(i), c(i);
            },
            children: /* @__PURE__ */ a(j, { size: 16 })
          }
        )
      ] }) })
    }
  );
}
S.displayName = "Pagination.Controls";
function z({ className: n }) {
  return /* @__PURE__ */ a(
    "div",
    {
      "data-slot": "pagination-separator",
      className: d("mx-2 h-6 border-l border-kumo-hairline", n)
    }
  );
}
z.displayName = "Pagination.Separator";
function L(n) {
  const {
    page: l = 1,
    perPage: o,
    totalCount: e,
    setPage: i,
    children: r,
    className: s,
    labels: c
  } = n, g = "text" in n ? n.text : void 0, t = "controls" in n ? n.controls ?? x.controls : x.controls, [u, w] = T(1), M = N(
    () => ({ ...K, ...c }),
    [c]
  );
  B(() => {
    w(l);
  }, [l]);
  const f = N(() => {
    let h = l * (o ?? 1) - (o ?? 0) + 1, b = Math.min(l * (o ?? 0), e ?? 0);
    return Number.isNaN(h) && (h = 0), Number.isNaN(b) && (b = 0), `${h}-${b}`;
  }, [l, o, e]), _ = N(() => Math.ceil((e ?? 1) / (o ?? 1)), [e, o]), I = {
    page: l,
    perPage: o,
    totalCount: e,
    maxPage: _,
    pageShowingRange: f,
    setPage: i,
    editingPage: u,
    setEditingPage: w,
    labels: M
  };
  if (r)
    return /* @__PURE__ */ a(v.Provider, { value: I, children: /* @__PURE__ */ a(
      "div",
      {
        "data-slot": "pagination",
        className: d("flex items-center gap-2 w-full", s),
        children: r
      }
    ) });
  const O = () => g ? g({ page: l, perPage: o, totalCount: e, pageShowingRange: f }) : e && e > 0 ? /* @__PURE__ */ p(E, { children: [
    "Showing ",
    /* @__PURE__ */ a("span", { className: "tabular-nums", children: f }),
    " of",
    " ",
    /* @__PURE__ */ a("span", { className: "tabular-nums", children: e })
  ] }) : null;
  return /* @__PURE__ */ a(v.Provider, { value: I, children: /* @__PURE__ */ p(
    "div",
    {
      "data-slot": "pagination",
      className: d("flex items-center gap-2 w-full", s),
      children: [
        /* @__PURE__ */ a(
          "div",
          {
            "aria-live": "polite",
            "aria-atomic": "true",
            "data-slot": "pagination-info",
            className: "grow text-sm text-kumo-subtle",
            children: O()
          }
        ),
        /* @__PURE__ */ a(S, { controls: t })
      ]
    }
  ) });
}
L.displayName = "Pagination";
const W = Object.assign(L, {
  Info: k,
  PageSize: A,
  Controls: S,
  Separator: z
});
export {
  W as P
};
//# sourceMappingURL=pagination-j33ntwqmm9qskfgv.js.map
