import { jsx as a, jsxs as X, Fragment as J } from "react/jsx-runtime";
import { forwardRef as K, useState as S, useEffect as B } from "react";
import { a as L } from "./spinner-DRQN6YdK.mjs";
import { T as Y } from "./text-field-PKAaXO1N.mjs";
import { c as j } from "./bind-CU-R61T-.mjs";
import { c as G } from "./button-DBX_tvfJ.mjs";
import { b as k } from "./icon-DNHpBTHs-JfI8WyiR.mjs";
const W = {
  "purpur-quantity-selector": "_purpur-quantity-selector_pun3b_1",
  "purpur-quantity-selector--full-width": "_purpur-quantity-selector--full-width_pun3b_25",
  "purpur-quantity-selector--loading": "_purpur-quantity-selector--loading_pun3b_31",
  "purpur-quantity-selector--no-gap": "_purpur-quantity-selector--no-gap_pun3b_34",
  "purpur-quantity-selector--no-gap-disabled-left": "_purpur-quantity-selector--no-gap-disabled-left_pun3b_39",
  "purpur-quantity-selector--no-gap-disabled-right": "_purpur-quantity-selector--no-gap-disabled-right_pun3b_43",
  "purpur-quantity-selector--no-gap-disabled": "_purpur-quantity-selector--no-gap-disabled_pun3b_39",
  "purpur-quantity-selector__spinner": "_purpur-quantity-selector__spinner_pun3b_50",
  "purpur-quantity-selector__spinner--full-width": "_purpur-quantity-selector__spinner--full-width_pun3b_57",
  "purpur-quantity-selector__spinner--separated": "_purpur-quantity-selector__spinner--separated_pun3b_61"
}, T = {
  name: "minus",
  svg: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path fill-rule="evenodd" d="M4.1 12a.9.9 0 0 1 .9-.9h14a.9.9 0 1 1 0 1.8H5a.9.9 0 0 1-.9-.9Z" clip-rule="evenodd"/></svg>',
  keywords: ["minus"],
  category: "utility"
}, D = (s) => /* @__PURE__ */ a(k, { ...s, svg: T });
D.displayName = "IconMinus";
const tt = {
  name: "plus",
  svg: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path fill-rule="evenodd" d="M12 4.1a.9.9 0 0 1 .9.9v6.1H19a.9.9 0 1 1 0 1.8h-6.1V19a.9.9 0 0 1-1.8 0v-6.1H5a.9.9 0 0 1 0-1.8h6.1V5a.9.9 0 0 1 .9-.9Z" clip-rule="evenodd"/></svg>',
  keywords: ["plus"],
  category: "utility"
}, F = (s) => /* @__PURE__ */ a(k, { ...s, svg: tt });
F.displayName = "IconPlus";
const et = {
  "purpur-quantity-selector-button": "_purpur-quantity-selector-button_qjlbs_1",
  "purpur-quantity-selector-button--gap-left": "_purpur-quantity-selector-button--gap-left_qjlbs_4",
  "purpur-quantity-selector-button--gap-right": "_purpur-quantity-selector-button--gap-right_qjlbs_7",
  "purpur-quantity-selector-button--no-gap-right": "_purpur-quantity-selector-button--no-gap-right_qjlbs_10",
  "purpur-quantity-selector-button--no-gap-left": "_purpur-quantity-selector-button--no-gap-left_qjlbs_13"
}, rt = j.bind(et), _ = "purpur-quantity-selector-button", nt = ({
  ["data-testid"]: s,
  disabled: y,
  hasGap: e,
  variant: q,
  buttonType: r,
  onClick: o
}) => {
  const f = rt(_, {
    [`${_}--gap-left`]: e && r === "decrement",
    [`${_}--gap-right`]: e && r === "increment",
    [`${_}--no-gap-left`]: !e && r === "decrement",
    [`${_}--no-gap-right`]: !e && r === "increment"
  });
  return /* @__PURE__ */ a(
    G,
    {
      "data-testid": s,
      className: f,
      size: "sm",
      type: "submit",
      variant: q,
      onClick: () => o(r),
      iconOnly: !0,
      disabled: y,
      tabIndex: -1,
      children: r === "increment" ? /* @__PURE__ */ a(F, { size: "xs" }) : /* @__PURE__ */ a(D, { size: "xs" })
    }
  );
}, V = j.bind(W), dt = ["attached", "separated"], _t = ["primary", "secondary"], n = "purpur-quantity-selector", st = K(
  ({
    ["data-testid"]: s,
    className: y,
    variant: e,
    buttonVariant: q,
    onChange: r,
    value: o,
    defaultValue: f,
    fullWidth: h,
    disabled: M,
    loading: v,
    minValue: z,
    maxValue: E,
    ariaLabelDecrement: Q,
    ariaLabelIncrement: R,
    ...H
  }, I) => {
    const [O, $] = S(f ?? 0), [w, x] = S(O), u = o !== void 0, l = u ? o : w, N = (t) => s ? `${s}-${t}` : void 0, i = M || v, g = Math.max(z ?? 0, 0), m = E ?? Number.MAX_SAFE_INTEGER, c = l === m, d = l === g;
    B(() => {
      u && $(o);
    }, [o, u]), B(() => {
      if (!u) {
        const t = setTimeout(() => {
          $(w);
        }, 300);
        return () => clearTimeout(t);
      }
    }, [w, u, r]);
    const A = V(y, n, {
      [`${n}--no-gap`]: e === "attached" && !i && !d && !c,
      [`${n}--no-gap-disabled-left`]: e === "attached" && !i && !c && d,
      [`${n}--no-gap-disabled-right`]: e === "attached" && !i && !d && c,
      [`${n}--no-gap-disabled`]: e === "attached" && (i || c && d),
      [`${n}--full-width`]: h,
      [`${n}--loading`]: v
    }), Z = (t) => {
      if (!/^[0-9]*$/.test(t.target.value))
        return;
      const b = +t.target.value;
      b < g || b > m || (u || x(b), r(b));
    }, P = (t) => {
      const p = t === "increment" ? l + 1 : l - 1;
      p < 0 || (u || x(p), r(p));
    }, C = (t) => {
      const p = t === "increment" && c || t === "decrement" && d;
      return /* @__PURE__ */ a(
        nt,
        {
          "data-testid": N("button-" + t),
          variant: q,
          buttonType: t,
          hasGap: e === "separated",
          onClick: P,
          disabled: i || p,
          "aria-label": t === "increment" ? R : Q
        }
      );
    }, U = /* @__PURE__ */ X(J, { children: [
      v && /* @__PURE__ */ a(
        "div",
        {
          className: V(`${n}__spinner`, {
            [`${n}__spinner--full-width`]: h,
            [`${n}__spinner--separated`]: !h && e === "separated"
          }),
          children: /* @__PURE__ */ a(
            L,
            {
              disabled: i,
              size: "xs",
              "data-testid": N("spinner")
            },
            "spinner"
          )
        }
      ),
      C("decrement")
    ] });
    return /* @__PURE__ */ a(
      Y,
      {
        value: l,
        type: "number",
        ref: I,
        className: A,
        "data-testid": s,
        onChange: Z,
        beforeField: U,
        afterField: C("increment"),
        disabled: i,
        min: g,
        max: m,
        role: "spinbutton",
        "aria-valuenow": l,
        "aria-valuemax": m,
        "aria-valuemin": g,
        ...H
      }
    );
  }
);
st.displayName = "QuantitySelector";
export {
  st as Q,
  _t as a,
  dt as q
};
//# sourceMappingURL=quantity-selector-_VcZYLjR.mjs.map
