import { __assign, __extends } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import { Component } from 'react';
import classNames from 'classnames';
import memoize from '../utils/memorize-one';
import GroupContext from './GroupContext';
import { DisabledContext } from '../disabled';
var GroupContextProvider = GroupContext.Provider;
var CheckboxGroup = (function (_super) {
    __extends(CheckboxGroup, _super);
    function CheckboxGroup() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.getGroupContext = memoize(function (maybeValue, disabled, readOnly, isValueEqual) {
            var value;
            if (Array.isArray(maybeValue)) {
                value = maybeValue;
            }
            else {
                value = [];
            }
            return {
                value: value,
                disabled: disabled,
                readOnly: readOnly,
                isValueEqual: isValueEqual,
                onChange: _this.onCheckboxChange,
            };
        });
        _this.onCheckboxChange = function (child) {
            var _a = _this.props, isValueEqual = _a.isValueEqual, onChange = _a.onChange, prevValue = _a.value;
            if (!onChange) {
                return;
            }
            var value = prevValue ? prevValue.slice() : [];
            var index = value.findIndex(function (it) { return isValueEqual(it, child); });
            if (index !== -1) {
                value.splice(index, 1);
            }
            else {
                value.push(child);
            }
            onChange(value);
        };
        return _this;
    }
    CheckboxGroup.prototype.render = function () {
        var _a = this.props, className = _a.className, style = _a.style, children = _a.children, value = _a.value, _b = _a.disabled, disabled = _b === void 0 ? this.context.value : _b, _c = _a.readOnly, readOnly = _c === void 0 ? false : _c, isValueEqual = _a.isValueEqual;
        var classString = classNames('zent-checkbox-group', className);
        return (_jsx(GroupContextProvider, __assign({ value: this.getGroupContext(value, disabled, readOnly, isValueEqual) }, { children: _jsx("div", __assign({ className: classString, style: style, "data-zv": 'v10.0.11' }, { children: children }), void 0) }), void 0));
    };
    CheckboxGroup.defaultProps = {
        isValueEqual: Object.is,
        value: [],
    };
    CheckboxGroup.contextType = DisabledContext;
    return CheckboxGroup;
}(Component));
export { CheckboxGroup };
export default CheckboxGroup;
