import { __assign } from "tslib";
import { useCallback, useRef } from 'react';
function makeEvent(event, props) {
    var e = Object.create(event);
    e.target = __assign(__assign({}, props), { type: 'radio', checked: event.target.checked });
    return e;
}
export function useRadioHandler(ctx, props) {
    var propsRef = useRef(props);
    propsRef.current = props;
    var cxOnChange = ctx && ctx.onRadioChange;
    return useCallback(function (event) {
        var e = makeEvent(event, propsRef.current);
        if (cxOnChange) {
            cxOnChange(e);
        }
        else {
            var onChange = propsRef.current.onChange;
            onChange && onChange(e);
        }
    }, [cxOnChange]);
}
function getDisabled(disabledCtx, groupCtx, props) {
    if (typeof props.disabled === 'boolean') {
        return props.disabled;
    }
    if (groupCtx && typeof groupCtx.disabled === 'boolean') {
        return groupCtx.disabled;
    }
    return disabledCtx.value;
}
function getReadOnly(groupCtx, props) {
    if (typeof props.readOnly === 'boolean') {
        return props.readOnly;
    }
    if (groupCtx) {
        return groupCtx.readOnly;
    }
    return false;
}
export function getRadioState(disabledCtx, groupCtx, props) {
    var disabled = getDisabled(disabledCtx, groupCtx, props);
    var readOnly = getReadOnly(groupCtx, props);
    var checked;
    if (groupCtx) {
        checked = groupCtx.isValueEqual(groupCtx.value, props.value);
    }
    else {
        checked = !!props.checked;
    }
    return {
        checked: checked,
        disabled: disabled,
        readOnly: readOnly,
    };
}
