import { __assign, __extends } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import { PureComponent } from 'react';
import Checkbox from '../checkbox';
var SelectionCheckboxAll = (function (_super) {
    __extends(SelectionCheckboxAll, _super);
    function SelectionCheckboxAll(props) {
        var _this = _super.call(this, props) || this;
        _this.subscribe = function () {
            var store = _this.props.store;
            _this.unsubscribe = store.subscribe('selectedRowKeys', function () {
                _this.setCheckState(_this.props);
            });
        };
        _this.getCheckBoxState = function (props, type) {
            var _a;
            var datasets = props.datasets, disabledDatasets = props.disabledDatasets, store = props.store, getDataKey = props.getDataKey;
            var activeDatasets = (datasets === null || datasets === void 0 ? void 0 : datasets.length) ? datasets : disabledDatasets;
            var selectedRowKeys = (_a = store.getState('selectedRowKeys')) !== null && _a !== void 0 ? _a : [];
            if (selectedRowKeys.length === 0)
                return false;
            if (type === 'every') {
                return activeDatasets.every(function (data, index) { return selectedRowKeys.indexOf(getDataKey(data, index)) !== -1; });
            }
            return activeDatasets.some(function (data, index) { return selectedRowKeys.indexOf(getDataKey(data, index)) !== -1; });
        };
        _this.getCheckState = function (props) {
            return _this.getCheckBoxState(props, 'every');
        };
        _this.getIndeterminateState = function (props) {
            return _this.getCheckBoxState(props, 'some');
        };
        _this.setCheckState = function (props) {
            var checked = _this.getCheckState(props);
            var indeterminate = _this.getIndeterminateState(props);
            _this.setState({
                checked: checked,
                indeterminate: indeterminate,
            });
        };
        _this.onChange = function (e) {
            var datasets = _this.props.datasets;
            var checked = e.target.checked;
            _this.props.onSelect(checked ? 'selectAll' : 'removeAll', datasets);
        };
        _this.state = {
            checked: _this.getCheckState(props),
            indeterminate: _this.getIndeterminateState(props),
        };
        return _this;
    }
    SelectionCheckboxAll.prototype.componentDidMount = function () {
        this.subscribe();
    };
    SelectionCheckboxAll.prototype.componentWillReceiveProps = function (nextProps) {
        this.setCheckState(nextProps);
    };
    SelectionCheckboxAll.prototype.componentWillUnmount = function () {
        if (this.unsubscribe) {
            this.unsubscribe();
        }
    };
    SelectionCheckboxAll.prototype.render = function () {
        var _a = this.state, checked = _a.checked, indeterminate = _a.indeterminate;
        var disabled = this.props.disabled;
        var props = {
            checked: checked,
            indeterminate: indeterminate && checked ? false : indeterminate,
        };
        return _jsx(Checkbox, __assign({}, props, { onChange: this.onChange, disabled: disabled }), void 0);
    };
    return SelectionCheckboxAll;
}(PureComponent));
export default SelectionCheckboxAll;
