import { __assign, __rest, __spreadArrays } 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 CheckboxGroup = React.forwardRef(function (props, ref) {
    var _a = props.defaultValue, defaultValue = _a === void 0 ? [] : _a, customisedCls = props.prefixCls, onChange = props.onChange, disabled = props.disabled, className = props.className, children = props.children, otherProps = __rest(props, ["defaultValue", "prefixCls", "onChange", "disabled", "className", "children"]);
    var configContext = useContext(ConfigContext);
    var prefixCls = getPrefixCls('checkbox-group', configContext.prefixCls, customisedCls);
    var cls = classNames(prefixCls, className);
    var _b = useState('value' in props ? props.value : defaultValue), value = _b[0], setValue = _b[1];
    var itemOnChange = function (e) {
        if (!disabled) {
            var name_1 = e.currentTarget.name;
            var idx = value.indexOf(name_1);
            if (idx > -1) {
                value.splice(idx, 1);
            }
            else {
                value.push(name_1);
            }
            !('value' in props) && setValue(__spreadArrays(value));
            onChange && onChange(value);
        }
    };
    useEffect(function () {
        'value' in props && setValue(__spreadArrays(props.value));
    }, [props]);
    return (React.createElement(CheckboxGroupContext.Provider, { value: {
            value: value,
            disabled: disabled,
            onChange: itemOnChange,
        } },
        React.createElement("div", __assign({}, otherProps, { ref: ref, role: "group", className: cls }), children)));
});
CheckboxGroup.displayName = 'CheckboxGroup';
export default CheckboxGroup;
//# sourceMappingURL=checkbox-group.js.map