'use strict';
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
import React from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';
import { Checkbox } from '../../form/Checkbox';
import lang from '../../../../lang/common/index-en_US';
var ListControlCheckbox = /** @class */ (function (_super) {
    __extends(ListControlCheckbox, _super);
    function ListControlCheckbox() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    ListControlCheckbox.prototype.getVisibleItems = function () {
        return this.context.listStore.getPrimaryKeys();
    };
    ListControlCheckbox.prototype.onChange = function () {
        var _this = this;
        var formRecords = this.context.form.value;
        var items = this.getVisibleItems();
        var checked = this.refs.input.getValue();
        var newFormValue = {};
        items.forEach(function (id) {
            var _a;
            newFormValue[id] = _.extend({}, formRecords[id], (_a = {},
                _a[_this.props.dependentFieldName] = checked,
                _a));
        });
        this.context.form.setValue(newFormValue, newFormValue);
    };
    ListControlCheckbox.prototype.render = function () {
        var _this = this;
        var formRecords = this.context.form.value;
        var items = this.getVisibleItems();
        var checkboxValue = false;
        if (items.length > 0) {
            var selectedItems = items.filter(function (id) { return formRecords[id] && formRecords[id][_this.props.dependentFieldName]; });
            if (selectedItems.length === items.length) {
                checkboxValue = true;
            }
            if (selectedItems.length > 0 && selectedItems.length < items.length) {
                checkboxValue = null;
            }
        }
        return (React.createElement(Checkbox, { name: undefined, "aria-checked": checkboxValue === null ? 'mixed' : checkboxValue, checked: checkboxValue, hiddenLabel: this.props.hiddenLabel, disabled: items.length === 0, onChange: this.onChange.bind(this), ref: "input", label: this.props.label, groupClassName: "rc-list-checkbox", "aria-labelledby": this.props.ariaLabelledById, removeName: true }));
    };
    ListControlCheckbox.contextTypes = {
        listStore: PropTypes.object,
        form: PropTypes.object
    };
    ListControlCheckbox.propTypes = {
        label: PropTypes.string,
        dependentFieldName: PropTypes.string.isRequired,
        ariaLabelledById: PropTypes.string,
        hiddenLabel: PropTypes.bool
    };
    ListControlCheckbox.defaultProps = {
        get label() {
            return lang.SELECT_ALL;
        },
        ariaLabelledById: null,
        hiddenLabel: true
    };
    return ListControlCheckbox;
}(React.Component));
export { ListControlCheckbox };
//# sourceMappingURL=ListControlCheckbox.js.map