"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

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

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

var _classnames = _interopRequireDefault(require("classnames"));

var _components = require("../components");

var _ReactNodeValidate = require("../utils/ReactNodeValidate");

var _toArray = _interopRequireDefault(require("../components/rc-util/Children/toArray"));

var RcItem = _components.Menu.MenuItem;

var MenuItem = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var _classNames, _icon$props;

  var title = props.title,
      danger = props.danger,
      children = props.children,
      className = props.className,
      level = props.level,
      rootPrefixCls = props.rootPrefixCls,
      icon = props.icon,
      inlineCollapsed = props.inlineCollapsed,
      restProps = (0, _objectWithoutProperties2.default)(props, ["title", "danger", "children", "className", "level", "rootPrefixCls", "icon", "inlineCollapsed"]);
  var menuItemRef = ref || (0, _react.useRef)(null);

  var renderItemChildren = function renderItemChildren(inlineCollapsedUse) {
    if (!icon || (0, _ReactNodeValidate.isValidElement)(children) && children.type === 'span') {
      if (children && inlineCollapsedUse && level === 1 && typeof children === 'string') {
        return /*#__PURE__*/_react.default.createElement("div", {
          className: "".concat(rootPrefixCls, "-inline-collapsed-noicon")
        }, children.charAt(0));
      }

      return children;
    }

    return /*#__PURE__*/_react.default.createElement("span", null, children);
  };

  var childrenLength = (0, _toArray.default)(children).length;
  var endCls = (0, _classnames.default)((_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(rootPrefixCls, "-item-danger"), danger), (0, _defineProperty2.default)(_classNames, "".concat(rootPrefixCls, "-item-only-child"), (icon ? childrenLength + 1 : childrenLength) === 1), _classNames), className);
  var comProps = (0, _objectSpread2.default)({
    ref: menuItemRef,
    title: title,
    level: level,
    rootPrefixCls: rootPrefixCls,
    className: endCls,
    inlineCollapsed: inlineCollapsed
  }, restProps);
  return /*#__PURE__*/_react.default.createElement(RcItem, Object.assign({}, comProps), (0, _ReactNodeValidate.cloneElement)(icon, {
    className: (0, _classnames.default)((0, _ReactNodeValidate.isValidElement)(icon) ? (_icon$props = icon.props) === null || _icon$props === void 0 ? void 0 : _icon$props.className : '', "".concat(rootPrefixCls, "-item-icon"))
  }), renderItemChildren(inlineCollapsed));
});

var _default = MenuItem;
exports.default = _default;