"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.RadioGroup = 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 RadioGroup = /** @class */ (function (_super) {
    __extends(RadioGroup, _super);
    function RadioGroup() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.cn = bem_react_classname_1.createCn('radio-group');
        _this.state = {
            value: '',
        };
        _this.handleRadioChange = function (value) {
            if (_this.state.value !== value) {
                _this.setState({ value: value });
            }
            if (_this.props.value !== value && _this.props.onChange) {
                _this.props.onChange(value);
            }
        };
        _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;
    }
    RadioGroup.prototype.render = function () {
        var _this = this;
        var children = null;
        var _a = this.props, size = _a.size, name = _a.name;
        var props = { name: name };
        var radioGroupParts = {};
        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.radios = [];
            var value_1 = this.props.value === undefined ? this.state.value : this.props.value;
            react_1.default.Children.forEach(children, function (radio, index) {
                if (react_1.default.isValidElement(radio)) {
                    radioGroupParts["radio-" + index] = react_1.default.cloneElement(radio, __assign({ ref: function (radio) { return _this.radios.push(radio); }, error: radio.props.error === undefined ? Boolean(_this.props.error) : radio.props.error, checked: (radio.props.checked === undefined ? (value_1 === radio.props.value) : radio.props.checked), onChange: radio.props.onChange === undefined ? _this.handleRadioChange : radio.props.onChange }, props));
                }
            });
        }
        return (react_1.default.createElement("div", { className: this.cn(__assign({ type: this.props.type, invalid: !!this.props.error, size: size }, props)) + " control-group" + (this.props.error ? ' control-group_invalid' : ''), role: "group", tabIndex: -1, onFocus: this.handleFocus, onBlur: this.handleBlur, "data-test-id": this.props['data-test-id'] },
            react_1.default.createElement("div", { className: this.cn('inner') },
                !!this.props.label
                    && react_1.default.createElement("div", { className: this.cn('top') }, this.props.label),
                react_1.default.createElement("div", { className: this.cn('box') }, react_addons_create_fragment_1.default(radioGroupParts)),
                (this.props.error || this.props.hint)
                    && (react_1.default.createElement("span", { className: this.cn('sub') }, this.props.error || this.props.hint)))));
    };
    /**
     * Устанавливает фокус на первую радиокнопку в группе.
     */
    RadioGroup.prototype.focus = function () {
        if (this.radios && this.radios[0]) {
            this.radios[0].focus();
        }
    };
    /**
     * Убирает фокус с группы радио-кнопок.
     */
    // eslint-disable-next-line class-methods-use-this
    RadioGroup.prototype.blur = function () {
        if (document.activeElement instanceof HTMLElement) {
            document.activeElement.blur();
        }
    };
    RadioGroup.defaultProps = {
        type: 'normal',
        size: 'm',
    };
    return RadioGroup;
}(react_1.default.PureComponent));
exports.RadioGroup = RadioGroup;
exports.default = cn_1.withTheme(RadioGroup);

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