"use client";
/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../../../_virtual/_rolldown/runtime.cjs');
const require_components_client_Radio_RadioGroupStore = require('./RadioGroupStore.cjs');
let react = require("react");

//#region src/components/client/Radio/useRadioGroup.ts
const useRadioGroupName = (name) => {
	const uid = (0, react.useId)();
	return name ?? `uds-radio-group-${uid}`;
};
/**
* A hook that connects a single Radio to its RadioGroupStore. If the Radio is used within a RadioGroupProvider,
* it will use the context store. Otherwise, it will use a shared global store across all un-grouped Radios by name.
*
* @param name The name of the Radio which should be shared across the group.
* @param value The value of the Radio.
* @param checked Whether the Radio is checked by its controlled prop.
* @param defaultChecked The uncontrolled default checked state of the Radio.
* @returns The name of the RadioGroup, whether the Radio is controlled, the checked state of the Radio, and a function to set the RadioGroup value.
*/
const useRadioGroup = ({ name: radioName, value: radioValue, checked: radioIsChecked, defaultChecked: radioDefaultIsChecked }) => {
	const ctx = (0, react.useContext)(require_components_client_Radio_RadioGroupStore.RadioGroupContext);
	const hasRadioGroupProvider = ctx instanceof require_components_client_Radio_RadioGroupStore.RadioGroupStore;
	const isControlled = radioIsChecked !== void 0 || hasRadioGroupProvider;
	const uniqueName = useRadioGroupName(radioName);
	const store = (0, react.useMemo)(() => {
		if (hasRadioGroupProvider) return ctx;
		return require_components_client_Radio_RadioGroupStore.RadioGroupStore.findOrCreate(ctx, uniqueName);
	}, [
		ctx,
		hasRadioGroupProvider,
		uniqueName
	]);
	const [radioGroupValue, setInternalRadioGroupValue] = (0, react.useState)(store.value);
	const name = store.name ?? uniqueName;
	const setGroupValue = (0, react.useCallback)((value) => {
		store.setValue(value);
		if (isControlled) return;
		setInternalRadioGroupValue(value);
	}, [isControlled, store]);
	(0, react.useEffect)(() => store.onChange(setInternalRadioGroupValue), [store]);
	(0, react.useEffect)(() => {
		if (radioIsChecked !== true) return;
		setGroupValue(radioValue);
	}, [
		radioIsChecked,
		radioValue,
		setGroupValue
	]);
	(0, react.useEffect)(() => {
		if (hasRadioGroupProvider) return;
		const store = require_components_client_Radio_RadioGroupStore.RadioGroupStore.findOrCreate(ctx, name);
		store.activeCount += 1;
		ctx.set(name, store);
		return () => {
			const entry = ctx.get(name);
			if (!entry) return;
			entry.activeCount -= 1;
			if (entry.activeCount === 0) ctx.delete(name);
		};
	}, [
		ctx,
		hasRadioGroupProvider,
		name
	]);
	return {
		name,
		isControlled,
		checked: (() => {
			if (isControlled && !hasRadioGroupProvider) return radioIsChecked;
			if (radioGroupValue !== void 0) return radioValue === radioGroupValue;
			return radioDefaultIsChecked ?? false;
		})(),
		setGroupValue
	};
};

//#endregion
exports.useRadioGroup = useRadioGroup;
exports.useRadioGroupName = useRadioGroupName;