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 { CheckboxGroupContext } from './checkbox-group-context';
var Checkbox = React.forwardRef(function (props, ref) {
    var _a;
    var _b = props.defaultChecked, defaultChecked = _b === void 0 ? false : _b, _c = props.indeterminate, indeterminate = _c === void 0 ? false : _c, value = props.value, onChange = props.onChange, className = props.className, children = props.children, checkboxRef = props.checkboxRef, customisedCls = props.prefixCls, otherProps = __rest(props, ["defaultChecked", "indeterminate", "value", "onChange", "className", "children", "checkboxRef", "prefixCls"]);
    var configContext = useContext(ConfigContext);
    var checkboxGroupContext = useContext(CheckboxGroupContext);
    var initialChecked = 'checked' in props ? props.checked : defaultChecked;
    var _d = useState('value' in checkboxGroupContext ? checkboxGroupContext.value === value : initialChecked), checked = _d[0], setChecked = _d[1];
    var prefixCls = getPrefixCls('checkbox', configContext.prefixCls, customisedCls);
    var disabled = 'disabled' in props
        ? props.disabled
        : 'disabled' in checkboxGroupContext
            ? checkboxGroupContext.disabled
            : false;
    var cls = classNames(prefixCls, className, (_a = {},
        _a[prefixCls + "_indeterminate"] = indeterminate,
        _a[prefixCls + "_checked"] = checked && !indeterminate,
        _a[prefixCls + "_disabled"] = disabled,
        _a));
    var checkboxOnChange = function (e) {
        if (!disabled) {
            !('checked' in props) && setChecked(e.currentTarget.checked);
            onChange && onChange(e);
            checkboxGroupContext.onChange && checkboxGroupContext.onChange(e);
        }
    };
    useEffect(function () {
        if ('value' in checkboxGroupContext && 'value' in props) {
            setChecked(checkboxGroupContext.value.includes(value));
        }
        'checked' in props && setChecked(props.checked);
    }, [props, checkboxGroupContext, value]);
    return (React.createElement("label", __assign({}, otherProps, { ref: ref, className: cls }),
        React.createElement("input", { ref: checkboxRef, role: "checkbox", "aria-checked": checked, name: value, disabled: disabled, className: prefixCls + "__native", type: "checkbox", checked: checked, onChange: checkboxOnChange }),
        React.createElement("span", { className: prefixCls + "__inner" }),
        React.createElement("span", null, children)));
});
Checkbox.displayName = 'Checkbox';
export default Checkbox;
//# sourceMappingURL=checkbox.js.map