import { __assign, __rest } from "tslib";
import React, { useState, useEffect, useContext } from 'react';
import classNames from 'classnames';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
import { RadioGroupContext } from './radio-group-context';
var Radio = React.forwardRef(function (props, ref) {
    var _a;
    var _b = props.defaultChecked, defaultChecked = _b === void 0 ? false : _b, radioRef = props.radioRef, name = props.name, value = props.value, onChange = props.onChange, className = props.className, children = props.children, customisedCls = props.prefixCls, otherProps = __rest(props, ["defaultChecked", "radioRef", "name", "value", "onChange", "className", "children", "prefixCls"]);
    var configContext = useContext(ConfigContext);
    var radioGroupContext = useContext(RadioGroupContext);
    var initialChecked = 'checked' in props ? props.checked : defaultChecked;
    var _c = useState('value' in radioGroupContext ? radioGroupContext.value === value : initialChecked), checked = _c[0], setChecked = _c[1];
    var prefixCls = getPrefixCls('radio', configContext.prefixCls, customisedCls);
    var disabled = 'disabled' in props
        ? props.disabled
        : 'disabled' in radioGroupContext
            ? radioGroupContext.disabled
            : false;
    var cls = classNames(prefixCls, className, (_a = {},
        _a[prefixCls + "_checked"] = checked,
        _a[prefixCls + "_disabled"] = disabled,
        _a));
    var radioOnChange = function (e) {
        if (!disabled) {
            !('checked' in props) && setChecked(e.currentTarget.checked);
            onChange && onChange(e);
            radioGroupContext.onChange && radioGroupContext.onChange(e);
        }
    };
    useEffect(function () {
        'value' in radioGroupContext && setChecked(value === radioGroupContext.value);
        'checked' in props && setChecked(props.checked);
    }, [props, radioGroupContext, value]);
    return (React.createElement("label", __assign({}, otherProps, { ref: ref, className: cls }),
        React.createElement("input", { ref: radioRef, role: "radio", "aria-checked": checked, name: radioGroupContext.name || name, disabled: disabled, value: value, className: prefixCls + "__native", type: "radio", checked: checked, onChange: radioOnChange }),
        React.createElement("span", { className: prefixCls + "__inner" }),
        React.createElement("span", null, children)));
});
Radio.displayName = 'Radio';
export default Radio;
//# sourceMappingURL=radio.js.map