import { jsx as l, jsxs as w } from "react/jsx-runtime";
import * as B from "react";
import d, { isValidElement as z, useId as L, Children as I, cloneElement as U, useEffect as q } from "react";
import { a as J } from "./heading-gqtQNo5n.mjs";
import { c as K } from "./index-DOfn3ixy.mjs";
import { u as y, P as b, c as Q } from "./index-DCcK_d4W.mjs";
import { c as $, I as W, R as X } from "./index-IpWiejWQ.mjs";
import { u as Y } from "./index-ByaXH_ih.mjs";
import { c as x } from "./bind-CU-R61T-.mjs";
import { a as Z } from "./checkmark.es-DdgIZN2R.mjs";
var R = "Toggle", V = B.forwardRef((e, i) => {
  const { pressed: n, defaultPressed: r, onPressedChange: t, ...s } = e, [o, a] = y({
    prop: n,
    onChange: t,
    defaultProp: r ?? !1,
    caller: R
  });
  return /* @__PURE__ */ l(
    b.button,
    {
      type: "button",
      "aria-pressed": o,
      "data-state": o ? "on" : "off",
      "data-disabled": e.disabled ? "" : void 0,
      ...s,
      ref: i,
      onClick: Q(e.onClick, () => {
        e.disabled || a(!o);
      })
    }
  );
});
V.displayName = R;
var g = "ToggleGroup", [E] = K(g, [
  $
]), M = $(), P = d.forwardRef((e, i) => {
  const { type: n, ...r } = e;
  if (n === "single")
    return /* @__PURE__ */ l(ee, { ...r, ref: i });
  if (n === "multiple")
    return /* @__PURE__ */ l(oe, { ...r, ref: i });
  throw new Error(`Missing prop \`type\` expected on \`${g}\``);
});
P.displayName = g;
var [S, A] = E(g), ee = d.forwardRef((e, i) => {
  const {
    value: n,
    defaultValue: r,
    onValueChange: t = () => {
    },
    ...s
  } = e, [o, a] = y({
    prop: n,
    defaultProp: r ?? "",
    onChange: t,
    caller: g
  });
  return /* @__PURE__ */ l(
    S,
    {
      scope: e.__scopeToggleGroup,
      type: "single",
      value: d.useMemo(() => o ? [o] : [], [o]),
      onItemActivate: a,
      onItemDeactivate: d.useCallback(() => a(""), [a]),
      children: /* @__PURE__ */ l(j, { ...s, ref: i })
    }
  );
}), oe = d.forwardRef((e, i) => {
  const {
    value: n,
    defaultValue: r,
    onValueChange: t = () => {
    },
    ...s
  } = e, [o, a] = y({
    prop: n,
    defaultProp: r ?? [],
    onChange: t,
    caller: g
  }), u = d.useCallback(
    (p) => a((c = []) => [...c, p]),
    [a]
  ), m = d.useCallback(
    (p) => a((c = []) => c.filter((_) => _ !== p)),
    [a]
  );
  return /* @__PURE__ */ l(
    S,
    {
      scope: e.__scopeToggleGroup,
      type: "multiple",
      value: o,
      onItemActivate: u,
      onItemDeactivate: m,
      children: /* @__PURE__ */ l(j, { ...s, ref: i })
    }
  );
});
P.displayName = g;
var [re, te] = E(g), j = d.forwardRef(
  (e, i) => {
    const {
      __scopeToggleGroup: n,
      disabled: r = !1,
      rovingFocus: t = !0,
      orientation: s,
      dir: o,
      loop: a = !0,
      ...u
    } = e, m = M(n), p = Y(o), c = { role: "group", dir: p, ...u };
    return /* @__PURE__ */ l(re, { scope: n, rovingFocus: t, disabled: r, children: t ? /* @__PURE__ */ l(
      X,
      {
        asChild: !0,
        ...m,
        orientation: s,
        dir: p,
        loop: a,
        children: /* @__PURE__ */ l(b.div, { ...c, ref: i })
      }
    ) : /* @__PURE__ */ l(b.div, { ...c, ref: i }) });
  }
), v = "ToggleGroupItem", D = d.forwardRef(
  (e, i) => {
    const n = A(v, e.__scopeToggleGroup), r = te(v, e.__scopeToggleGroup), t = M(e.__scopeToggleGroup), s = n.value.includes(e.value), o = r.disabled || e.disabled, a = { ...e, pressed: s, disabled: o }, u = d.useRef(null);
    return r.rovingFocus ? /* @__PURE__ */ l(
      W,
      {
        asChild: !0,
        ...t,
        focusable: !o,
        active: s,
        ref: u,
        children: /* @__PURE__ */ l(T, { ...a, ref: i })
      }
    ) : /* @__PURE__ */ l(T, { ...a, ref: i });
  }
);
D.displayName = v;
var T = d.forwardRef(
  (e, i) => {
    const { __scopeToggleGroup: n, value: r, ...t } = e, s = A(v, n), o = { role: "radio", "aria-checked": e.pressed, "aria-pressed": void 0 }, a = s.type === "single" ? o : void 0;
    return /* @__PURE__ */ l(
      V,
      {
        ...a,
        ...t,
        ref: i,
        onPressedChange: (u) => {
          u ? s.onItemActivate(r) : s.onItemDeactivate(r);
        }
      }
    );
  }
), k = P, ae = D;
const F = {
  "purpur-chip-group": "_purpur-chip-group_m2vd5_1",
  "purpur-chip-group--container": "_purpur-chip-group--container_m2vd5_6",
  "purpur-chip-group--full-width": "_purpur-chip-group--full-width_m2vd5_12"
}, le = {
  "purpur-chip-group-item": "_purpur-chip-group-item_11j23_1",
  "purpur-chip-group-item__line-wrapper": "_purpur-chip-group-item__line-wrapper_11j23_19",
  "purpur-chip-group-item__checkmark-icon": "_purpur-chip-group-item__checkmark-icon_11j23_36",
  "purpur-chip-group-item--disabled": "_purpur-chip-group-item--disabled_11j23_51"
}, C = x.bind(le), f = "purpur-chip-group-item", O = ({
  ["aria-label"]: e,
  children: i,
  className: n,
  ["data-testid"]: r,
  disabled: t = !1,
  showCheckmark: s = !1,
  unavailable: o = !1,
  value: a,
  ...u
}) => {
  const m = C([
    f,
    {
      [`${f}--disabled`]: t,
      [`${f}--unavailable`]: o
    },
    n
  ]), p = (c) => {
    t && c.preventDefault();
  };
  return /* @__PURE__ */ w(
    ae,
    {
      "aria-label": e,
      "data-testid": r,
      className: `${m} ${f}`,
      value: a,
      onClick: p,
      "aria-disabled": t,
      ...u,
      children: [
        i,
        s && /* @__PURE__ */ l(
          Z,
          {
            size: "sm",
            className: C(`${f}__checkmark-icon`),
            "data-testid": "checkmark icon"
          }
        ),
        o && /* @__PURE__ */ l(
          "span",
          {
            className: C(`${f}__line-wrapper`),
            "data-testid": "unavailable line wrapper"
          }
        )
      ]
    }
  );
}, ie = (e) => z(e) && e?.type === O, ne = (e) => e.type === "choice", N = 2, se = x.bind(F), G = "purpur-chip-group", H = ({ children: e, className: i, fullWidth: n = !1, title: r, titleProps: t, name: s, ...o }) => {
  const a = se([
    G,
    {
      [`${G}--full-width`]: n
    },
    i
  ]), u = L(), m = I.toArray(e).filter(ie), p = I.map(
    m,
    (h) => U(h, {
      showCheckmark: o.type === "filter" && o.value.includes(h.props.value),
      unavailable: o.type === "choice" && h.props.unavailable
    })
  ), c = (h) => {
    (o.type === "filter" || h !== "") && o.onValueChange(h);
  };
  q(() => {
    o.type === "choice" && p.length < N && console.warn(
      `Minimum number of chips should be ${N} when using the "choice" type`
    );
  }, [p, o.type]);
  const _ = () => ne(o) ? /* @__PURE__ */ l(
    k,
    {
      className: a,
      ...o,
      "aria-labelledby": r ? `${t?.id ?? u}-label` : void 0,
      onValueChange: c,
      type: "single",
      children: p
    }
  ) : /* @__PURE__ */ l(
    k,
    {
      className: a,
      ...o,
      "aria-labelledby": r ? `${t?.id ?? u}-label` : void 0,
      onValueChange: c,
      type: "multiple",
      children: p
    }
  );
  return /* @__PURE__ */ w("div", { className: F[`${G}--container`], children: [
    r && /* @__PURE__ */ l(
      J,
      {
        tag: "h2",
        variant: "subsection-100",
        ...t,
        id: `${t?.id ?? u}-label`,
        children: r
      }
    ),
    _(),
    s && o.type === "choice" && /* @__PURE__ */ l("input", { type: "hidden", name: s, value: o.value })
  ] });
};
H.Item = O;
H.displayName = "ChipGroup";
export {
  H as C
};
//# sourceMappingURL=chip-group-CJVvfl_J.mjs.map
