"use client";
/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { createSlot } from "../../../utils/createSlot.js";
import { RadioGroupContext, RadioGroupStore } from "./RadioGroupStore.js";
import { useRadioGroupName } from "./useRadioGroup.js";
import React, { forwardRef, useEffect, useMemo } from "react";
import { jsx } from "react/jsx-runtime";

//#region src/components/client/Radio/RadioGroupProvider.tsx
const Slot = createSlot();
/**
* A custom RadioGroupStore that extends the default RadioGroupStore to allow for controlled RadioGroups.
*/
var RadioGroupProviderStore = class extends RadioGroupStore {
	onChangeCb;
	isControlled;
	constructor(name, isControlled) {
		super(name);
		this.isControlled = isControlled;
	}
	setValue(value, force = false) {
		if (!this.isControlled || force) {
			super.setValue(value);
			return;
		}
		this.onChangeCb?.(value);
	}
};
/**
* **⚙️️ A RadioGroup that improves accessibility and provides a central component for state manangement.
*
* @componentType Client component
*
* @description
* Provides a radio group
*
* @see
* Check out the {@link https://uds.build/docs/components/radio Radio Docs} for more info
*
* @usage
* - Forms: RadioGroup
*
* @example
* ```tsx
* 'use client';
* import { RadioGroup } from "@yahoo/uds";
*
* <RadioGroup name="items" value="1">
*   <Radio value="1" label="One" />
*   <Radio value="2" label="Two" />
* </RadioGroup>
*```
*
* @related [Radio](https://uds.build/docs/components/radio).
**/
const RadioGroupProvider = forwardRef(function RadioGroupProvider({ asChild, name, value: valueProp, defaultValue, onChange, ...props }, ref) {
	const isControlled = valueProp !== void 0;
	const localName = useRadioGroupName(name);
	const store = useMemo(() => {
		const s = new RadioGroupProviderStore(localName, isControlled);
		s.value = isControlled ? valueProp : defaultValue;
		return s;
	}, [
		defaultValue,
		isControlled,
		localName,
		valueProp
	]);
	useEffect(() => {
		store.onChangeCb = onChange;
	}, [onChange, store]);
	useEffect(() => {
		if (!isControlled) return;
		store.setValue(valueProp, true);
	}, [
		isControlled,
		store,
		valueProp
	]);
	useEffect(() => {
		return store.onChange((value) => onChange?.(value));
	}, [onChange, store]);
	const Comp = asChild ? Slot : "div";
	return /* @__PURE__ */ jsx(RadioGroupContext.Provider, {
		value: store,
		children: /* @__PURE__ */ jsx(Comp, {
			role: "radiogroup",
			ref,
			...props
		})
	});
});
RadioGroupProvider.displayName = "RadioGroupProvider";

//#endregion
export { RadioGroupProvider };