import { jsx as d, jsxs as b } from "react/jsx-runtime";
import { createContext as P, useContext as j, useState as x, Children as B, isValidElement as v, cloneElement as L } from "react";
import c from "../../utils/classNames.js";
import S from "./RadioButton.js";
const V = P(void 0), A = (o, t, l, s) => {
  if (s === "stacked") {
    const n = t === 0, a = t === l - 1;
    return c(
      "padding-15 border",
      n && "rounded-top-left rounded-top-right",
      !n && "margin-top--1",
      a && "rounded-bottom-left rounded-bottom-right",
      o && "border-color-highlight bg-highlight-decent z-index-1"
    );
  }
  if (s === "stacked-horizontal") {
    const n = t === 0, a = t === l - 1;
    return c(
      "padding-15 border",
      n && "rounded-top-left rounded-bottom-left",
      !n && "margin-left--1",
      a && "rounded-top-right rounded-bottom-right",
      o && "border-color-highlight bg-highlight-decent z-index-1"
    );
  }
  return c("padding-15 border rounded", o && "border-color-highlight");
}, N = (o) => {
  const { value: t, disabled: l = !1, className: s, children: n, _itemIndex: a = 0, _itemsCount: u = 1, ...h } = o, r = j(V);
  if (!r)
    throw new Error("RadioCardItem must be used inside RadioCardGroup.");
  const m = r.selectedValue === t;
  return /* @__PURE__ */ d(
    S,
    {
      ...h,
      name: r.name,
      value: t,
      custom: !0,
      checked: m,
      onChange: (g) => r.onSelect(t, g),
      disabled: l,
      className: c(
        A(m, a, u, r.variant),
        r.focusedValue === t && !m && "border-color-gray",
        r.cardClassName,
        s
      ),
      onFocus: () => r.setFocusedValue(t),
      onBlur: () => r.setFocusedValue(void 0),
      children: /* @__PURE__ */ d("div", { className: "radio-text-wrapper display-flex", children: n })
    }
  );
}, O = ({ className: o, ...t }) => /* @__PURE__ */ d("span", { className: c("radio-text", o), ...t }), K = (o) => {
  const {
    name: t,
    options: l = [],
    value: s,
    defaultValue: n,
    variant: a = "inline",
    gap: u = "gap-15",
    onChange: h,
    className: r,
    cardClassName: m,
    children: g,
    ...y
  } = o, p = s != null, [I, R] = x(n), [k, F] = x(), _ = p ? s : I, z = (e) => (i) => {
    p || R(e), h?.(e, i);
  }, w = c(
    a === "stacked" && "display-flex flex-column",
    a === "stacked-horizontal" && "display-flex",
    a === "inline" && "display-flex",
    a === "inline" && u,
    r
  ), E = {
    name: t,
    selectedValue: _,
    variant: a,
    cardClassName: m,
    focusedValue: k,
    setFocusedValue: F,
    onSelect: (e, i) => z(e)(i)
  }, f = B.toArray(g).filter((e) => v(e)), C = f.map((e, i) => !v(e) || e.type !== N ? e : L(e, {
    _itemIndex: i,
    _itemsCount: f.length
  })), G = l.map((e, i) => /* @__PURE__ */ b(
    N,
    {
      value: e.value,
      disabled: e.disabled,
      className: e.className,
      _itemIndex: i,
      _itemsCount: l.length,
      children: [
        /* @__PURE__ */ d(O, {}),
        /* @__PURE__ */ b("div", { className: "margin-left-5 margin-right-5", children: [
          /* @__PURE__ */ d("div", { className: "text-medium", children: e.label }),
          e.description && /* @__PURE__ */ d("div", { className: "text-color-dark", children: e.description })
        ] })
      ]
    },
    e.value
  ));
  return /* @__PURE__ */ d(V.Provider, { value: E, children: /* @__PURE__ */ d("div", { className: w, ...y, children: C.length > 0 ? C : G }) });
};
export {
  O as RadioCardIndicator,
  N as RadioCardItem,
  K as default
};
//# sourceMappingURL=RadioCardGroup.js.map
