"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Radio = void 0;
var react_1 = __importDefault(require("react"));
var bem_react_classname_1 = require("bem-react-classname");
var cn_1 = require("../cn");
var tag_button_1 = __importDefault(require("../tag-button/tag-button"));
var scroll_to_1 = __importDefault(require("../lib/scroll-to"));
var vars_1 = require("../vars");
/**
 * Компонент радио-кнопки.
 */
var Radio = /** @class */ (function (_super) {
    __extends(Radio, _super);
    function Radio() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.cn = bem_react_classname_1.createCn('radio');
        _this.state = {
            focused: false,
            hovered: false,
            checked: false,
        };
        _this.handleInputControlClick = function (event) {
            event.stopPropagation();
        };
        _this.handleChange = function (event) {
            if (!_this.props.disabled) {
                // eslint-disable-next-line react/no-access-state-in-setstate
                var nextCheckedValue = !(_this.props.checked === undefined ? _this.state.checked : _this.props.checked);
                _this.setState({ checked: nextCheckedValue });
                if (_this.props.onChange) {
                    _this.props.onChange(_this.props.value, nextCheckedValue, event);
                }
            }
        };
        _this.handleFocus = function (event) {
            if (!_this.props.disabled) {
                _this.setState({ focused: true });
            }
            if (_this.props.type !== 'button') {
                // eslint-disable-next-line no-param-reassign
                event.target.value = _this.props.value;
            }
            if (_this.props.onFocus) {
                _this.props.onFocus(event);
            }
        };
        _this.handleUnfocus = function () { return setImmediate(function () { return _this.setState({ focused: false }); }); };
        _this.handleBlur = function (event) {
            if (!_this.props.disabled) {
                _this.setState({ focused: false });
            }
            if (_this.props.type !== 'button') {
                // eslint-disable-next-line no-param-reassign
                event.target.value = _this.props.value;
            }
            if (_this.props.onBlur) {
                _this.props.onBlur(event);
            }
        };
        _this.handleMouseEnter = function (event) {
            if (!_this.props.disabled) {
                _this.setState({ hovered: true });
            }
            if (_this.props.onMouseEnter) {
                _this.props.onMouseEnter(event);
            }
        };
        _this.handleMouseLeave = function (event) {
            if (!_this.props.disabled) {
                _this.setState({ hovered: false });
            }
            if (_this.props.onMouseLeave) {
                _this.props.onMouseLeave(event);
            }
        };
        return _this;
    }
    Radio.prototype.render = function () {
        var _this = this;
        var checked = this.props.checked === undefined ? this.state.checked : this.props.checked;
        return (
        // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
        react_1.default.createElement("label", { className: this.cn({
                size: this.props.size,
                disabled: this.props.disabled,
                checked: checked,
                focused: this.state.focused,
                hovered: this.state.hovered,
                pressed: this.state.pressed,
                invalid: !!this.props.error,
                width: this.props.type === 'button' ? this.props.width : null,
            }), htmlFor: this.props.id, tabIndex: this.props.tabIndex, onFocus: this.handleFocus, onBlur: this.handleBlur, onMouseEnter: this.handleMouseEnter, onMouseLeave: this.handleMouseLeave, onMouseDown: this.handleUnfocus, onMouseUp: this.handleUnfocus, ref: function (label) {
                _this.label = label;
            }, "data-test-id": this.props['data-test-id'] }, this.props.type === 'button'
            ? this.renderButtonRadio(checked, tag_button_1.default)
            : this.renderNormalRadio(checked)));
    };
    Radio.prototype.renderNormalRadio = function (checked) {
        var _this = this;
        return [
            react_1.default.createElement("span", { className: this.cn('box'), key: 0 },
                react_1.default.createElement("input", { checked: checked, disabled: this.props.disabled, name: this.props.name, id: this.props.id, value: this.props.value, autoComplete: "off", tabIndex: -1, type: "radio", className: this.cn('control'), ref: function (control) {
                        _this.control = control;
                    }, onClick: this.handleInputControlClick, onChange: this.handleChange })),
            this.props.text && (react_1.default.createElement("span", { className: this.cn('text'), role: "presentation", key: 1 }, this.props.text)),
        ];
    };
    Radio.prototype.renderButtonRadio = function (checked, TagButton) {
        var _this = this;
        return (react_1.default.createElement("div", null,
            react_1.default.createElement(TagButton, { togglable: "check", checked: checked, disabled: this.props.disabled, size: this.props.size, width: this.props.width, focused: this.state.focused, hovered: this.state.hovered, tabIndex: -1, onClick: this.handleChange }, this.props.text ? this.props.text : ''),
            react_1.default.createElement("input", { checked: checked, disabled: this.props.disabled, name: this.props.name, id: this.props.id, value: this.props.value, autoComplete: "off", tabIndex: -1, type: "radio", className: this.cn('control'), onChange: this.handleChange, ref: function (control) {
                    _this.control = control;
                } })));
    };
    /**
     * Устанавливает фокус на радио-кнопку.
     */
    Radio.prototype.focus = function () {
        this.control.focus();
    };
    /**
     * Убирает фокус с радио-кнопки.
     */
    // eslint-disable-next-line class-methods-use-this
    Radio.prototype.blur = function () {
        if (document.activeElement) {
            document.activeElement.blur();
        }
    };
    /**
     * Скроллит страницу до радио-кнопки.
     */
    Radio.prototype.scrollTo = function () {
        var elementRect = this.label.getBoundingClientRect();
        scroll_to_1.default({
            // eslint-disable-next-line no-mixed-operators
            targetY: elementRect.top + window.pageYOffset - vars_1.SCROLL_TO_CORRECTION,
        });
    };
    Radio.defaultProps = {
        size: 'm',
        tabIndex: 0,
    };
    return Radio;
}(react_1.default.PureComponent));
exports.Radio = Radio;
exports.default = cn_1.withTheme(Radio);

//# sourceMappingURL=radio.js.map
