import { r as f, m as H, s as J, aT as u, j as g, aU as Q, aI as Y, aJ as Z, aV as q, aK as ee, ap as te, aW as re, aQ as ne, ai as k, al as $, am as oe, aX as ie, aY as ae, aZ as le } from "./index-6wLGsgHc.js";
import { S as ce } from "./useBasicWidgetState-DqA4I-I_-Cy-zNgyc.js";
var ue = {
  secondary: "secondary"
}, z = {
  default: "default"
}, se = {
  default: "default"
}, P = Object.freeze({
  radio: "radio",
  checkbox: "checkbox"
}), N = ie("div", function(e) {
  var t = e.$shape, r = e.$length, n = e.$theme, o = r === 1 ? void 0 : t !== z.default ? "-".concat(n.sizing.scale100) : "-0.5px";
  return {
    display: "flex",
    marginLeft: o,
    marginRight: o
  };
});
N.displayName = "StyledRoot";
N.displayName = "StyledRoot";
function D(e) {
  "@babel/helpers - typeof";
  return D = 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;
  }, D(e);
}
function A() {
  return A = 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;
  }, A.apply(this, arguments);
}
function U(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 fe(e) {
  for (var t = 1; t < arguments.length; t++) {
    var r = arguments[t] != null ? arguments[t] : {};
    t % 2 ? U(Object(r), !0).forEach(function(n) {
      V(e, n, r[n]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(r)) : U(Object(r)).forEach(function(n) {
      Object.defineProperty(e, n, Object.getOwnPropertyDescriptor(r, n));
    });
  }
  return e;
}
function de(e, t) {
  return me(e) || be(e, t) || ye(e, t) || pe();
}
function pe() {
  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 ye(e, t) {
  if (e) {
    if (typeof e == "string") return W(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 W(e, t);
  }
}
function W(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 be(e, t) {
  var r = e == null ? null : typeof Symbol < "u" && e[Symbol.iterator] || e["@@iterator"];
  if (r != null) {
    var n = [], o = !0, a = !1, i, d;
    try {
      for (r = r.call(e); !(o = (i = r.next()).done) && (n.push(i.value), !(t && n.length === t)); o = !0) ;
    } catch (b) {
      a = !0, d = b;
    } finally {
      try {
        !o && r.return != null && r.return();
      } finally {
        if (a) throw d;
      }
    }
    return n;
  }
}
function me(e) {
  if (Array.isArray(e)) return e;
}
function he(e, t) {
  if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function");
}
function ge(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 Se(e, t, r) {
  return t && ge(e.prototype, t), Object.defineProperty(e, "prototype", {
    writable: !1
  }), e;
}
function ve(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 && G(e, t);
}
function G(e, t) {
  return G = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(r, n) {
    return r.__proto__ = n, r;
  }, G(e, t);
}
function Oe(e) {
  var t = we();
  return function() {
    var r = I(e), n;
    if (t) {
      var o = I(this).constructor;
      n = Reflect.construct(r, arguments, o);
    } else n = r.apply(this, arguments);
    return Ee(this, n);
  };
}
function Ee(e, t) {
  if (t && (D(t) === "object" || typeof t == "function")) return t;
  if (t !== void 0) throw new TypeError("Derived constructors may only return object or undefined");
  return X(e);
}
function X(e) {
  if (e === void 0) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  return e;
}
function we() {
  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 I(e) {
  return I = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function(t) {
    return t.__proto__ || Object.getPrototypeOf(t);
  }, I(e);
}
function V(e, t, r) {
  return t in e ? Object.defineProperty(e, t, {
    value: r,
    enumerable: !0,
    configurable: !0,
    writable: !0
  }) : e[t] = r, e;
}
function Le(e, t) {
  return !Array.isArray(e) && typeof e != "number" ? !1 : Array.isArray(e) ? e.includes(t) : e === t;
}
var F = /* @__PURE__ */ (function(e) {
  ve(r, e);
  var t = Oe(r);
  function r() {
    var n;
    he(this, r);
    for (var o = arguments.length, a = new Array(o), i = 0; i < o; i++) a[i] = arguments[i];
    return n = t.call.apply(t, [this].concat(a)), V(X(n), "childRefs", {}), n;
  }
  return Se(r, [{
    key: "render",
    value: function() {
      var n = this, o = this.props, a = o.overrides, i = a === void 0 ? {} : a, d = o.mode, b = d === void 0 ? P.checkbox : d, c = o.children, m = o.selected, w = o.disabled, p = o.onClick, O = o.kind, y = o.shape, L = o.size, S = ae(i.Root, N), x = de(S, 2), j = x[0], T = x[1], R = this.props["aria-label"] || this.props.ariaLabel, s = b === P.radio, E = f.Children.count(c);
      return /* @__PURE__ */ f.createElement(le.Consumer, null, function(K) {
        return /* @__PURE__ */ f.createElement(j, A({
          "aria-label": R || K.buttongroup.ariaLabel,
          "data-baseweb": "button-group",
          role: s ? "radiogroup" : "group",
          $shape: y,
          $length: c.length
        }, T), f.Children.map(c, function(h, C) {
          if (!/* @__PURE__ */ f.isValidElement(h)) return null;
          var M = h.props.isSelected ? h.props.isSelected : Le(m, C);
          return s && (n.childRefs[C] = /* @__PURE__ */ f.createRef()), /* @__PURE__ */ f.cloneElement(h, {
            disabled: w || h.props.disabled,
            isSelected: M,
            ref: s ? n.childRefs[C] : void 0,
            tabIndex: !s || M || s && (!m || m === -1) && C === 0 ? 0 : -1,
            onKeyDown: function(l) {
              if (s) {
                var v = Number(m) ? Number(m) : 0;
                if (l.key === "ArrowUp" || l.key === "ArrowLeft") {
                  l.preventDefault && l.preventDefault();
                  var _ = v - 1 < 0 ? E - 1 : v - 1;
                  p && p(l, _), n.childRefs[_].current && n.childRefs[_].current.focus();
                }
                if (l.key === "ArrowDown" || l.key === "ArrowRight") {
                  l.preventDefault && l.preventDefault();
                  var B = v + 1 > E - 1 ? 0 : v + 1;
                  p && p(l, B), n.childRefs[B].current && n.childRefs[B].current.focus();
                }
              }
            },
            kind: O,
            onClick: function(l) {
              w || (h.props.onClick && h.props.onClick(l), p && p(l, C));
            },
            shape: y,
            size: L,
            overrides: fe({
              BaseButton: {
                style: function(l) {
                  var v = l.$theme;
                  return c.length === 1 ? {} : y !== z.default ? {
                    marginLeft: v.sizing.scale100,
                    marginRight: v.sizing.scale100
                  } : {
                    marginLeft: "0.5px",
                    marginRight: "0.5px"
                  };
                },
                props: {
                  "aria-checked": M,
                  role: s ? "radio" : "checkbox"
                }
              }
            }, h.props.overrides)
          });
        }));
      });
    }
  }]), r;
})(f.Component);
V(F, "defaultProps", {
  disabled: !1,
  onClick: function() {
  },
  shape: z.default,
  size: se.default,
  kind: ue.secondary
});
function je(e, t) {
  return t.includes(e) ? t.filter((r) => r !== e) : [...t, e];
}
function Re(e, t, r) {
  return e == u.ClickMode.MULTI_SELECT ? je(t, r ?? []) : r?.includes(t) ? [] : [t];
}
function Ce(e) {
  return e.length === 0 ? -1 : e[0];
}
function ke(e, t, r, n) {
  t.setIntArrayValue(e, r.value, {
    fromUi: r.fromUi
  }, n);
}
function xe(e, t, r) {
  const n = r === u.Style.PILLS ? k.PILLS : r === u.Style.BORDERLESS ? k.BORDERLESS_ICON : k.SEGMENTED_CONTROL, o = r === u.Style.BORDERLESS ? $.XSMALL : $.MEDIUM, a = n === k.PILLS || n === k.SEGMENTED_CONTROL, i = r === u.Style.BORDERLESS ? "lg" : "base";
  return {
    element: /* @__PURE__ */ g.jsx(oe, {
      icon: t,
      label: e,
      iconSize: i,
      useSmallerFont: a
    }),
    kind: n,
    size: o
  };
}
function Pe(e, t, r, n) {
  return r.indexOf(n) > -1 ? !0 : t !== u.ClickMode.SINGLE_SELECT || e !== u.SelectionVisualization.ALL_UP_TO_SELECTED ? !1 : r.length > 0 && n < r[0];
}
function Ie(e, t) {
  return e && (t = `${t}Active`), t;
}
function Te(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", a = r ? {} : {
    content: "''",
    flex: 1e4
  };
  switch (e) {
    case u.Style.BORDERLESS:
      return {
        ...n,
        columnGap: t.threeXS,
        rowGap: t.threeXS
      };
    case u.Style.PILLS:
      return {
        ...n,
        columnGap: t.twoXS,
        rowGap: t.twoXS,
        width: o
      };
    case u.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": a,
        width: o
      };
    default:
      return n;
  }
}
function Me(e, t, r, n, o, a, i) {
  const d = Pe(r, n, o, t);
  let b = e.content, c = e.contentIcon;
  return d && (b = e.selectedContent ? e.selectedContent : b, c = e.selectedContentIcon ? e.selectedContentIcon : c), f.forwardRef(function(m, w) {
    const {
      element: p,
      kind: O,
      size: y
    } = xe(b ?? "", c ?? void 0, a), L = Ie(!!(d && !e.selectedContent && !e.selectedContentIcon), O);
    return /* @__PURE__ */ g.jsx(ne, {
      ...m,
      size: y,
      kind: L,
      containerWidth: i,
      children: p
    });
  });
}
function _e(e, t) {
  return e.getIntArrayValue(t);
}
function Be(e) {
  return e.default ?? null;
}
function De(e) {
  return e.value ?? null;
}
function Ae(e) {
  const {
    disabled: t,
    element: r,
    fragmentId: n,
    widgetMgr: o,
    widthConfig: a
  } = e, {
    clickMode: i,
    options: d,
    selectionVisualization: b,
    style: c,
    label: m,
    labelVisibility: w,
    help: p
  } = r, O = H(), [y, L] = ce({
    getStateFromWidgetMgr: _e,
    getDefaultStateFromProto: Be,
    getCurrStateFromProto: De,
    updateWidgetMgrState: ke,
    element: r,
    widgetMgr: o,
    fragmentId: n
  }), S = J(a), x = (R, s) => {
    const E = Re(i, s, y);
    L({
      value: E,
      fromUi: !0
    });
  };
  let j;
  i === u.ClickMode.SINGLE_SELECT ? j = P.radio : i === u.ClickMode.MULTI_SELECT && (j = P.checkbox);
  const T = f.useMemo(() => d.map((R, s) => {
    const E = Me(R, s, b, i, y, c, S);
    return /* @__PURE__ */ g.jsx(E, {}, `${R.content}-${s}`);
  }), [i, d, b, c, y, S]);
  return /* @__PURE__ */ g.jsxs(Q, {
    className: "stButtonGroup",
    "data-testid": "stButtonGroup",
    containerWidth: S,
    children: [/* @__PURE__ */ g.jsx(Y, {
      label: m,
      disabled: t,
      labelVisibility: Z(w?.value ?? re.LabelVisibilityOptions.COLLAPSED),
      children: p && /* @__PURE__ */ g.jsx(q, {
        children: /* @__PURE__ */ g.jsx(ee, {
          content: p,
          placement: te.TOP
        })
      })
    }), /* @__PURE__ */ g.jsx(F, {
      disabled: t,
      mode: j,
      onClick: x,
      selected: i === u.ClickMode.MULTI_SELECT ? y : Ce(y),
      overrides: {
        Root: {
          style: f.useCallback(() => Te(c, O.spacing, S), [c, O.spacing, S])
        }
      },
      children: T
    })]
  });
}
const Ne = f.memo(Ae);
export {
  Ne as default
};
//# sourceMappingURL=index-DmB3SAP2-CarDrJ9D.js.map
