/**
 * DevExtreme (esm/renovation/ui/grids/data_grid_next/selection/select_all_checkbox.js)
 * Version: 22.1.3 (build 22180-0310)
 * Build date: Wed Jun 29 2022
 *
 * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import _extends from "@babel/runtime/helpers/esm/extends";
import {
    createComponentVNode
} from "inferno";
import {
    InfernoEffect,
    InfernoComponent
} from "@devextreme/runtime/inferno";
import {
    PluginsContext
} from "../../../../utils/plugin/context";
import {
    CheckBox
} from "../../../editors/check_box/check_box";
import {
    ClearSelection,
    SelectableCount,
    SelectAll,
    SelectedCount
} from "./plugins";
import CLASSES from "../classes";
export var viewFunction = viewModel => createComponentVNode(2, CheckBox, {
    className: "".concat(CLASSES.selectCheckbox, " ").concat(CLASSES.checkboxSize),
    value: viewModel.value,
    valueChange: viewModel.onValueChange
});
export var SelectAllCheckboxProps = {};
export class SelectAllCheckbox extends InfernoComponent {
    constructor(props) {
        super(props);
        this.state = {
            selectedCount: 0,
            selectableCount: 0
        };
        this.updateSelectableCount = this.updateSelectableCount.bind(this);
        this.updateSelectedCount = this.updateSelectedCount.bind(this);
        this.onValueChange = this.onValueChange.bind(this)
    }
    get plugins() {
        if ("PluginsContext" in this.context) {
            return this.context.PluginsContext
        }
        return PluginsContext
    }
    createEffects() {
        return [new InfernoEffect(this.updateSelectableCount, [this.plugins]), new InfernoEffect(this.updateSelectedCount, [this.plugins])]
    }
    updateEffects() {
        var _this$_effects$, _this$_effects$2;
        null === (_this$_effects$ = this._effects[0]) || void 0 === _this$_effects$ ? void 0 : _this$_effects$.update([this.plugins]);
        null === (_this$_effects$2 = this._effects[1]) || void 0 === _this$_effects$2 ? void 0 : _this$_effects$2.update([this.plugins])
    }
    updateSelectableCount() {
        return this.plugins.watch(SelectableCount, count => {
            this.setState(__state_argument => ({
                selectableCount: count
            }))
        })
    }
    updateSelectedCount() {
        return this.plugins.watch(SelectedCount, count => {
            this.setState(__state_argument => ({
                selectedCount: count
            }))
        })
    }
    get value() {
        if (0 === this.state.selectedCount) {
            return false
        }
        if (this.state.selectedCount === this.state.selectableCount) {
            return true
        }
        return null
    }
    onValueChange(value) {
        if (value) {
            this.plugins.callAction(SelectAll)
        } else {
            this.plugins.callAction(ClearSelection)
        }
    }
    get restAttributes() {
        var _this$props = this.props,
            restProps = _extends({}, _this$props);
        return restProps
    }
    render() {
        var props = this.props;
        return viewFunction({
            props: _extends({}, props),
            selectedCount: this.state.selectedCount,
            selectableCount: this.state.selectableCount,
            plugins: this.plugins,
            value: this.value,
            onValueChange: this.onValueChange,
            restAttributes: this.restAttributes
        })
    }
}
SelectAllCheckbox.defaultProps = SelectAllCheckboxProps;
