import _extends from "@babel/runtime/helpers/extends";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import { findDOMNode } from 'react-dom';
import scrollIntoView from 'dom-scroll-into-view';
import classNames from 'classnames';
import { connect } from 'mini-store';
import omit from 'lodash/omit';
import noop from 'lodash/noop';
import { hide, show } from '../../../pro/es/tooltip/singleton';
import isOverflow from '../../../pro/es/overflow-tip/util';
import KeyCode from '../../_util/KeyCode';
import Checkbox from '../../checkbox/Checkbox';
import Ripple from '../../ripple';
import { menuAllProps } from './util';
/* eslint react/no-is-mounted:0 */

export var MenuItem = /*#__PURE__*/function (_React$Component) {
  _inherits(MenuItem, _React$Component);

  var _super = _createSuper(MenuItem);

  function MenuItem() {
    var _this;

    _classCallCheck(this, MenuItem);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));

    _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (e) {
      var keyCode = e.keyCode;

      if (keyCode === KeyCode.ENTER) {
        _this.onClick(e);

        return true;
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onMouseLeave", function (e) {
      var _this$props = _this.props,
          eventKey = _this$props.eventKey,
          onItemHover = _this$props.onItemHover,
          onMouseLeave = _this$props.onMouseLeave;
      onItemHover({
        key: eventKey,
        hover: false
      });
      onMouseLeave({
        key: eventKey,
        domEvent: e
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onMouseEnter", function (e) {
      var _this$props2 = _this.props,
          eventKey = _this$props2.eventKey,
          onItemHover = _this$props2.onItemHover,
          onMouseEnter = _this$props2.onMouseEnter;
      onItemHover({
        key: eventKey,
        hover: true
      });
      onMouseEnter({
        key: eventKey,
        domEvent: e
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleRippleMouseEnter", function (e) {
      var _this$props3 = _this.props,
          tooltip = _this$props3.tooltip,
          tooltipTheme = _this$props3.tooltipTheme,
          tooltipPlacement = _this$props3.tooltipPlacement,
          children = _this$props3.children,
          mode = _this$props3.mode;
      var currentTarget = e.currentTarget;

      if (children && (tooltip === 'always' || tooltip === 'overflow' && isOverflow(currentTarget))) {
        show(currentTarget, {
          title: children,
          placement: tooltipPlacement || mode === 'horizontal' ? 'top' : 'right',
          theme: tooltipTheme
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onClick", function (e) {
      var _this$props4 = _this.props,
          eventKey = _this$props4.eventKey,
          multiple = _this$props4.multiple,
          onClick = _this$props4.onClick,
          onSelect = _this$props4.onSelect,
          onDeselect = _this$props4.onDeselect,
          isSelected = _this$props4.isSelected;
      var info = {
        key: eventKey,
        keyPath: [eventKey],
        item: _assertThisInitialized(_this),
        domEvent: e
      };
      onClick(info);

      if (multiple) {
        if (isSelected) {
          onDeselect(info);
        } else {
          onSelect(info);
        }
      } else if (!isSelected) {
        onSelect(info);
      }
    });

    return _this;
  }

  _createClass(MenuItem, [{
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var props = this.props;

      if (props.onDestroy) {
        props.onDestroy(props.eventKey);
      }
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      // invoke customized ref to expose component to mixin
      this.callRef();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      if (this.props.active) {
        scrollIntoView(findDOMNode(this), findDOMNode(this.props.parentMenu), {
          onlyScrollIfNeeded: true
        });
      }

      this.callRef();
    }
  }, {
    key: "getPrefixCls",
    value: function getPrefixCls() {
      return "".concat(this.props.rootPrefixCls, "-item");
    }
  }, {
    key: "getActiveClassName",
    value: function getActiveClassName() {
      return "".concat(this.getPrefixCls(), "-active");
    }
  }, {
    key: "getSelectedClassName",
    value: function getSelectedClassName() {
      return "".concat(this.getPrefixCls(), "-selected");
    }
  }, {
    key: "getDisabledClassName",
    value: function getDisabledClassName() {
      return "".concat(this.getPrefixCls(), "-disabled");
    }
  }, {
    key: "callRef",
    value: function callRef() {
      if (this.props.manualRef) {
        this.props.manualRef(this);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var props = _objectSpread({}, this.props);

      var className = classNames(this.getPrefixCls(), props.className, (_classNames = {}, _defineProperty(_classNames, this.getActiveClassName(), !props.disabled && props.active), _defineProperty(_classNames, this.getSelectedClassName(), props.isSelected), _defineProperty(_classNames, this.getDisabledClassName(), props.disabled), _classNames));

      var attrs = _objectSpread(_objectSpread({}, props.attribute), {}, {
        title: props.title,
        className: className,
        role: props.role || 'menuitem',
        'aria-disabled': props.disabled
      });

      if (props.role === 'option') {
        // overwrite to option
        attrs = _objectSpread(_objectSpread({}, attrs), {}, {
          role: 'option',
          'aria-selected': props.isSelected
        });
      } else if (props.role === null || props.role === 'none') {
        attrs.role = 'none';
      }

      var mouseEvent = {
        onClick: props.disabled ? null : this.onClick,
        onMouseLeave: props.disabled ? null : this.onMouseLeave,
        onMouseEnter: props.disabled ? null : this.onMouseEnter,
        onMouseDown: props.disabled ? null : props.onMouseDown
      };

      var style = _objectSpread({}, props.style);

      if (props.mode === 'inline') {
        style.paddingLeft = props.inlineIndent * props.level;
      }

      var checkbox = props.multiple && props.checkable !== false ? /*#__PURE__*/React.createElement(Checkbox, {
        disabled: props.disabled,
        checked: props.isSelected,
        tabIndex: -1
      }) : null;
      var rippleProps = {
        disabled: props.disabled || props.rippleDisabled
      };

      if (['overflow', 'always'].includes(props.tooltip)) {
        rippleProps.onMouseEnter = this.handleRippleMouseEnter;
        rippleProps.onMouseLeave = hide;
      }

      menuAllProps.forEach(function (key) {
        return delete props[key];
      });
      return /*#__PURE__*/React.createElement(Ripple, rippleProps, /*#__PURE__*/React.createElement("li", _extends({}, omit(props, ['tooltipPlacement', 'tooltipTheme']), attrs, mouseEvent, {
        style: style
      }), checkbox, props.children));
    }
  }]);

  return MenuItem;
}(React.Component);

_defineProperty(MenuItem, "displayName", 'MenuItem');

_defineProperty(MenuItem, "defaultProps", {
  onSelect: noop,
  onMouseEnter: noop,
  onMouseLeave: noop,
  onMouseDown: noop,
  manualRef: noop
});

MenuItem.isMenuItem = true;
var connected = connect(function (_ref, _ref2) {
  var activeKey = _ref.activeKey,
      selectedKeys = _ref.selectedKeys;
  var eventKey = _ref2.eventKey,
      subMenuKey = _ref2.subMenuKey;
  return {
    active: activeKey[subMenuKey] === eventKey,
    isSelected: selectedKeys.indexOf(eventKey) !== -1
  };
})(MenuItem);
export default connected;
//# sourceMappingURL=MenuItem.js.map
