import { jsxs as m, jsx as r, Fragment as V } from "react/jsx-runtime";
import { forwardRef as _, useState as z, useCallback as R } from "react";
import { cx as h } from "../../utils/classNames.js";
import { useArrowTheme as j } from "../../hooks/useArrowTheme.js";
function E() {
  return /* @__PURE__ */ r(
    "svg",
    {
      role: "img",
      "aria-label": "error",
      width: "12",
      height: "12",
      viewBox: "0 0 24 24",
      fill: "currentColor",
      children: /* @__PURE__ */ r("path", { d: "M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z" })
    }
  );
}
function F(d, a) {
  if (a !== void 0)
    return a;
  const l = d.find((o) => o.checked);
  return l ? l.value : "";
}
const G = _(
  ({
    legend: d,
    inverse: a,
    groupName: l,
    ariaRequired: o = !1,
    errorMessage: t = "",
    radioButtons: n,
    legendAsLabel: p = !1,
    value: s,
    defaultValue: f,
    className: b,
    onChange: c,
    ...u
  }, y) => {
    const g = j(), N = a ?? g.inverse, i = t.trim(), [k, x] = z(
      () => F(n, f)
    ), v = s !== void 0 ? s : k, w = R(
      (e) => {
        s === void 0 && x(e.target.value), c == null || c(e.target.value, e);
      },
      [c, s]
    );
    return n.length === 0 ? null : /* @__PURE__ */ m(
      "fieldset",
      {
        ref: y,
        className: h("tdds-radiogroup", b),
        role: "radiogroup",
        "aria-required": o || void 0,
        "data-inverse": N ? "true" : void 0,
        "aria-label": p ? d : void 0,
        "aria-describedby": i ? l + "-error" : void 0,
        ...u,
        children: [
          p ? null : /* @__PURE__ */ r("legend", { className: "tdds-radiogroup__legend", children: d }),
          n.map((e) => {
            const I = v !== "" ? v === e.value : !!e.checked;
            return /* @__PURE__ */ m("div", { className: "tdds-radio", children: [
              /* @__PURE__ */ r(
                "input",
                {
                  id: e.id,
                  type: "radio",
                  name: l,
                  value: e.value,
                  "aria-disabled": e.disabled || void 0,
                  checked: I,
                  "aria-invalid": i.length > 0 || void 0,
                  "aria-describedby": e.optionalBody ? e.id + "-optional-body" : void 0,
                  "aria-labelledby": e.labelledBy,
                  className: h({ "force-focus": e.focused }),
                  onChange: w
                }
              ),
              /* @__PURE__ */ r(
                "label",
                {
                  htmlFor: e.id,
                  className: h({
                    "force-pressed": e.pressed,
                    "force-hover": e.hover
                  }),
                  children: e.label
                }
              ),
              e.optionalBody ? /* @__PURE__ */ r(
                "p",
                {
                  id: e.id + "-optional-body",
                  className: "tdds-radio__optional-body",
                  children: e.optionalBody
                }
              ) : null
            ] }, e.id);
          }),
          /* @__PURE__ */ r(
            "p",
            {
              id: l + "-error",
              className: "tdds-radiogroup--error-message",
              "aria-hidden": !i || void 0,
              children: i ? /* @__PURE__ */ m(V, { children: [
                /* @__PURE__ */ r(E, {}),
                i
              ] }) : null
            }
          )
        ]
      }
    );
  }
);
G.displayName = "RadioGroup";
export {
  G as RadioGroup
};
//# sourceMappingURL=RadioGroup.js.map
