import { __assign, __extends } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import { PureComponent } from 'react';
import Checkbox from '../checkbox';
import Pop from '../pop';
var SelectionCheckbox = (function (_super) {
    __extends(SelectionCheckbox, _super);
    function SelectionCheckbox(props) {
        var _this = _super.call(this, props) || this;
        _this.subscribe = function () {
            var store = _this.props.store;
            _this.unsubscribe = store.subscribe('selectedRowKeys', function () {
                var checked = _this.getCheckState(_this.props);
                if (_this.state.checked !== checked) {
                    _this.setState({ checked: checked });
                }
            });
        };
        _this.getCheckState = function (props) {
            var _a;
            var store = props.store, rowIndex = props.rowIndex;
            return ((_a = store.getState('selectedRowKeys')) !== null && _a !== void 0 ? _a : []).indexOf(rowIndex) !== -1;
        };
        _this.state = {
            checked: _this.getCheckState(props),
        };
        return _this;
    }
    SelectionCheckbox.prototype.componentDidMount = function () {
        this.subscribe();
    };
    SelectionCheckbox.prototype.componentWillReceiveProps = function (nextProps, nextState) {
        var checked = this.getCheckState(nextProps);
        if (checked !== nextState.checked) {
            this.setState({ checked: checked });
        }
    };
    SelectionCheckbox.prototype.componentWillUnmount = function () {
        if (this.unsubscribe) {
            this.unsubscribe();
        }
    };
    SelectionCheckbox.prototype.render = function () {
        var _a = this.props, onChange = _a.onChange, disabled = _a.disabled, reason = _a.reason;
        var checked = this.state.checked;
        return reason && disabled ? (_jsx(Pop, __assign({ content: reason, trigger: "hover", position: "top-left", centerArrow: true }, { children: _jsx(Checkbox, { onChange: onChange, checked: checked, disabled: disabled }, void 0) }), void 0)) : (_jsx(Checkbox, { onChange: onChange, checked: checked, disabled: disabled }, void 0));
    };
    return SelectionCheckbox;
}(PureComponent));
export default SelectionCheckbox;
