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);
};
import * as _ from "lodash";
import * as classnames from "classnames";
import * as PropTypes from "prop-types";
import * as React from "react";
import * as ReactDOM from "react-dom";
import "./MenuItem.less";
var propTypes = {
    children: PropTypes.node.isRequired,
    className: PropTypes.string,
    component: PropTypes.any,
    customStyles: PropTypes.bool,
    disabled: PropTypes.bool,
    focused: PropTypes.bool,
    href: PropTypes.string,
    onBlur: PropTypes.func,
    onClick: PropTypes.func,
    onMouseEnter: PropTypes.func,
    selected: PropTypes.bool,
    target: PropTypes.string,
};
var defaultProps = {
    onClick: _.noop,
    onMouseEnter: _.noop,
};
export var cssClass = {
    CONTAINER: "Menu--MenuItem",
    DEFAULT_STYLES: "Menu--MenuItem--default",
    selected: function (baseClass) { return (baseClass ? baseClass + "--selected" : ""); },
};
var MenuItem = /** @class */ (function (_super) {
    __extends(MenuItem, _super);
    function MenuItem() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    MenuItem.prototype.componentDidMount = function () {
        this._containerEl = ReactDOM.findDOMNode(this);
        this._updateFocus(this.props);
    };
    MenuItem.prototype.componentWillReceiveProps = function (nextProps) {
        this._updateFocus(nextProps);
    };
    MenuItem.prototype.render = function () {
        var _a = this.props, children = _a.children, className = _a.className, component = _a.component, customStyles = _a.customStyles, href = _a.href, onBlur = _a.onBlur, onClick = _a.onClick, onMouseEnter = _a.onMouseEnter, selected = _a.selected, target = _a.target, disabled = _a.disabled;
        var additionalProps = _.omit(this.props, Object.keys(propTypes));
        var MenuButton = component;
        if (!MenuButton) {
            // Render disabled href elements as buttons so that they can be disabled
            MenuButton = href && !disabled ? "a" : "button";
        }
        // Safari fires focusout events on button mousedown events - it basically removes and returns
        // focus between mousedown and mouseup. Yes, that is quite weird.
        //
        // To avoid closing the menu before the user is done clicking a button menu item, we trigger
        // onClick on mousedown instead.
        //
        // This only needs to be done for buttons, not anchors.
        var onMouseDown;
        if (MenuButton === "button") {
            onMouseDown = onClick;
        }
        return (React.createElement(MenuButton, __assign({}, additionalProps, { className: classnames(cssClass.CONTAINER, !customStyles && cssClass.DEFAULT_STYLES, className, selected && cssClass.selected(cssClass.CONTAINER), selected && cssClass.selected(className)), href: href, onBlur: onBlur, onClick: onClick, onMouseDown: onMouseDown, onMouseEnter: onMouseEnter, disabled: disabled, role: "menuitem", tabIndex: -1, target: target }), children));
    };
    MenuItem.prototype._updateFocus = function (props) {
        var _this = this;
        if (props.focused) {
            if (this._containerEl && this._containerEl.focus) {
                setTimeout(function () { return _this._containerEl.focus(); });
            }
        }
    };
    MenuItem.propTypes = propTypes;
    MenuItem.defaultProps = defaultProps;
    return MenuItem;
}(React.PureComponent));
export default MenuItem;
