import { jsxs, jsx } from 'react/jsx-runtime';
import * as React from 'react';
import { createContext, useContext, forwardRef, useId, useMemo, useState } from 'react';
import { c as cx } from './index-De1g9FRV.js';
import { F as FieldLabel } from './FieldLabel-DZWu5dMp.js';
import { F as FieldMessage } from './FieldMessage-DzxfodaI.js';
import { w as warnDeprecatedErrorUsage } from './utils-CM48ODEJ.js';
import { u as useLayoutPropsUtil } from './useLayoutPropsUtil-CB_zHDbW.js';
import { w as warnOnce } from './warnOnce-Y9PRHcU4.js';
import { s as stripInlineMarkdown } from './stripInlineMarkdown-BWbMy7r_.js';
import { I as Icon } from './Icon-BMMbp40n.js';
import { H as Helper } from './Helper-DtDeaYTn.js';

import './Radio.css';const wrapper = "_wrapper_1fh0d_1";
const radio = "_radio_1fh0d_7";
const icon = "_icon_1fh0d_85";
const legend = "_legend_1fh0d_248";
const children = "_children_1fh0d_261";
const styles = {
	wrapper: wrapper,
	radio: radio,
	"icon-wrapper": "_icon-wrapper_1fh0d_85",
	"focus-label": "_focus-label_1fh0d_106",
	"helper-wrapper": "_helper-wrapper_1fh0d_129",
	icon: icon,
	"radio-checked": "_radio-checked_1fh0d_145",
	"radio-error": "_radio-error_1fh0d_182",
	"sr-only": "_sr-only_1fh0d_218",
	"radio-group": "_radio-group_1fh0d_231",
	legend: legend,
	children: children,
	"label-row": "_label-row_1fh0d_273"
};

const RadioGroupContext = createContext(void 0);
const useRadioGroupContext = () => {
  return useContext(RadioGroupContext);
};

const RadioGroup = forwardRef(
  (props, ref) => {
    const { layoutStyles, componentProps } = useLayoutPropsUtil(props);
    const {
      className,
      children,
      description,
      error,
      errorAriaLive: _errorAriaLive,
      hint,
      warning,
      legend,
      moreInfo,
      legendProps,
      onBlur,
      onFocus,
      required,
      style,
      hideLabel = false,
      ...rest
    } = componentProps;
    const RadioGroupClassNames = cx(styles["radio-group"], className);
    const helperUid = useId();
    const moreInfoUid = useId();
    const styleCombined = {
      ...style,
      ...layoutStyles,
      alignItems: layoutStyles?.alignItems ?? (layoutStyles?.flexDirection === "row" ? "center" : void 0)
    };
    warnDeprecatedErrorUsage("RadioGroup", error);
    if (typeof legend !== "string") {
      warnOnce(
        "RadioGroup: Passing ReactNode to legend is deprecated. Use a plain string with inline markdown instead."
      );
    }
    const errorMessages = typeof error === "boolean" || error === void 0 ? void 0 : error;
    const hasHelperText = hint || errorMessages || warning || description;
    const ariaDescribedBy = [hasHelperText && helperUid, moreInfo && moreInfoUid].filter(Boolean).join(" ") || void 0;
    const contextValue = useMemo(
      () => ({
        hasError: !!error
      }),
      [error]
    );
    return /* @__PURE__ */ jsxs(
      "fieldset",
      {
        role: "radiogroup",
        "data-anv": "radio-group",
        className: RadioGroupClassNames,
        "aria-required": required,
        style: styleCombined,
        ref,
        onFocus,
        onBlur,
        ...rest,
        "aria-describedby": ariaDescribedBy,
        children: [
          /* @__PURE__ */ jsxs("legend", { className: styles["legend"], children: [
            typeof legend === "string" ? stripInlineMarkdown(legend) : legend,
            required && ", required"
          ] }),
          !hideLabel && /* @__PURE__ */ jsx("div", { className: styles["label-row"], children: /* @__PURE__ */ jsx(
            FieldLabel,
            {
              "aria-hidden": true,
              required,
              moreInfo,
              moreInfoId: moreInfo ? moreInfoUid : void 0,
              ...legendProps,
              className: legendProps?.className,
              children: legend
            }
          ) }),
          /* @__PURE__ */ jsx(RadioGroupContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsx(
            "div",
            {
              className: styles["children"],
              style: {
                flexDirection: layoutStyles?.flexDirection,
                gap: layoutStyles?.gap
              },
              children
            }
          ) }),
          hasHelperText ? /* @__PURE__ */ jsx(
            FieldMessage,
            {
              id: helperUid,
              hint,
              error: errorMessages,
              warning,
              description
            }
          ) : null
        ]
      }
    );
  }
);
RadioGroup.displayName = "RadioGroup";

