"use client";
import { useCallback, useRef, useState } from "react";
const noop = ()=>{
// do nothing
};
/**
 * @example Strict Union Example
 * ```tsx
 * type ValidValues = 1 | 2 | 3 | 4 | "" | "a" | "b";
 *
 * const { value, getRadioProps } = useRadioGroup<ValidValues>({
 *   name: "group",
 *   defaultValue: ""
 * });
 *
 *
 * return (
 *   <>
 *     <Radio {...getRadioProps(1)} label="First" />
 *     <Radio {...getRadioProps(2)} label="Second" />
 *     <Radio {...getRadioProps(3)} label="Third" />
 *     <Radio {...getRadioProps(4)} label="Forth" />
 *     <Radio {...getRadioProps("a")} label="Fifth" />
 *     <Radio {...getRadioProps("b")} label="Sixth" />
 *     <Radio {...getRadioProps("c")} label="Seventh" />
 *   </>
 * );
 * ```
 *
 * @see {@link https://react-md.dev/components/radio | Radio Demos}
 * @see {@link https://react-md.dev/hooks/use-radio-group | useRadioGroup Demos}
 * @since 6.0.0
 */ export function useRadioGroup(options) {
    const { name, defaultValue, menu = false, required, onChange = noop, onInvalid = noop } = options;
    const [value, setValue] = useState(()=>{
        if (typeof defaultValue === "function") {
            return defaultValue();
        }
        return defaultValue ?? "";
    });
    const initial = useRef(value);
    const [error, setError] = useState(false);
    return {
        reset: useCallback(()=>{
            setError(false);
            setValue(initial.current);
        }, []),
        value,
        setValue,
        getRadioProps (radioValue) {
            const checked = value === radioValue;
            if (menu) {
                return {
                    checked,
                    onCheckedChange () {
                        setValue(radioValue);
                    }
                };
            }
            return {
                name,
                value: radioValue,
                error,
                checked,
                required,
                onChange (event) {
                    onChange(event);
                    setError(false);
                    setValue(radioValue);
                },
                onInvalid (event) {
                    onInvalid(event);
                    setError(true);
                }
            };
        }
    };
}

//# sourceMappingURL=useRadioGroup.js.map