import { filterUndefined } from "../utils/object.js";
import { cs } from "../utils/styles.js";
import "../utils/index.js";
import { disabled } from "../checkbox/consts.js";
import { radioColors } from "./consts.js";
import { omitThemingProps, th, useStyleConfig } from "../core/theme.js";
import { styled } from "../core/styled.js";
import { vui } from "../core/vui.js";
import icon_default from "../icon/icon.js";
import "../icon/index.js";
import t_default from "../t/t.js";
import "../t/index.js";
import "../core/index.js";
import { useRadioGroupContext } from "./context.js";
import { useEffect, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";

//#region src/radio/radio.tsx
const RadioControl = styled.spanBox`
  border-radius: 50%;
  display: inline-flex;
  flex-shrink: 0;
  position: relative;
  transition-duration: fast;
`;
const RadioInput = styled.input`
  cursor: inherit;
  height: 100%;
  left: 0;
  margin: 0;
  opacity: 0;
  padding: 0;
  position: absolute;
  top: 0;
  width: 100%;
  z-index: 1;
`;
const RadioBase = styled.labelBox`
  align-items: center;
  cursor: pointer;
  display: inline-flex;
  width: fit-content;

  &:not([aria-disabled='true']):hover .vui-radioControl {
    color: ${(p) => th.color(p.controlHoverColor)};
  }

  .vui-radioControl:focus-within {
    color: ${radioColors.focus};
  }

  &[aria-disabled='true'] {
    color: ${disabled.text};
    cursor: not-allowed;

    .vui-radioControl {
      color: ${disabled.icon};
    }
  }
`;
/**
* Allows selection of a single choice from a set of items. Handles controlled and uncontrolled modes.
* Uses icons to display itself in different states.
*/
const Radio = vui((props, ref) => {
	const { defaultValue: groupDefaultValue, isChecked: groupIsChecked, onChange: groupOnChange, value: groupValue, ...radioGroupProps } = useRadioGroupContext() ?? {};
	const defaultIsChecked = groupIsChecked !== void 0 ? groupIsChecked === props.value : void 0;
	const mergedProps = {
		...radioGroupProps,
		...props
	};
	const { checked = groupValue !== void 0 ? props.value === groupValue : void 0, children, className, defaultChecked = groupDefaultValue !== void 0 ? props.value === groupDefaultValue : void 0, disabled: disabled$1, icon: iconProp = "uiRadioUnselected", iconChecked = "uiRadioSelected", id, inputProps, inputRef, label, name, onChange, required, value, ...rest } = omitThemingProps(mergedProps);
	const [isChecked, setIsChecked] = useState(defaultIsChecked ?? checked ?? defaultChecked);
	const styles = useStyleConfig("Radio", mergedProps);
	const { color: controlColor, hoverColor, ...controlStyles } = styles.control;
	const icon = isChecked ? iconChecked : iconProp;
	const controlMr = children || label ? 1 : 0;
	const color = isChecked ? radioColors.checked : controlColor;
	const controlHoverColor = hoverColor ? hoverColor : radioColors.hover;
	useEffect(() => {
		if (groupIsChecked !== void 0) setIsChecked(groupIsChecked === value);
		else if (checked !== void 0) setIsChecked(checked);
	}, [checked, groupIsChecked]);
	function handleOnChange(e) {
		groupOnChange?.(e);
		onChange?.(e);
	}
	const aliasedProps = filterUndefined({ "aria-disabled": disabled$1 });
	return /* @__PURE__ */ jsxs(RadioBase, {
		className: cs("vui-radio", className),
		controlHoverColor,
		ref,
		...styles.container,
		...aliasedProps,
		...rest,
		children: [/* @__PURE__ */ jsxs(RadioControl, {
			bg: "white",
			className: "vui-radioControl",
			color,
			focusWithinRing: 3,
			mr: controlMr,
			...controlStyles,
			children: [/* @__PURE__ */ jsx(RadioInput, {
				className: "vui-radioInput",
				onClick: handleOnChange,
				ref: inputRef,
				type: "radio",
				defaultChecked: checked,
				disabled: disabled$1,
				id,
				name,
				required,
				value,
				...inputProps
			}), /* @__PURE__ */ jsx(icon_default, {
				className: "vui-radioIcon",
				h: "100%",
				name: icon,
				w: "100%"
			})]
		}), children ?? (label && /* @__PURE__ */ jsx(t_default, {
			className: "vui-radioLabel",
			lineHeight: "normal",
			...styles.label,
			children: label
		}))]
	});
});
Radio.displayName = "Radio";

//#endregion
export { Radio, RadioBase };
globalThis.__vuiVersion__ = "4.0.0-beta.1"
//# sourceMappingURL=radio.js.map