const SvgRadioButtonUnchecked = (props) => /* @__PURE__ */ React.createElement("svg", { xmlns: "http://www.w3.org/2000/svg", width: "1em", height: "1em", viewBox: "0 0 24 24", ...props }, /* @__PURE__ */ React.createElement("path", { d: "M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z" }));

const SvgRadioButtonChecked = (props) => /* @__PURE__ */ React.createElement("svg", { xmlns: "http://www.w3.org/2000/svg", width: "1em", height: "1em", viewBox: "0 0 24 24", ...props }, /* @__PURE__ */ React.createElement("path", { d: "M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z" }), /* @__PURE__ */ React.createElement("circle", { cx: 12, cy: 12, r: 5 }));

const RadioElement = forwardRef((props, ref) => {
  const { layoutStyles, componentProps } = useLayoutPropsUtil(props);
  const {
    className,
    checked: checkedProp,
    defaultChecked,
    value,
    icons,
    error,
    label,
    onChange,
    style,
    description,
    hideRadio = false,
    labelWrapperClassName,
    ...rest
  } = componentProps;
  const [checked, setChecked] = useState(checkedProp ?? defaultChecked);
  const groupContext = useRadioGroupContext();
  const {
    checked: checkedIcon = SvgRadioButtonChecked,
    unchecked: uncheckedIcon = SvgRadioButtonUnchecked
  } = { ...icons };
  const isControlled = typeof checkedProp !== "undefined";
  const onChangeHandler = (e) => {
    if (!isControlled) {
      setChecked(e.target.checked);
    }
    onChange?.(e, { value, checked: checkedProp ?? e.target.checked });
  };
  const helperUid = useId();
  const hasError = error || groupContext?.hasError;
  const wrapperClassNames = cx(
    styles["wrapper"],
    { [styles["focus"]]: hideRadio },
    className
  );
  const radioClassNames = cx(styles["radio"], {
    [styles["radio-error"]]: hasError,
    [styles["radio-checked"]]: isControlled ? checkedProp : checked
  });
  const styleCombined = {
    ...style,
    ...layoutStyles
  };
  return /* @__PURE__ */ jsxs("div", { className: wrapperClassNames, style: styleCombined, "data-anv": "radio", children: [
    /* @__PURE__ */ jsxs("label", { className: radioClassNames, children: [
      /* @__PURE__ */ jsx(
        "input",
        {
          type: "radio",
          "aria-checked": isControlled ? checkedProp : checked,
          checked: isControlled ? checkedProp : void 0,
          defaultChecked,
          value,
          onChange: onChangeHandler,
          "aria-describedby": description ? helperUid : void 0,
          ref,
          ...rest
        }
      ),
      /* @__PURE__ */ jsx(
        "span",
        {
          className: cx(styles["icon-wrapper"], {
            [styles["sr-only"]]: hideRadio
          }),
          "aria-hidden": true,
          children: /* @__PURE__ */ jsx(
            Icon,
            {
              size: "large",
              svg: (isControlled ? checkedProp : checked) ? checkedIcon : uncheckedIcon,
              className: styles["icon"]
            }
          )
        }
      ),
      /* @__PURE__ */ jsx(
        "span",
        {
          className: cx(labelWrapperClassName, {
            [styles["focus-label"]]: hideRadio
          }),
          children: label
        }
      )
    ] }),
    description ? /* @__PURE__ */ jsx("div", { className: styles["helper-wrapper"], children: /* @__PURE__ */ jsx(Helper, { id: helperUid, description }) }) : null
  ] });
});
RadioElement.displayName = "RadioElement";
const Radio = RadioElement;

export { Radio as R, SvgRadioButtonChecked as S, RadioGroup as a, SvgRadioButtonUnchecked as b };
//# sourceMappingURL=Radio-DO4UhbBE.js.map
