'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 __());
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var Row_1 = __importDefault(require("react-bootstrap/lib/Row"));
var Col_1 = __importDefault(require("react-bootstrap/lib/Col"));
var lodash_1 = __importDefault(require("lodash"));
var classnames_1 = __importDefault(require("classnames"));
var Checkbox_1 = require("./Checkbox");
var Form_1 = require("./Form");
var Icon_1 = require("../Icon");
var CheckboxGroup = /** @class */ (function (_super) {
    __extends(CheckboxGroup, _super);
    function CheckboxGroup(props, context) {
        var _this = _super.call(this, props, context) || this;
        _this.state = {
            opened: true
        };
        return _this;
    }
    CheckboxGroup.prototype.getMainCheckbox = function () {
        var _this = this;
        return (react_1.default.createElement(Checkbox_1.Checkbox, { name: undefined, readOnly: this.extractFlatProperty('readOnly'), disabled: this.extractFlatProperty('disabled'), checked: this.getMainCheckboxValue(), onChange: function (e) { return _this.onMainCheckboxClick(!!e.target.checked); }, label: this.props.title }));
    };
    CheckboxGroup.prototype.getMainCheckboxValue = function () {
        var value = this.getInputValue();
        var selectedCheckboxes = react_1.default.Children.toArray(this.props.children).filter(function (checkbox) { return value[checkbox.props.name]; });
        return this.calculateMainCheckboxValue(selectedCheckboxes.length, this.props.children.length);
    };
    CheckboxGroup.prototype.calculateMainCheckboxValue = function (selectedCheckboxesCount, totalCheckboxesCount) {
        var mainCheckboxValue = null; // indeterminate by default
        if (selectedCheckboxesCount === totalCheckboxesCount) {
            mainCheckboxValue = true;
        }
        if (selectedCheckboxesCount === 0) {
            mainCheckboxValue = false;
        }
        return mainCheckboxValue;
    };
    CheckboxGroup.prototype.onMainCheckboxClick = function (checked) {
        var value = lodash_1.default.extend({}, this.getInputValue());
        var diff = {};
        react_1.default.Children.toArray(this.props.children).forEach(function (checkbox) {
            if (checkbox.props.readOnly) {
                return;
            }
            var id = checkbox.props.name;
            if (id in value) {
                if (value[id] !== checked) {
                    diff[id] = checked;
                }
                value[id] = checked;
            }
        });
        this.setOutputValue(value, diff);
    };
    CheckboxGroup.prototype.checkboxesPanelToggleHandler = function () {
        this.setState({ opened: !this.state['opened'] });
    };
    CheckboxGroup.prototype.render = function () {
        var checkboxGroupClassNames = classnames_1.default('rc-checkbox-group', {
            opened: this.state['opened'],
            collapsed: !this.state['opened']
        });
        var iconName = this.state['opened'] ? 'chevron-up-big' : 'chevron-down-big';
        return (react_1.default.createElement("div", { className: checkboxGroupClassNames, "data-test-automation-id": this.props.testAutomationId },
            react_1.default.createElement(Row_1.default, { className: "rc-checkbox-group-header" },
                react_1.default.createElement(Col_1.default, { xs: 6 }, this.getMainCheckbox()),
                react_1.default.createElement(Col_1.default, { xs: 6, className: "rc-checkbox-group-icon", onClick: this.checkboxesPanelToggleHandler.bind(this) },
                    react_1.default.createElement(Icon_1.Icon, { name: iconName, className: "pull-right" }))),
            react_1.default.createElement(Row_1.default, null,
                react_1.default.createElement(Col_1.default, { xs: 12, className: "rc-checkbox-group-body", "data-test-automation-id": "list" }, this.props.children))));
    };
    return CheckboxGroup;
}(Form_1.Form));
exports.CheckboxGroup = CheckboxGroup;
//# sourceMappingURL=CheckboxGroup.js.map