import { __assign, __extends } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { PureComponent } from 'react';
import classnames from 'classnames';
import SelectionCheckboxAll from './SelectionCheckboxAll';
import uniq from '../utils/uniq';
import { getCompatSelectionPropsFn, getSelectAllCheckboxState } from './utils';
var BatchComponents = (function (_super) {
    __extends(BatchComponents, _super);
    function BatchComponents() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            selectedRows: [],
            batchNeedRenderFixed: false,
        };
        _this.getSelectionPropsByItem = function (data, rowIndex) {
            var _a = _this.props, selection = _a.selection, selectionPropsCache = _a.selectionPropsCache;
            var getSelectionProps = getCompatSelectionPropsFn(selection);
            if (!getSelectionProps) {
                return {};
            }
            if (!selectionPropsCache[rowIndex]) {
                selectionPropsCache[rowIndex] = getSelectionProps(data);
            }
            return selectionPropsCache[rowIndex] || {};
        };
        _this.getData = function () {
            var _a = _this.props, datasets = _a.datasets, getDataKey = _a.getDataKey, selection = _a.selection;
            if (!selection) {
                return datasets;
            }
            return (datasets || []).filter(function (item, index) {
                var _a, _b;
                var rowIndex = getDataKey(item, index);
                return !((_b = (_a = _this.getSelectionPropsByItem(item, rowIndex)) === null || _a === void 0 ? void 0 : _a.disabled) !== null && _b !== void 0 ? _b : false);
            });
        };
        _this.getCheckboxAllDisabled = function () {
            var _a = _this.props, getDataKey = _a.getDataKey, datasets = _a.datasets;
            return datasets.every(function (item, index) {
                var rowIndex = getDataKey(item, index);
                return _this.getSelectionPropsByItem(item, rowIndex).disabled;
            });
        };
        _this.getSelectedRows = function () {
            var store = _this.props.store;
            var selectedRowKeys = store.getState('selectedRowKeys') || [];
            var prevSelectedRows = store.getState('selectedRows') || [];
            var _a = _this.props, datasets = _a.datasets, getDataKey = _a.getDataKey, rowKey = _a.rowKey;
            var selectedRows = (uniq(datasets.concat(prevSelectedRows), rowKey) || []).filter(function (row, i) { return selectedRowKeys.indexOf(getDataKey(row, i)) > -1; });
            store.setState({
                selectedRows: selectedRows,
            });
            return selectedRows;
        };
        _this.subscribe = function () {
            var store = _this.props.store;
            _this.unsubscribe = store.subscribe('selectedRowKeys', function () {
                store.setState({
                    selectedRows: _this.getSelectedRows(),
                });
            });
            _this.unsubscribeBatchRenderFixed = store.subscribe('batchRenderFixed', function () {
                var selectedRows = store.getState('selectedRows') || [];
                _this.setState({
                    batchNeedRenderFixed: store.getState('batchRenderFixed') && selectedRows.length > 0,
                });
            });
        };
        return _this;
    }
    BatchComponents.prototype.componentDidMount = function () {
        this.props.store.setState({
            selectedRows: this.getSelectedRows(),
        });
        this.subscribe();
    };
    BatchComponents.prototype.componentWillUnmount = function () {
        if (this.unsubscribe) {
            this.unsubscribe();
        }
        if (this.unsubscribeBatchRenderFixed) {
            this.unsubscribeBatchRenderFixed();
        }
    };
    BatchComponents.prototype.render = function () {
        var _a;
        var _b = this.props, prefix = _b.prefix, onSelect = _b.onSelect, store = _b.store, getDataKey = _b.getDataKey, batchRender = _b.batchRender, selection = _b.selection, position = _b.position, datasets = _b.datasets;
        if (!batchRender || !selection) {
            return null;
        }
        var selectedRows = store.getState('selectedRows') || [];
        var batchTree = batchRender(selectedRows, position);
        if (batchTree === null) {
            return null;
        }
        var batchNeedRenderFixed = this.state.batchNeedRenderFixed;
        var batchRenderFixedStyles = store.getState('batchRenderFixedStyles');
        var className = classnames(prefix + "-grid-batch", prefix + "-grid-batch__" + position, (_a = {},
            _a[prefix + "-grid-batch--fixed"] = batchNeedRenderFixed && position === 'foot',
            _a));
        var selectAllState = getSelectAllCheckboxState(datasets !== null && datasets !== void 0 ? datasets : [], getDataKey, this.getSelectionPropsByItem);
        var styles = batchNeedRenderFixed ? batchRenderFixedStyles : {};
        var isSingleSelection = selection.isSingleSelection;
        return (_jsxs("div", __assign({ className: className, style: styles, "data-zv": 'v10.0.11' }, { children: [!isSingleSelection && (_jsx(SelectionCheckboxAll, { getDataKey: getDataKey, onSelect: onSelect, store: store, disabled: selectAllState.allDisabled, datasets: selectAllState.enabledRows, disabledDatasets: selectAllState.disabledRows }, void 0)), batchTree] }), void 0));
    };
    return BatchComponents;
}(PureComponent));
export default BatchComponents;
