import { useCallback, useMemo, useRef, useState } from '@byted-doubao-apps/framework';
import { warnUnsupported } from '../base/warn-unsupported.js';
import { RadioGroupRuntimeContext } from './context.js';
import { mapRadioGroupProps, resolveCheckedRadioSelection, resolveGroupSelectedValue, shouldDispatchGroupChange, toRadioGroupChangeEvent } from './map.js';
export function RadioGroup(props) {
    const { children, onChange, ...nonEventProps } = props;
    const { mapped, unsupported, groupId, resolvedDisabled } = mapRadioGroupProps(nonEventProps);
    const checkedSelection = resolveCheckedRadioSelection(children);
    const { hasCheckedRadio, selectedValue: checkedValue, missingValueCount } = checkedSelection;
    warnUnsupported('RadioGroup', unsupported);
    warnCheckedRadioValueMissing(missingValueCount);
    const [uncontrolledValue, setUncontrolledValue] = useState(checkedValue);
    const radioChangeHandlersRef = useRef(new Map());
    const selectedValue = resolveGroupSelectedValue(hasCheckedRadio, checkedValue, uncontrolledValue);
    const registerRadioOnChange = useCallback((value, handler) => {
        const handlerMap = radioChangeHandlersRef.current;
        const bucket = handlerMap.get(value) ?? new Set();
        bucket.add(handler);
        handlerMap.set(value, bucket);
        return () => {
            const current = handlerMap.get(value);
            if (!current) {
                return;
            }
            current.delete(handler);
            if (current.size === 0) {
                handlerMap.delete(value);
            }
        };
    }, []);
    const triggerRadioChange = useCallback((value) => {
        const handlers = radioChangeHandlersRef.current.get(value);
        if (!handlers) {
            return;
        }
        handlers.forEach((handler) => {
            handler();
        });
    }, []);
    const selectValue = useCallback((nextValue) => {
        if (resolvedDisabled || !shouldDispatchGroupChange(selectedValue, nextValue)) {
            return;
        }
        if (!hasCheckedRadio) {
            setUncontrolledValue(nextValue);
        }
        onChange?.(toRadioGroupChangeEvent({ value: nextValue }, 'change', groupId));
        triggerRadioChange(nextValue);
    }, [groupId, hasCheckedRadio, onChange, resolvedDisabled, selectedValue, triggerRadioChange]);
    const contextValue = useMemo(() => ({
        groupId,
        selectedValue,
        disabled: resolvedDisabled,
        selectValue,
        registerRadioOnChange
    }), [groupId, registerRadioOnChange, resolvedDisabled, selectValue, selectedValue]);
    const lynxGroupProps = {
        disabled: resolvedDisabled,
        onValueChange: selectValue,
        ...(selectedValue !== null ? { value: selectedValue } : {})
    };
    return (<RadioGroupRuntimeContext.Provider value={contextValue}>
      <view {...mapped}>
        <radio-group {...lynxGroupProps}>{children}</radio-group>
      </view>
    </RadioGroupRuntimeContext.Provider>);
}
let hasWarnedCheckedRadioValueMissing = false;
function warnCheckedRadioValueMissing(missingValueCount) {
    if (missingValueCount === 0 || isProduction() || hasWarnedCheckedRadioValueMissing) {
        return;
    }
    hasWarnedCheckedRadioValueMissing = true;
    // eslint-disable-next-line no-console
    console.warn('[components-taro] Radio with checked=true inside RadioGroup requires a string value.');
}
function isProduction() {
    return (globalThis.process?.env?.NODE_ENV === 'production');
}
//# sourceMappingURL=index.jsx.map