import { aa as q, o as J, ar as Q, u as Y, as as ee, at as l, j as g, au as te, W as re, l as ne, g as oe, T as ie, P as ae, av as ue, c as le, B as P, d as U, D as se } from "./index-CsBCemCF.js";
import * as S from "react";
import { memo as ce, useMemo as fe, useCallback as pe, forwardRef as de } from "react";
import { u as ye } from "./useBasicWidgetState-KOlq_goV.js";
var me = {
  secondary: "secondary"
}, N = {
  default: "default"
}, he = {
  default: "default"
}, j = Object.freeze({
  radio: "radio",
  checkbox: "checkbox"
}), z = q("div", function(e) {
  var t = e.$shape, r = e.$length, n = e.$theme, o = r === 1 ? void 0 : t !== N.default ? "-".concat(n.sizing.scale100) : "-0.5px";
  return {
    display: "flex",
    marginLeft: o,
    marginRight: o
  };
});
z.displayName = "StyledRoot";
z.displayName = "StyledRoot";
function M(e) {
  "@babel/helpers - typeof";
  return M = typeof Symbol == "function" && typeof Symbol.iterator == "symbol" ? function(t) {
    return typeof t;
  } : function(t) {
    return t && typeof Symbol == "function" && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t;
  }, M(e);
}
function G() {
  return G = Object.assign ? Object.assign.bind() : function(e) {
    for (var t = 1; t < arguments.length; t++) {
      var r = arguments[t];
      for (var n in r)
        Object.prototype.hasOwnProperty.call(r, n) && (e[n] = r[n]);
    }
    return e;
  }, G.apply(this, arguments);
}
function X(e, t) {
  var r = Object.keys(e);
  if (Object.getOwnPropertySymbols) {
    var n = Object.getOwnPropertySymbols(e);
    t && (n = n.filter(function(o) {
      return Object.getOwnPropertyDescriptor(e, o).enumerable;
    })), r.push.apply(r, n);
  }
  return r;
}
function be(e) {
  for (var t = 1; t < arguments.length; t++) {
    var r = arguments[t] != null ? arguments[t] : {};
    t % 2 ? X(Object(r), !0).forEach(function(n) {
      x(e, n, r[n]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(r)) : X(Object(r)).forEach(function(n) {
      Object.defineProperty(e, n, Object.getOwnPropertyDescriptor(r, n));
    });
  }
  return e;
}
function Se(e, t) {
  return Ee(e) || Oe(e, t) || ve(e, t) || ge();
}
function ge() {
  throw new TypeError(`Invalid attempt to destructure non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.`);
}
function ve(e, t) {
  if (e) {
    if (typeof e == "string") return K(e, t);
    var r = Object.prototype.toString.call(e).slice(8, -1);
    if (r === "Object" && e.constructor && (r = e.constructor.name), r === "Map" || r === "Set") return Array.from(e);
    if (r === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)) return K(e, t);
  }
}
function K(e, t) {
  (t == null || t > e.length) && (t = e.length);
  for (var r = 0, n = new Array(t); r < t; r++)
    n[r] = e[r];
  return n;
}
function Oe(e, t) {
  var r = e == null ? null : typeof Symbol < "u" && e[Symbol.iterator] || e["@@iterator"];
  if (r != null) {
    var n = [], o = !0, i = !1, a, p;
    try {
      for (r = r.call(e); !(o = (a = r.next()).done) && (n.push(a.value), !(t && n.length === t)); o = !0)
        ;
    } catch (s) {
      i = !0, p = s;
    } finally {
      try {
        !o && r.return != null && r.return();
      } finally {
        if (i) throw p;
      }
    }
    return n;
  }
}
function Ee(e) {
  if (Array.isArray(e)) return e;
}
function _e(e, t) {
  if (!(e instanceof t))
    throw new TypeError("Cannot call a class as a function");
}
function we(e, t) {
  for (var r = 0; r < t.length; r++) {
    var n = t[r];
    n.enumerable = n.enumerable || !1, n.configurable = !0, "value" in n && (n.writable = !0), Object.defineProperty(e, n.key, n);
  }
}
function Le(e, t, r) {
  return t && we(e.prototype, t), Object.defineProperty(e, "prototype", { writable: !1 }), e;
}
function Re(e, t) {
  if (typeof t != "function" && t !== null)
    throw new TypeError("Super expression must either be null or a function");
  e.prototype = Object.create(t && t.prototype, { constructor: { value: e, writable: !0, configurable: !0 } }), Object.defineProperty(e, "prototype", { writable: !1 }), t && $(e, t);
}
function $(e, t) {
  return $ = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(n, o) {
    return n.__proto__ = o, n;
  }, $(e, t);
}
function Ce(e) {
  var t = je();
  return function() {
    var n = k(e), o;
    if (t) {
      var i = k(this).constructor;
      o = Reflect.construct(n, arguments, i);
    } else
      o = n.apply(this, arguments);
    return Pe(this, o);
  };
}
function Pe(e, t) {
  if (t && (M(t) === "object" || typeof t == "function"))
    return t;
  if (t !== void 0)
    throw new TypeError("Derived constructors may only return object or undefined");
  return F(e);
}
function F(e) {
  if (e === void 0)
    throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  return e;
}
function je() {
  if (typeof Reflect > "u" || !Reflect.construct || Reflect.construct.sham) return !1;
  if (typeof Proxy == "function") return !0;
  try {
    return Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {
    })), !0;
  } catch {
    return !1;
  }
}
function k(e) {
  return k = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function(r) {
    return r.__proto__ || Object.getPrototypeOf(r);
  }, k(e);
}
function x(e, t, r) {
  return t in e ? Object.defineProperty(e, t, { value: r, enumerable: !0, configurable: !0, writable: !0 }) : e[t] = r, e;
}
function ke(e, t) {
  return !Array.isArray(e) && typeof e != "number" ? !1 : Array.isArray(e) ? e.includes(t) : e === t;
}
var H = /* @__PURE__ */ (function(e) {
  Re(r, e);
  var t = Ce(r);
  function r() {
    var n;
    _e(this, r);
    for (var o = arguments.length, i = new Array(o), a = 0; a < o; a++)
      i[a] = arguments[a];
    return n = t.call.apply(t, [this].concat(i)), x(F(n), "childRefs", {}), n;
  }
  return Le(r, [{
    key: "render",
    value: function() {
      var o = this, i = this.props, a = i.overrides, p = a === void 0 ? {} : a, s = i.mode, d = s === void 0 ? j.checkbox : s, v = i.children, y = i.selected, _ = i.disabled, c = i.onClick, m = i.kind, O = i.shape, h = i.size, B = J(p.Root, z), w = Se(B, 2), T = w[0], R = w[1], L = this.props["aria-label"] || this.props.ariaLabel, f = d === j.radio, V = S.Children.count(v);
      return /* @__PURE__ */ S.createElement(Q.Consumer, null, function(Z) {
        return /* @__PURE__ */ S.createElement(T, G({
          "aria-label": L || Z.buttongroup.ariaLabel,
          "data-baseweb": "button-group",
          role: f ? "radiogroup" : "group",
          $shape: O,
          $length: v.length
        }, R), S.Children.map(v, function(b, C) {
          if (!/* @__PURE__ */ S.isValidElement(b))
            return null;
          var I = b.props.isSelected ? b.props.isSelected : ke(y, C);
          return f && (o.childRefs[C] = /* @__PURE__ */ S.createRef()), /* @__PURE__ */ S.cloneElement(b, {
            disabled: _ || b.props.disabled,
            isSelected: I,
            ref: f ? o.childRefs[C] : void 0,
            tabIndex: !f || I || f && (!y || y === -1) && C === 0 ? 0 : -1,
            onKeyDown: function(u) {
              if (f) {
                var E = Number(y) ? Number(y) : 0;
                if (u.key === "ArrowUp" || u.key === "ArrowLeft") {
                  u.preventDefault && u.preventDefault();
                  var A = E - 1 < 0 ? V - 1 : E - 1;
                  c && c(u, A), o.childRefs[A].current && o.childRefs[A].current.focus();
                }
                if (u.key === "ArrowDown" || u.key === "ArrowRight") {
                  u.preventDefault && u.preventDefault();
                  var D = E + 1 > V - 1 ? 0 : E + 1;
                  c && c(u, D), o.childRefs[D].current && o.childRefs[D].current.focus();
                }
              }
            },
            kind: m,
            onClick: function(u) {
              _ || (b.props.onClick && b.props.onClick(u), c && c(u, C));
            },
            shape: O,
            size: h,
            overrides: be({
              BaseButton: {
                style: function(u) {
                  var E = u.$theme;
                  return v.length === 1 ? {} : O !== N.default ? {
                    marginLeft: E.sizing.scale100,
                    marginRight: E.sizing.scale100
                  } : {
                    marginLeft: "0.5px",
                    marginRight: "0.5px"
                  };
                },
                props: {
                  "aria-checked": I,
                  role: f ? "radio" : "checkbox"
                }
              }
            }, b.props.overrides)
          });
        }));
      });
    }
  }]), r;
})(S.Component);
x(H, "defaultProps", {
  disabled: !1,
  onClick: function() {
  },
  shape: N.default,
  size: he.default,
  kind: me.secondary
});
function Be(e, t) {
  return t.includes(e) ? t.filter((r) => r !== e) : [...t, e];
}
function Te(e, t, r) {
  return e == l.ClickMode.MULTI_SELECT ? Be(t, r ?? []) : r?.includes(t) ? [] : [t];
}
function Ie(e) {
  return e.length === 0 ? -1 : e[0];
}
function Ae(e, t, r, n) {
  t.setIntArrayValue(e, r.value, {
    fromUi: r.fromUi
  }, n);
}
function De(e, t, r) {
  const n = r === l.Style.PILLS ? P.PILLS : r === l.Style.BORDERLESS ? P.BORDERLESS_ICON : P.SEGMENTED_CONTROL, o = r === l.Style.BORDERLESS ? U.XSMALL : U.MEDIUM, i = n === P.PILLS || n === P.SEGMENTED_CONTROL, a = r === l.Style.BORDERLESS ? "lg" : "base";
  return {
    element: /* @__PURE__ */ g.jsx(se, { icon: t, label: e, iconSize: a, useSmallerFont: i }),
    kind: n,
    size: o
  };
}
function Me(e, t, r, n) {
  return r.indexOf(n) > -1 ? !0 : t !== l.ClickMode.SINGLE_SELECT || e !== l.SelectionVisualization.ALL_UP_TO_SELECTED ? !1 : r.length > 0 && n < r[0];
}
function Ge(e, t) {
  return e && (t = `${t}Active`), t;
}
function $e(e, t, r) {
  const n = {
    flexWrap: "wrap",
    maxWidth: "100%",
    // This ensures that the button
    // group does not overflow the container due
    // to the negative margins that BaseWeb adds.
    // When maxWidth is set to 100%, without this,
    // the buttons will wrap to the next line.
    margin: "0 0"
  }, o = r ? "100%" : "auto", i = r ? {} : {
    content: "''",
    flex: 1e4
  };
  switch (e) {
    case l.Style.BORDERLESS:
      return {
        ...n,
        columnGap: t.threeXS,
        rowGap: t.threeXS
      };
    case l.Style.PILLS:
      return {
        ...n,
        columnGap: t.twoXS,
        rowGap: t.twoXS,
        width: o
      };
    case l.Style.SEGMENTED_CONTROL:
      return {
        ...n,
        columnGap: t.none,
        rowGap: t.twoXS,
        // Adding an empty pseudo-element after the last button in the group.
        // This will make buttons only as big as needed without stretching to the whole container width (aka let them 'hug' to the side)
        // This is only needed if the button group has content width.
        "::after": i,
        width: o
      };
    default:
      return n;
  }
}
function Ne(e, t, r, n, o, i, a) {
  const p = Me(r, n, o, t);
  let s = e.content, d = e.contentIcon;
  return p && (s = e.selectedContent ? e.selectedContent : s, d = e.selectedContentIcon ? e.selectedContentIcon : d), de(function(y, _) {
    const {
      element: c,
      kind: m,
      size: O
    } = De(s ?? "", d ?? void 0, i), h = Ge(!!(p && !e.selectedContent && !e.selectedContentIcon), m);
    return /* @__PURE__ */ g.jsx(le, { ...y, size: O, kind: h, containerWidth: a, children: c });
  });
}
function ze(e, t) {
  return e.getIntArrayValue(t);
}
function xe(e) {
  return e.default ?? null;
}
function Ve(e) {
  return e.value ?? null;
}
function We(e) {
  const {
    disabled: t,
    element: r,
    fragmentId: n,
    widgetMgr: o,
    widthConfig: i
  } = e, {
    clickMode: a,
    options: p,
    selectionVisualization: s,
    style: d,
    label: v,
    labelVisibility: y,
    help: _
  } = r, c = Y(), [m, O] = ye({
    getStateFromWidgetMgr: ze,
    getDefaultStateFromProto: xe,
    getCurrStateFromProto: Ve,
    updateWidgetMgrState: Ae,
    element: r,
    widgetMgr: o,
    fragmentId: n
  }), h = ee(i), B = (R, L) => {
    const f = Te(a, L, m);
    O({
      value: f,
      fromUi: !0
    });
  };
  let w;
  a === l.ClickMode.SINGLE_SELECT ? w = j.radio : a === l.ClickMode.MULTI_SELECT && (w = j.checkbox);
  const T = fe(() => p.map((R, L) => {
    const f = Ne(R, L, s, a, m, d, h);
    return /* @__PURE__ */ g.jsx(f, {}, `${R.content}-${L}`);
  }), [a, p, s, d, m, h]);
  return /* @__PURE__ */ g.jsxs(te, { className: "stButtonGroup", "data-testid": "stButtonGroup", containerWidth: h, children: [
    /* @__PURE__ */ g.jsx(re, { label: v, disabled: t, labelVisibility: ne(y?.value ?? ue.LabelVisibilityOptions.COLLAPSED), children: _ && /* @__PURE__ */ g.jsx(oe, { children: /* @__PURE__ */ g.jsx(ie, { content: _, placement: ae.TOP }) }) }),
    /* @__PURE__ */ g.jsx(H, { disabled: t, mode: w, onClick: B, selected: a === l.ClickMode.MULTI_SELECT ? m : Ie(m), overrides: {
      Root: {
        style: pe(() => $e(d, c.spacing, h), [d, c.spacing, h])
      }
    }, children: T })
  ] });
}
const Fe = ce(We);
export {
  Fe as default
};
//# sourceMappingURL=index-AJUjtcR2.js.map
