import { j as t } from "../../_virtual/jsx-runtime.js";
import { forwardRef as R, useState as z, useEffect as D } from "react";
import N from "../../icons/AlertTriangle.js";
import T from "../../icons/Minus.js";
import W from "../../icons/Plus.js";
import { IconButton as y } from "../IconButton/index.js";
import { getId as S, noop as O, isDifferentFromUndefinedOrNull as $ } from "../../utils/index.js";
import { Wrapper as P, FocusBorder as U, Border as q, NumberField as G } from "./styles.js";
import { LabelWrapper as J, Label as K, Text as Q, SupportingText as X, ErrorMessage as Y, AssistMessage as Z } from "../Field/styles.js";
function _({
  step: s = 1,
  min: o,
  max: d,
  value: r,
  id: x,
  label: n,
  optionalText: A,
  name: f,
  errorText: g,
  assistText: p,
  onChange: E = O,
  touched: m,
  disabled: i = !1,
  required: B = !0,
  hasError: u = !1,
  fullWidth: F = !1,
  stepperVariant: l = "default",
  componentHeight: b = "medium",
  iconButtonVariant: k = "ghost",
  ...a
}, I) {
  const [c, C] = z(!1), h = S(a["data-testid"]), j = S(a["data-testid"]), w = (e) => {
    E && E(e);
  }, L = () => {
    let e;
    $(o) ? r > o ? r - s < o ? e = o : e = r - s : e = r : e = r - s, e !== r && w(e);
  }, M = () => {
    let e;
    $(d) ? r < d ? r + s > d ? e = d : e = r + s : e = r : e = r + s, e !== r && w(e);
  };
  return D(() => {
    C(typeof m > "u" ? u : u && m);
  }, [u, m]), /* @__PURE__ */ t.jsxs(
    P,
    {
      fullWidth: F,
      "data-testid": `${a["data-testid"]}-stepper`,
      children: [
        /* @__PURE__ */ t.jsxs(J, { disabled: i, hasLabel: !!(n != null && n.length), children: [
          n && /* @__PURE__ */ t.jsx(K, { htmlFor: x || `input-${f}`, disabled: i, children: n }),
          !B && /* @__PURE__ */ t.jsx(Q, { children: A })
        ] }),
        /* @__PURE__ */ t.jsx(U, { children: /* @__PURE__ */ t.jsxs(
          q,
          {
            hasError: c,
            disabled: i,
            stepperVariant: l,
            children: [
              /* @__PURE__ */ t.jsx(
                y,
                {
                  onClick: L,
                  variant: k,
                  size: b,
                  icon: /* @__PURE__ */ t.jsx(T, {}),
                  disabled: i || r - s < o,
                  "aria-label": `Remove ${s}`,
                  "aria-labelledby": "step-decrement label-text",
                  "data-testid": h("stepper-minus-button"),
                  "data-trackid": j("stepper-minus-button")
                }
              ),
              /* @__PURE__ */ t.jsx(
                G,
                {
                  buttonsDisabled: i,
                  ref: I,
                  id: x || f && `input-${f}`,
                  type: "number",
                  value: r,
                  min: o,
                  max: d,
                  name: f,
                  disabled: !0,
                  componentHeight: b,
                  stepperVariant: l,
                  hasError: c,
                  "aria-label": a["aria-label"] || n,
                  "aria-live": "polite",
                  "aria-labelledby": "label-number",
                  "data-testid": h("stepper-number-field"),
                  "data-trackid": j("stepper-number-field")
                }
              ),
              /* @__PURE__ */ t.jsx(
                y,
                {
                  onClick: M,
                  variant: k,
                  size: b,
                  icon: /* @__PURE__ */ t.jsx(W, {}),
                  disabled: i || r + s > d,
                  "aria-label": `Add ${s}`,
                  "aria-labelledby": "step-increment label-text",
                  "data-testid": h("stepper-plus-button"),
                  "data-trackid": j("stepper-plus-button")
                }
              )
            ]
          }
        ) }),
        /* @__PURE__ */ t.jsxs(X, { children: [
          !!g && c && !i && (l == null ? void 0 : l.includes("input")) && /* @__PURE__ */ t.jsxs(Y, { children: [
            /* @__PURE__ */ t.jsx(N, { width: 20, height: 20 }),
            g
          ] }),
          !c && (typeof p == "string" ? /* @__PURE__ */ t.jsx(Z, { children: p }) : p)
        ] })
      ]
    }
  );
}
const de = R(_);
export {
  de as Stepper
};
