import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import React, { useRef } from 'react';
import classNames from 'classnames';
import { Menu as RcMenu } from '../components';
import { cloneElement, isValidElement } from '../utils/ReactNodeValidate';
import toArray from '../components/rc-util/Children/toArray';
var RcItem = RcMenu.MenuItem;
var MenuItem = /*#__PURE__*/React.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 = _objectWithoutProperties(props, ["title", "danger", "children", "className", "level", "rootPrefixCls", "icon", "inlineCollapsed"]);

  var menuItemRef = ref || useRef(null);

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

      return children;
    }

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

  var childrenLength = toArray(children).length;
  var endCls = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(rootPrefixCls, "-item-danger"), danger), _defineProperty(_classNames, "".concat(rootPrefixCls, "-item-only-child"), (icon ? childrenLength + 1 : childrenLength) === 1), _classNames), className);

  var comProps = _objectSpread({
    ref: menuItemRef,
    title: title,
    level: level,
    rootPrefixCls: rootPrefixCls,
    className: endCls,
    inlineCollapsed: inlineCollapsed
  }, restProps);

  return /*#__PURE__*/React.createElement(RcItem, Object.assign({}, comProps), cloneElement(icon, {
    className: classNames(isValidElement(icon) ? (_icon$props = icon.props) === null || _icon$props === void 0 ? void 0 : _icon$props.className : '', "".concat(rootPrefixCls, "-item-icon"))
  }), renderItemChildren(inlineCollapsed));
});
export default MenuItem;