import { t as uuid } from "./uuid.js";
import { i as FormSize, n as AnalyticsId, r as FormOnColor } from "./constants2.js";
import { t as usePseudoClasses } from "./usePseudoClasses.js";
import { n as mergeRefs, t as isMutableRefObject } from "./refs.js";
import { t as useIdWithFallback } from "./useIdWithFallback.js";
import { t as getAriaDescribedBy } from "./accessibility.js";
import { t as ErrorWrapper_default } from "./ErrorWrapper.js";
import { r as renderLabelAsParent, t as getLabelText } from "./utils2.js";
import classNames from "classnames";
import { useState } from "react";
import { jsx } from "react/jsx-runtime";
import radioButtonStyles from "./components/RadioButton/styles.module.scss";
const RadioButton = (props) => {
	const { className, defaultChecked, onChange, disabled, label, inputId = uuid(), onColor = FormOnColor.onwhite, name = inputId, size, errorText, errorTextId: errorTextIdProp, error = !!errorText, errorWrapperClassName, value = getLabelText(label), testId, required, labelClassNames, ref, ...rest } = props;
	const invalid = error || onColor === FormOnColor.oninvalid;
	const onDark = onColor === FormOnColor.ondark;
	const onBlueberry = onColor === FormOnColor.onblueberry;
	const onCherry = onColor === FormOnColor.oninvalid;
	const isLarge = size === FormSize.large;
	const [checked, changeChecked] = useState();
	const { refObject, isFocused } = usePseudoClasses(isMutableRefObject(ref) ? ref : null);
	const mergedRefs = mergeRefs([ref, refObject]);
	const errorTextId = useIdWithFallback(errorTextIdProp);
	const radioButtonWrapperClasses = classNames(radioButtonStyles["radio-button-wrapper"], {
		[radioButtonStyles["radio-button-wrapper__large"]]: isLarge,
		[radioButtonStyles["radio-button-wrapper__large--focused"]]: isLarge && isFocused,
		[radioButtonStyles["radio-button-wrapper__large--selected"]]: isLarge && checked && isFocused,
		[radioButtonStyles["radio-button-wrapper__large--invalid"]]: isLarge && onCherry && isFocused,
		[radioButtonStyles["radio-button-wrapper__large--on-blueberry"]]: isLarge && onBlueberry && isFocused
	});
	const radioButtonLabelClasses = classNames(radioButtonStyles["radio-button-label"], {
		[radioButtonStyles["radio-button-label--disabled"]]: disabled,
		[radioButtonStyles["radio-button-label--on-dark"]]: onDark,
		[radioButtonStyles["radio-button-label--invalid"]]: invalid,
		[radioButtonStyles["radio-button-label__large"]]: isLarge,
		[radioButtonStyles["radio-button-label__large--focused"]]: isFocused && isLarge,
		[radioButtonStyles["radio-button-label__large--disabled"]]: isLarge && disabled
	}, labelClassNames);
	const radioButtonClasses = classNames(radioButtonStyles["radio-button"], {
		[radioButtonStyles["radio-button--on-dark"]]: onDark,
		[radioButtonStyles["radio-button--disabled"]]: disabled,
		[radioButtonStyles["radio-button--on-blueberry"]]: onBlueberry,
		[radioButtonStyles["radio-button--invalid"]]: invalid,
		[radioButtonStyles["radio-button__large"]]: isLarge,
		[radioButtonStyles["radio-button__large--disabled"]]: isLarge && disabled,
		[radioButtonStyles["radio-button__large--invalid"]]: isLarge && invalid
	}, className);
	const change = (e) => {
		changeChecked(e.target.checked);
		if (onChange) onChange(e);
	};
	const getLabelContent = () => /* @__PURE__ */ jsx("input", {
		...rest,
		id: inputId,
		name,
		className: radioButtonClasses,
		type: "radio",
		disabled,
		value,
		ref: mergedRefs,
		defaultChecked,
		"aria-describedby": getAriaDescribedBy(props, errorTextId),
		required,
		onChange: (e) => change(e)
	});
	return /* @__PURE__ */ jsx(ErrorWrapper_default, {
		className: errorWrapperClassName,
		errorText,
		errorTextId,
		children: /* @__PURE__ */ jsx("div", {
			"data-testid": testId,
			"data-analyticsid": AnalyticsId.RadioButton,
			className: radioButtonWrapperClasses,
			children: renderLabelAsParent({
				label,
				children: getLabelContent(),
				inputId,
				onColor,
				labelClassName: radioButtonLabelClasses,
				sublabelWrapperClassName: radioButtonStyles["radiobutton-sublabel-wrapper"],
				large: isLarge,
				afterLabelChildrenClassName: radioButtonStyles["radiobutton-afterlabelchildren-wrapper"]
			})
		})
	});
};
RadioButton.displayName = "RadioButton";
var RadioButton_default = RadioButton;
const getRadioLabelClasses = (radioId, onColor, large, checkedRadioId) => {
	const onCherry = onColor === "oninvalid";
	const checked = radioId === checkedRadioId;
	return classNames({
		[radioButtonStyles["radio-button-label__large--on-grey"]]: large && onColor === "ongrey" && !checked,
		[radioButtonStyles["radio-button-label__large--on-blueberry"]]: onColor === "onblueberry" && !checked && large,
		[radioButtonStyles["radio-button-label__large--selected"]]: large && checked && !onCherry,
		[radioButtonStyles["radio-button-label__large--selected-invalid"]]: large && checked && onCherry
	});
};
export { RadioButton as n, RadioButton_default as r, getRadioLabelClasses as t };

//# sourceMappingURL=utils3.js.map