"use strict";
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
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 __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckBoxGroup = void 0;
var react_addons_create_fragment_1 = __importDefault(require("react-addons-create-fragment"));
var react_1 = __importDefault(require("react"));
var bem_react_classname_1 = require("bem-react-classname");
var cn_1 = require("../cn");
/**
 * Компонент группы чекбоксов.
 */
var CheckBoxGroup = /** @class */ (function (_super) {
    __extends(CheckBoxGroup, _super);
    function CheckBoxGroup() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.cn = bem_react_classname_1.createCn('checkbox-group');
        _this.state = {
            value: [],
        };
        _this.handleCheckboxChange = function (value, checked, event) {
            // eslint-disable-next-line react/no-access-state-in-setstate
            var newValue = _this.props.value ? _this.props.value.slice() : _this.state.value.slice();
            var changedValueIndex = newValue.findIndex(function (stateValue) { return stateValue === value; });
            if (checked) {
                newValue.push(value);
            }
            else {
                newValue.splice(changedValueIndex, 1);
            }
            _this.setState({
                value: newValue,
            });
            if (_this.props.onChange) {
                _this.props.onChange(newValue, event);
            }
        };
        _this.handleFocus = function (event) {
            if (_this.props.onFocus) {
                _this.props.onFocus(event);
            }
        };
        _this.handleBlur = function (event) {
            if (_this.props.onBlur) {
                _this.props.onBlur(event);
            }
        };
        return _this;
    }
    CheckBoxGroup.prototype.render = function () {
        var _this = this;
        var children = null;
        var props = { name: this.props.name };
        var checkboxGroupParts = {};
        if (this.props.disabled !== undefined) {
            props.disabled = this.props.disabled;
        }
        if (this.props.children) {
            var propsChildren = this.props.children;
            children = react_1.default.Children.toArray(propsChildren);
        }
        if (this.props.type === 'button') {
            props = __assign(__assign({}, props), { width: this.props.width });
        }
        if (children) {
            this.checkboxes = [];
            var value_1 = this.props.value === undefined ? this.state.value : this.props.value;
            react_1.default.Children.forEach(children, function (checkbox, index) {
                if (react_1.default.isValidElement(checkbox)) {
                    var checkboxNode = react_1.default.cloneElement(checkbox, __assign({ ref: function (checkbox) { return _this.checkboxes.push(checkbox); }, checked: checkbox.props.checked === undefined
                            ? value_1.some(function (groupValue) { return groupValue === checkbox.props.value; })
                            : checkbox.props.checked, onChange: checkbox.props.onChange === undefined
                            ? function (checked, _text, event) { return _this.handleCheckboxChange(checkbox.props.value, checked, event); }
                            : checkbox.props.onChange }, props));
                    checkboxGroupParts["checkbox-" + index] = (_this.props.type !== 'button' && _this.props.type !== 'line')
                        ? react_1.default.createElement("div", null, checkboxNode)
                        : checkboxNode;
                }
            });
        }
        return (react_1.default.createElement("span", { className: this.cn({
                type: this.props.type,
                disabled: props.disabled,
                width: props.width ? props.width : null,
            }) + " control-group", id: this.props.id, role: "group", tabIndex: -1, onFocus: this.handleFocus, onBlur: this.handleBlur, "data-test-id": this.props['data-test-id'] },
            !!this.props.label
                && (react_1.default.createElement("div", { className: this.cn('label') }, this.props.label)),
            react_1.default.createElement("div", { className: this.cn('box') }, react_addons_create_fragment_1.default(checkboxGroupParts))));
    };
    /**
     * Устанавливает фокус на первую чекбокс-кнопку в группе.
     */
    CheckBoxGroup.prototype.focus = function () {
        if (this.checkboxes && this.checkboxes[0]) {
            this.checkboxes[0].focus();
        }
    };
    /**
     * Убирает фокус с группы чекбокс-кнопок.
     */
    // eslint-disable-next-line class-methods-use-this
    CheckBoxGroup.prototype.blur = function () {
        if (document.activeElement instanceof HTMLElement) {
            document.activeElement.blur();
        }
    };
    CheckBoxGroup.defaultProps = {
        type: 'normal',
    };
    return CheckBoxGroup;
}(react_1.default.PureComponent));
exports.CheckBoxGroup = CheckBoxGroup;
exports.default = cn_1.withTheme(CheckBoxGroup);

//# sourceMappingURL=checkbox-group.js.map
