"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.Button = void 0;
var react_1 = __importDefault(require("react"));
var bem_react_classname_1 = require("bem-react-classname");
var cn_1 = require("../cn");
var keyboard_code_1 = __importDefault(require("../lib/keyboard-code"));
/**
 * Компонент кнопки (да, она нажимается!).
 */
var Button = /** @class */ (function (_super) {
    __extends(Button, _super);
    function Button() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.cn = bem_react_classname_1.createCn('button');
        _this.state = {
            focused: false,
            hovered: false,
            pressed: false,
        };
        _this.handleClick = function (event) {
            if (_this.props.onClick) {
                _this.props.onClick(event);
            }
        };
        _this.handleFocus = function (event) {
            if (_this.state.pressed) {
                return;
            }
            _this.setState({ focused: true });
            if (_this.props.onFocus) {
                _this.props.onFocus(event);
            }
        };
        _this.handleBlur = function (event) {
            _this.setState({ focused: false });
            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);
            }
        };
        _this.handleMouseDown = function (event) {
            if (!_this.props.disabled) {
                _this.setState({ pressed: true });
            }
            if (_this.props.onMouseDown) {
                _this.props.onMouseDown(event);
            }
        };
        _this.handleMouseUp = function (event) {
            if (!_this.props.disabled) {
                _this.setState({ pressed: false });
            }
            if (_this.props.onMouseUp) {
                _this.props.onMouseUp(event);
            }
        };
        _this.handleMouseOut = function (event) {
            if (!_this.props.disabled) {
                _this.setState({ pressed: false });
            }
            if (_this.props.onMouseOut) {
                _this.props.onMouseOut(event);
            }
        };
        _this.handleKeyDown = function (event) {
            if ((event.which === keyboard_code_1.default.ENTER || event.which === keyboard_code_1.default.SPACE) && !_this.props.disabled) {
                _this.setState({ pressed: true });
            }
            if (_this.props.onKeyDown) {
                _this.props.onKeyDown(event);
            }
        };
        _this.handleKeyUp = function (event) {
            if ((event.which === keyboard_code_1.default.ENTER || event.which === keyboard_code_1.default.SPACE) && !_this.props.disabled) {
                _this.setState({ pressed: false });
            }
            if (_this.props.onKeyUp) {
                _this.props.onKeyUp(event);
            }
        };
        return _this;
    }
    Button.getDerivedStateFromProps = function (nextProps) {
        if (nextProps.disabled) {
            return {
                hovered: false,
                focused: false,
            };
        }
        return null;
    };
    Button.prototype.render = function () {
        var _this = this;
        var isButton = this.props.tag !== 'span';
        var buttonProps = {
            ref: function (control) {
                _this.control = control;
            },
            role: 'button',
            id: this.props.id,
            name: this.props.name,
            type: this.props.type,
            title: this.props.title,
            tabIndex: this.props.disabled ? -1 : this.props.tabIndex,
            disabled: this.props.disabled,
            formNoValidate: isButton ? this.props.formNoValidate : null,
            className: this.cn({
                disabled: this.props.disabled,
                pseudo: this.props.pseudo,
                view: this.props.view,
                size: this.props.size,
                width: this.props.width,
                focused: this.props.focused === undefined ? this.state.focused : this.props.focused,
                hovered: this.state.hovered,
                pressed: this.state.pressed,
                togglable: this.props.togglable,
                checked: this.props.checked,
            }),
            onClick: this.handleClick,
            onFocus: this.handleFocus,
            onBlur: this.handleBlur,
            onMouseEnter: this.handleMouseEnter,
            onMouseLeave: this.handleMouseLeave,
            onMouseDown: this.handleMouseDown,
            onMouseUp: this.handleMouseUp,
            onMouseOut: this.handleMouseOut,
            onKeyDown: this.handleKeyDown,
            onKeyUp: this.handleKeyUp,
            'data-test-id': this.props['data-test-id'],
        };
        var buttonContent = [
            this.props.leftAddons && (react_1.default.createElement("span", { key: "left-addons", className: this.cn('addon') }, this.props.leftAddons)),
            (this.props.children || this.props.text || this.props.icon) && (react_1.default.createElement("span", { key: "content", className: this.cn('content') },
                this.props.icon && (react_1.default.createElement("span", { key: "icon", className: this.cn('icon') }, this.props.icon)),
                (this.props.children || this.props.text) && (react_1.default.createElement("span", { key: "text", className: this.cn('text') }, this.props.children || this.props.text)))),
            this.props.rightAddons && (react_1.default.createElement("span", { key: "right-addons", className: this.cn('addon') }, this.props.rightAddons)),
        ];
        return isButton ? (
        // eslint-disable-next-line react/button-has-type
        react_1.default.createElement("button", __assign({}, buttonProps), buttonContent)) : (react_1.default.createElement("span", __assign({}, buttonProps), buttonContent));
    };
    /**
     * Возвращает корневой `HTMLElement` компонента.
     */
    Button.prototype.getNode = function () {
        return this.control;
    };
    /**
     * Устанавливает фокус на поле ввода.
     */
    Button.prototype.focus = function () {
        this.control.focus();
    };
    /**
     * Убирает фокус с поля ввода.
     */
    // eslint-disable-next-line class-methods-use-this
    Button.prototype.blur = function () {
        if (document.activeElement) {
            document.activeElement.blur();
        }
    };
    Button.defaultProps = {
        type: 'button',
        tag: 'button',
        size: 'm',
        formNoValidate: false,
        view: 'default',
    };
    return Button;
}(react_1.default.PureComponent));
exports.Button = Button;
exports.default = cn_1.withTheme(Button);

//# sourceMappingURL=button.js.map
