import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import React, { PureComponent, Fragment, isValidElement, cloneElement } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { isValid } from 'ipaddr.js';
import { connect } from './MenuContext';
import Tooltip from '../tooltip';
import Check from '../icons/internals/Check';
/**
 *
 * MenuItem
 */

var MenuItem =
/*#__PURE__*/
function (_PureComponent) {
  _inherits(MenuItem, _PureComponent);

  function MenuItem() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, MenuItem);

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

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(MenuItem)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "keyDown", false);

    _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (event) {
      var onKeyDown = _this.props.onKeyDown;
      var key = event.key;

      if (!_this.keyDown && key === ' ') {
        _this.keyDown = true;
        event.persist();
      }

      if (onKeyDown) {
        onKeyDown(event);
      }

      if (event.target === _this.el && (key === ' ' || key === 'Enter')) {
        event.preventDefault();

        _this.onClick(event);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onKeyUp", function (event) {
      var onKeyUp = _this.props.onKeyUp;

      if (event === ' ') {
        _this.keyDown = false;
        event.persist();
      }

      if (onKeyUp) {
        onKeyUp(event);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onClick", function (e) {
      var _this$props = _this.props,
          disabled = _this$props.disabled,
          value = _this$props.value,
          onSelect = _this$props.onSelect,
          onClick = _this$props.onClick,
          close = _this$props.close;
      onClick && onClick(e);

      if (!disabled && value !== undefined) {
        onSelect && onSelect(e, value);
      }

      close && close();
    });

    return _this;
  }

  _createClass(MenuItem, [{
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props2 = this.props,
          className = _this$props2.className,
          index = _this$props2.index,
          value = _this$props2.value,
          selected = _this$props2.selected,
          disabled = _this$props2.disabled,
          prefixCls = _this$props2.prefixCls,
          onSelect = _this$props2.onSelect,
          close = _this$props2.close,
          children = _this$props2.children,
          prefix = _this$props2.prefix,
          suffix = _this$props2.suffix,
          tabIndex = _this$props2.tabIndex,
          isLastNode = _this$props2.isLastNode,
          level = _this$props2.level,
          compact = _this$props2.compact,
          indentBase = _this$props2.indentBase,
          searchText = _this$props2.searchText,
          disableTooltip = _this$props2.disableTooltip,
          Title = _this$props2.tag,
          indent = _this$props2.indent,
          autoIndent = _this$props2.autoIndent,
          selectable = _this$props2.selectable,
          currentValue = _this$props2.currentValue,
          inset = _this$props2.inset,
          mode = _this$props2.mode,
          autoFold = _this$props2.autoFold,
          prefixWidth = _this$props2.prefixWidth,
          multiple = _this$props2.multiple,
          other = _objectWithoutProperties(_this$props2, ["className", "index", "value", "selected", "disabled", "prefixCls", "onSelect", "close", "children", "prefix", "suffix", "tabIndex", "isLastNode", "level", "compact", "indentBase", "searchText", "disableTooltip", "tag", "indent", "autoIndent", "selectable", "currentValue", "inset", "mode", "autoFold", "prefixWidth", "multiple"]);

      var itemSelected = false;

      if (selected != null) {
        itemSelected = selected;
      } else {
        itemSelected = selectable && value != null && (Array.isArray(currentValue) ? currentValue.includes(value) : currentValue === value);
      }

      var indentLevel = autoIndent ? level : 1;
      var isSelected = !disabled && selectable && itemSelected;
      var classes = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-selected"), isSelected), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _defineProperty(_classNames, "".concat(prefixCls, "-multiple"), multiple), _classNames), className);
      var prefixWithClass = isValidElement(prefix) ? cloneElement(prefix, {
        className: classNames("".concat(prefixCls, "-prefix"), _defineProperty({}, "".concat(prefixCls, "-prefix-compact"), compact), prefix.props.className)
      }) : prefix;
      var suffixIcon = isSelected && multiple ? React.createElement(Check, null) : suffix;
      var suffixWithClass = isValidElement(suffixIcon) ? cloneElement(suffixIcon, {
        className: classNames("".concat(prefixCls, "-suffix"), suffixIcon.props.className)
      }) : null;
      var content = compact ? prefixWithClass || React.createElement("div", {
        className: "".concat(prefixCls, "-prefix ").concat(prefixCls, "-prefix-compact")
      }, children && (children[0] && children[0].toUpperCase ? children[0].toUpperCase() : children)) : React.createElement(Fragment, null, prefixWithClass, React.createElement("div", {
        className: "".concat(prefixCls, "-content")
      }, children), suffixWithClass);
      var title = React.createElement(Title, _extends({}, other, {
        style: {
          paddingLeft: indentLevel * indent + (compact ? 0 : indentBase) + (!prefix && inset ? prefixWidth : 0)
        },
        tabIndex: disabled || !itemSelected ? -1 : 0,
        className: classes,
        onKeyDown: this.onKeyDown,
        onKeyUp: this.onKeyUp,
        onClick: this.onClick
      }), content);
      return compact && !disableTooltip ? React.createElement(Tooltip, {
        content: title,
        autoAdjustPosition: true,
        position: "right"
      }, children) : title;
    }
  }]);

  return MenuItem;
}(PureComponent);

_defineProperty(MenuItem, "propTypes", {
  /**
   * 类前缀
   * */
  prefixCls: PropTypes.string,

  /** MenuItem的值 */
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]),

  /** 被选中时回调  */
  onSelect: PropTypes.func,

  /** @ignore 关闭父组件的回调 */
  close: PropTypes.func,

  /** 是否被选中 */
  selected: PropTypes.bool,

  /** 是否禁用 */
  disabled: PropTypes.bool,

  /** 是否自动缩进 */
  autoIndent: PropTypes.bool,

  /** 缩进距离（单位px） */
  indent: PropTypes.number,

  /** 当前嵌套层数 */
  level: PropTypes.number,

  /** 前缀 */
  suffix: PropTypes.element,

  /** 后缀 */
  prefix: PropTypes.element,

  /** 同原生role */
  role: PropTypes.string,

  /** 提示文本 */
  tag: PropTypes.string,

  /** 是否可选中 */
  selectable: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),

  /** 是否紧凑显示 */
  compact: PropTypes.bool,

  /** 是否显示提示框 */
  disableTooltip: PropTypes.bool,

  /** 是否留出缩进空白 */
  inset: PropTypes.bool,
  prefixWidth: PropTypes.number
});

_defineProperty(MenuItem, "defaultProps", {
  prefixCls: 'ne-menu-item',
  disabled: false,
  currentValue: [],
  level: 1,
  inset: false,
  role: 'menuitem',
  compact: false,
  tag: 'li',
  indentBase: 8,
  prefixWidth: 26,
  disableTooltip: false
});

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

var connectedMenuItem = connect(MenuItem);
connectedMenuItem.isMenuItem = true;
export default connectedMenuItem;