"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.MenuItem = void 0;
var react_1 = __importDefault(require("react"));
var bem_react_classname_1 = require("bem-react-classname");
var cn_1 = require("../cn");
var dropdown_1 = __importDefault(require("../dropdown/dropdown"));
var link_1 = __importDefault(require("../link/link"));
/**
 * Компонент элемента меню. Как правило, используется совместно с `Menu`.
 */
var MenuItem = /** @class */ (function (_super) {
    __extends(MenuItem, _super);
    function MenuItem() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.cn = bem_react_classname_1.createCn('menu-item');
        _this.state = {
            hovered: false,
            focused: false,
        };
        _this.handleClick = function (event) {
            if (_this.props.disabled) {
                event.preventDefault();
                return;
            }
            if (_this.props.onClick) {
                _this.props.onClick(event);
            }
        };
        _this.handleFocus = function (event) {
            _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) {
            _this.setState({ hovered: true });
            if (_this.props.onMouseEnter) {
                _this.props.onMouseEnter(event);
            }
        };
        _this.handleMouseLeave = function (event) {
            _this.setState({ hovered: false });
            if (_this.props.onMouseLeave) {
                _this.props.onMouseLeave(event);
            }
        };
        return _this;
    }
    MenuItem.prototype.render = function () {
        var _this = this;
        var content = this.props.children || this.props.value;
        var itemElement;
        var menuItemProps = {
            ref: function (root) {
                _this.root = root;
            },
            'data-test-id': this.props['data-test-id'],
        };
        switch (this.props.type) {
            case 'dropdown':
                itemElement = (react_1.default.createElement(dropdown_1.default, { ref: function (control) {
                        _this.control = control;
                    }, className: this.cn('control') + " " + this.cn('dropdown'), size: this.props.size, theme: this.props.theme, opened: this.state.hovered, switcherType: "link", switcherText: content, popupContent: this.props.popup, popupProps: {
                        directions: ['bottom-left'],
                        target: 'anchor',
                    }, mode: "hover", onSwitcherClick: this.handleClick, onSwitcherMouseEnter: this.handleMouseEnter, onSwitcherMouseLeave: this.handleMouseLeave }));
                break;
            case 'block':
                menuItemProps = {
                    ref: function (control) {
                        _this.root = control;
                        _this.control = control;
                    },
                    onClick: this.handleClick,
                    onMouseEnter: this.handleMouseEnter,
                    onMouseLeave: this.handleMouseLeave,
                };
                itemElement = react_1.default.createElement("span", { className: this.cn('control') }, content);
                break;
            case 'link':
            default:
                itemElement = (react_1.default.createElement(link_1.default, { ref: function (control) {
                        _this.control = control;
                    }, className: this.cn('control') + " " + this.cn('link'), size: this.props.size, theme: this.props.theme, pseudo: this.props.view === 'pseudo', disabled: this.props.disabled, checked: this.props.checked, text: this.props.view !== 'plain' && content, url: this.props.url, target: this.props.target, onClick: this.handleClick, onFocus: this.handleFocus, onBlur: this.handleBlur, onMouseEnter: this.handleMouseEnter, onMouseLeave: this.handleMouseLeave }, this.props.view === 'plain' && content));
                break;
        }
        return (react_1.default.createElement("div", __assign({ className: this.cn({
                disabled: this.props.disabled,
                checked: this.props.checked,
                hidden: this.props.hidden,
                type: this.props.type,
                hovered: this.props.hovered === undefined ? this.state.hovered : this.props.hovered,
                focused: this.state.focused,
            }) }, menuItemProps), itemElement));
    };
    /**
     * Возвращает корневой `HTMLElement` компонента.
     */
    MenuItem.prototype.getNode = function () {
        return this.root;
    };
    /**
     * Устанавливает фокус на элементе меню.
     */
    MenuItem.prototype.focus = function () {
        this.control.focus();
    };
    /**
     * Убирает фокус с элемента меню.
     */
    // eslint-disable-next-line class-methods-use-this
    MenuItem.prototype.blur = function () {
        if (document.activeElement) {
            document.activeElement.blur();
        }
    };
    MenuItem.defaultProps = {
        type: 'link',
        view: 'plain',
    };
    return MenuItem;
}(react_1.default.PureComponent));
exports.MenuItem = MenuItem;
exports.default = cn_1.withTheme(MenuItem);

//# sourceMappingURL=menu-item.js.map
