"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _ = require("./..");

// Style

/* istanbul ignore next */
if (typeof window !== 'undefined') {}

var MenuItem = function MenuItem(props) {
  var children = props.children,
      className = props.className,
      color = props.color,
      openAt = props.openAt,
      disabled = props.disabled,
      icon = props.icon,
      onClick = props.onClick,
      value = props.value,
      TagName = props.TagName,
      attrs = (0, _objectWithoutProperties2["default"])(props, ["children", "className", "color", "openAt", "disabled", "icon", "onClick", "value", "TagName"]);
  var isSubMenu = Array.isArray(children);
  var content = isSubMenu ? children[0] : children;
  var MenuItemClasses = ['Menu-Item', className, color && "Menu-Item_".concat(color), disabled && 'Menu-Item_link_disabled', isSubMenu && 'Menu-Item_arrow', TagName === 'a' && 'Menu-Item_link'].filter(function (e) {
    return e;
  }).join(' ');

  var handleFocus = function handleFocus(e) {
    return e.stopPropagation();
  };

  var handleKeyDown = function handleKeyDown(e) {
    if (e.which !== 32 && e.which !== 13) return; // only fire onClick on space and enter

    if (typeof onClick === 'function') onClick(e);
  };

  if (isSubMenu) {
    return /*#__PURE__*/_react["default"].createElement(_.Dropdown, {
      justify: openAt,
      trigger: "hover"
    }, /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({}, attrs, {
      className: MenuItemClasses,
      role: "button",
      tabIndex: 0
    }), icon && /*#__PURE__*/_react["default"].createElement("i", {
      className: icon
    }), content), /*#__PURE__*/_react["default"].createElement(_.Box, {
      bem: [['pop']],
      className: "Menu-Item-SubMenu Menu-Item-SubMenu_".concat(openAt)
    }, children[1]));
  }

  return /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({}, attrs, onClick && {
    onClick: onClick,
    onFocus: handleFocus,
    onKeyDown: handleKeyDown
  }, (onClick || TagName === 'a') && {
    role: 'button',
    tabIndex: 0
  }, {
    className: MenuItemClasses
  }), icon && /*#__PURE__*/_react["default"].createElement("i", {
    className: icon
  }), content);
};

MenuItem.propTypes = {
  children: _propTypes["default"].any,
  className: _propTypes["default"].string,
  color: _propTypes["default"].oneOf(['blue', 'green', 'purple', 'red', 'yellow']),
  disabled: _propTypes["default"].bool,
  icon: _propTypes["default"].string,
  onClick: _propTypes["default"].func,
  openAt: _propTypes["default"].oneOf(['left', 'right']),
  TagName: _propTypes["default"].string,
  value: _propTypes["default"].any
};
MenuItem.defaultProps = {
  className: '',
  openAt: 'right',
  TagName: 'a'
};
var _default = MenuItem;
exports["default"] = _default;