import { jsxs as x, jsx as r } from "react/jsx-runtime";
import { useState as F, useCallback as b, useMemo as O } from "react";
import { ChevronLeft as R, ChevronRight as y } from "@commercetools/nimbus-icons";
import { n as m, u as D } from "./normalize-messages-BN9BAFXe.es.js";
import { $ as G } from "./context-BfHy4NgG.es.js";
import { S as L } from "./stack-CoNreM7B.es.js";
import { F as v } from "./flex-BqxAMF-7.es.js";
import { S as $ } from "./select-DiSjgVZP.es.js";
import { T as M } from "./text-iTgAZrNO.es.js";
import { I as _ } from "./icon-button-DQaOMIAe.es.js";
import { N as j } from "./number-input-BAaT6aky.es.js";
function B({
  totalItems: e,
  currentPage: u,
  pageSize: n = 20,
  onPageChange: s,
  onPageSizeChange: f
}) {
  const [N, c] = F(1), [z, I] = F(n), i = u !== void 0, C = i ? u : N, g = n ?? z, a = Math.max(1, Math.ceil(e / g)), t = Math.max(1, Math.min(C, a)), h = Math.min((t - 1) * g + 1, e), o = Math.min(t * g, e), l = t > 1, d = t < a, P = b(
    (p) => {
      const T = Math.max(1, Math.min(p, a));
      i || c(T), s?.(T);
    },
    [a, i, s]
  ), A = b(() => {
    l && P(t - 1);
  }, [l, t, P]), k = b(() => {
    d && P(t + 1);
  }, [d, t, P]), w = b(
    (p) => {
      const T = Math.ceil(e / p), S = Math.min(t, T);
      I(p), !i && S !== t && c(S), f?.(p), S !== t && s?.(S);
    },
    [e, t, i, s, f]
  );
  return O(
    () => ({
      totalItems: e,
      currentPage: t,
      pageSize: g,
      totalPages: a,
      startItem: h,
      endItem: o,
      hasPreviousPage: l,
      hasNextPage: d,
      goToPage: P,
      goToPreviousPage: A,
      goToNextPage: k,
      setPageSize: w
    }),
    [
      e,
      t,
      g,
      a,
      h,
      o,
      l,
      d,
      P,
      A,
      k,
      w
    ]
  );
}
const V = {
  currentPage: "Current page",
  goToNextPage: "Go to next page",
  goToPreviousPage: "Go to previous page",
  itemsPerPage: "Items per page",
  itemsPerPageText: "items per page",
  ofTotalPages: (e) => `of ${e.totalPages}`,
  page: "Page",
  pagination: "Pagination"
}, W = {
  currentPage: "Aktuelle Seite",
  goToNextPage: "Weiter zur nächsten Seite",
  goToPreviousPage: "Zur vorherigen Seite",
  itemsPerPage: "Artikel pro Seite",
  itemsPerPageText: "Artikel pro Seite",
  ofTotalPages: (e) => `von ${e.totalPages}`,
  page: "Seite",
  pagination: "Seitennummerierung"
}, K = {
  currentPage: "Página actual",
  goToNextPage: "Ir a la siguiente página",
  goToPreviousPage: "Ir a la página anterior",
  itemsPerPage: "Artículos por página",
  itemsPerPageText: "artículos por página",
  ofTotalPages: (e) => `de ${e.totalPages}`,
  page: "página",
  pagination: "paginación"
}, Z = {
  currentPage: "Page actuelle",
  goToNextPage: "Aller à la page suivante",
  goToPreviousPage: "Aller à la page précédente",
  itemsPerPage: "Articles par page",
  itemsPerPageText: "Articles par page",
  ofTotalPages: (e) => `de ${e.totalPages}`,
  page: "Page",
  pagination: "Pagination"
}, q = {
  currentPage: "Página atual",
  goToNextPage: "Ir para a próxima página",
  goToPreviousPage: "Ir para a página anterior",
  itemsPerPage: "Itens por página",
  itemsPerPageText: "Itens por página",
  ofTotalPages: (e) => `de ${e.totalPages}`,
  page: "Página",
  pagination: "Paginação"
}, E = {
  en: /* @__PURE__ */ m(V),
  de: /* @__PURE__ */ m(W),
  es: /* @__PURE__ */ m(K),
  "fr-FR": /* @__PURE__ */ m(Z),
  "pt-BR": /* @__PURE__ */ m(q)
}, H = (e) => {
  const { locale: u } = G(), n = D(E), {
    totalItems: s,
    currentPage: f,
    pageSize: N,
    pageSizeOptions: c = [10, 20, 50, 100],
    onPageChange: z,
    onPageSizeChange: I,
    "aria-label": i,
    enablePageInput: C = !0,
    enablePageSizeSelector: g = !0
  } = e, a = B({
    totalItems: s,
    currentPage: f,
    pageSize: N,
    onPageChange: z,
    onPageSizeChange: I
  }), t = O(
    () => c.map((o) => ({
      id: o.toString(),
      name: `${o}`
    })),
    [c]
  ), h = (o) => {
    if (o) {
      const l = parseInt(o.toString(), 10);
      a.setPageSize(l);
    }
  };
  return /* @__PURE__ */ x(L, { gap: "400", direction: { base: "column", sm: "row" }, align: "center", children: [
    g && /* @__PURE__ */ x(v, { align: "center", gap: "200", children: [
      /* @__PURE__ */ r(
        $.Root,
        {
          isClearable: !1,
          selectedKey: a.pageSize.toString(),
          onSelectionChange: h,
          "aria-label": n.format("itemsPerPage"),
          children: /* @__PURE__ */ r($.Options, { children: t.map((o) => /* @__PURE__ */ r($.Option, { id: o.id, children: o.name }, o.id)) })
        }
      ),
      /* @__PURE__ */ r(M, { color: "neutral.12", children: n.format("itemsPerPageText") })
    ] }),
    /* @__PURE__ */ r(v, { flexGrow: "1" }),
    /* @__PURE__ */ x(
      v,
      {
        align: "center",
        gap: "200",
        role: "navigation",
        "aria-label": i ?? n.format("pagination"),
        children: [
          /* @__PURE__ */ r(
            _,
            {
              onClick: a.goToPreviousPage,
              isDisabled: !a.hasPreviousPage,
              variant: "ghost",
              colorPalette: "primary",
              "aria-label": n.format("goToPreviousPage"),
              children: /* @__PURE__ */ r(R, {})
            }
          ),
          /* @__PURE__ */ x(v, { align: "center", gap: "200", children: [
            /* @__PURE__ */ r(M, { color: "neutral.12", children: n.format("page") }),
            C ? /* @__PURE__ */ r(
              j,
              {
                value: a.currentPage,
                onChange: (o) => a.goToPage(o || 1),
                minValue: 1,
                maxValue: a.totalPages,
                step: 1,
                isDisabled: !1,
                width: "9ch",
                "aria-label": n.format("currentPage"),
                "aria-current": "page"
              }
            ) : /* @__PURE__ */ r(M, { fontWeight: "semibold", color: "neutral.12", "aria-current": "page", children: a.currentPage }),
            /* @__PURE__ */ r(M, { color: "neutral.12", children: (() => {
              const o = new Intl.NumberFormat(u).format(
                a.totalPages
              );
              return n.format("ofTotalPages", {
                totalPages: o
              });
            })() })
          ] }),
          /* @__PURE__ */ r(
            _,
            {
              onClick: a.goToNextPage,
              isDisabled: !a.hasNextPage,
              variant: "ghost",
              colorPalette: "primary",
              "aria-label": n.format("goToNextPage"),
              children: /* @__PURE__ */ r(y, {})
            }
          )
        ]
      }
    )
  ] });
};
H.displayName = "Pagination";
export {
  H as P
};
//# sourceMappingURL=pagination-CjC5FnAe.es.js.map
