"use strict";

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

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

var _class, _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")), _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")), _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")), _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")), _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")), _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")), _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")), _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")), _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")), _react = _interopRequireWildcard(require("react")), _propTypes = _interopRequireDefault(require("prop-types")), _amosReactRouter = require("amos-react-router"), _amosTool = require("amos-tool"), _amosIcon = _interopRequireDefault(require("amos-icon")), _amosFramework = require("amos-framework"), _classnames = _interopRequireDefault(require("classnames")), _SubMenuItem = _interopRequireDefault(require("./SubMenuItem")), _connectMenu = _interopRequireDefault(require("./connectMenu"));

function _getRequireWildcardCache(e) {
  if ("function" != typeof WeakMap) return null;
  var t = new WeakMap, r = new WeakMap;
  return (_getRequireWildcardCache = function(e) {
    return e ? r : t;
  })(e);
}

function _interopRequireWildcard(e, t) {
  if (!t && e && e.__esModule) return e;
  if (null === e || "object" !== _typeof(e) && "function" != typeof e) return {
    default: e
  };
  var r = _getRequireWildcardCache(t);
  if (r && r.has(e)) return r.get(e);
  var n = {}, a = Object.defineProperty && Object.getOwnPropertyDescriptor;
  for (var i in e) if ("default" !== i && Object.prototype.hasOwnProperty.call(e, i)) {
    var u = a ? Object.getOwnPropertyDescriptor(e, i) : null;
    u && (u.get || u.set) ? Object.defineProperty(n, i, u) : n[i] = e[i];
  }
  return n.default = e, r && r.set(e, n), n;
}

function ownKeys(e, t) {
  var r = Object.keys(e);
  if (Object.getOwnPropertySymbols) {
    var n = Object.getOwnPropertySymbols(e);
    t && (n = n.filter((function(t) {
      return Object.getOwnPropertyDescriptor(e, t).enumerable;
    }))), r.push.apply(r, n);
  }
  return r;
}

function _objectSpread(e) {
  for (var t = 1; t < arguments.length; t++) {
    var r = null != arguments[t] ? arguments[t] : {};
    t % 2 ? ownKeys(Object(r), !0).forEach((function(t) {
      (0, _defineProperty2.default)(e, t, r[t]);
    })) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(r)) : ownKeys(Object(r)).forEach((function(t) {
      Object.defineProperty(e, t, Object.getOwnPropertyDescriptor(r, t));
    }));
  }
  return e;
}

function _createSuper(e) {
  var t = _isNativeReflectConstruct();
  return function() {
    var r, n = (0, _getPrototypeOf2.default)(e);
    if (t) {
      var a = (0, _getPrototypeOf2.default)(this).constructor;
      r = Reflect.construct(n, arguments, a);
    } else r = n.apply(this, arguments);
    return (0, _possibleConstructorReturn2.default)(this, r);
  };
}

function _isNativeReflectConstruct() {
  if ("undefined" == typeof Reflect || !Reflect.construct) return !1;
  if (Reflect.construct.sham) return !1;
  if ("function" == typeof Proxy) return !0;
  try {
    return Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], (function() {}))), 
    !0;
  } catch (e) {
    return !1;
  }
}

var MenuItem = (0, _connectMenu.default)(_class = function(e) {
  (0, _inherits2.default)(r, e);
  var t = _createSuper(r);
  function r(e) {
    var n;
    return (0, _classCallCheck2.default)(this, r), n = t.call(this, e), (0, _defineProperty2.default)((0, 
    _assertThisInitialized2.default)(n), "isActive", (function() {
      var e = n.props;
      return e.menuKey === e.activeKey;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "genHiddenStyle", (function() {
      var e = n.props.height;
      return (void 0 === e ? 0 : e) > 0 ? {} : {
        display: "none"
      };
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "genStyle", (function() {
      var e = n.props.menuWidth;
      return e ? {
        width: e
      } : {};
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "renderItemLink", (function(e) {
      var t = n.props.iconPrefix, r = e.icon && _react.default.createElement(_amosIcon.default, {
        icon: e.icon,
        prefix: t,
        className: "menu-item-icon"
      });
      return e.isBlank ? _react.default.createElement("a", {
        href: e.path,
        target: "_blank",
        rel: "noopener noreferrer",
        className: "menu-item-link"
      }, r, e.permissionName) : _react.default.createElement(_amosReactRouter.Link, {
        to: e.path,
        className: "menu-item-link"
      }, r, e.permissionName);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "renderSub", (function() {
      var e = n.props, t = e.menuKey, r = e.children, a = e.onSubMenuClick, i = e.activeSubKey, u = e.setSubMenuActive, o = e.renderSubmenu, s = e.menuWidth, l = e.iconPrefix, c = n.genHiddenStyle();
      return _react.default.createElement("ul", {
        className: "menu-sublist"
      }, r.map((function(e) {
        var r = (0, _classnames.default)("menu-subitem", {
          active: e.sequenceNbr === i
        });
        return o ? o(_objectSpread(_objectSpread({}, e), {}, {
          className: r,
          onSubMenuClick: a,
          setSubMenuActive: u
        })) : _react.default.createElement(_SubMenuItem.default, (0, _extends2.default)({}, e, {
          key: e.sequenceNbr,
          className: r,
          iconPrefix: l,
          menuKey: e.sequenceNbr,
          rootMenuKey: t,
          subItems: e.children,
          onClick: function() {
            return a(t, e.sequenceNbr);
          },
          onSubMenuClick: a,
          setMenuActive: u,
          menuWidth: s
        }), n.renderItemLink(e), e.children && e.children.length > 0 && _react.default.createElement(_amosFramework.Icon, {
          icon: "right",
          className: "menu-subitem-icon",
          style: c
        }));
      })));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "renderNonSub", (function() {
      return _react.default.createElement("ul", {
        className: "menu-sublist"
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "renderFooter", (function() {
      var e = n.props, t = e.footerIcon, r = e.extraIcon, a = e.icon, i = e.iconPrefix, u = n.genHiddenStyle(), o = t || r || a, s = _amosTool.utils.isImageSrc(o) ? _react.default.createElement("img", {
        src: o,
        alt: ""
      }) : _react.default.createElement(_amosIcon.default, {
        prefix: i,
        icon: o
      });
      return _react.default.createElement("div", {
        className: "nav-menu-item-footer",
        style: u
      }, _react.default.createElement("div", {
        className: "footer-icon"
      }, s), _react.default.createElement("span", {
        className: "footer-line"
      }));
    })), n;
  }
  return (0, _createClass2.default)(r, [ {
    key: "render",
    value: function() {
      var e, t = this.props, r = t.menuKey, n = t.permissionName, a = t.children, i = t.activeKey, u = t.height, o = void 0 === u ? 0 : u, s = (0, 
      _objectWithoutProperties2.default)(t, [ "menuKey", "permissionName", "children", "activeKey", "height" ]);
      e = a ? _react.default.createElement("div", {
        className: "nav-menu-item-label"
      }, n) : _react.default.createElement("div", {
        className: "nav-menu-item-label"
      }, this.renderItemLink(_objectSpread({
        permissionName: n
      }, s)));
      var l = (0, _classnames.default)("nav-menu-item", {
        active: r === i
      }), c = this.genStyle();
      return _react.default.createElement("li", {
        className: l,
        style: c
      }, e, a && _react.default.createElement(_amosFramework.Icon, {
        icon: "caretdown",
        className: "nav-menu-item-icon"
      }), _react.default.createElement("div", {
        className: "menusub-wrapper",
        style: _objectSpread({
          height: o
        }, c)
      }, a && this.renderSub(), !a && this.renderNonSub(), this.renderFooter()));
    }
  } ]), r;
}(_react.PureComponent)) || _class;

MenuItem.displayName = "MenuItem", MenuItem.propTypes = {
  height: _propTypes.default.number,
  menuKey: _propTypes.default.string,
  permissionName: _propTypes.default.string.isRequired,
  iconPrefix: _propTypes.default.string,
  icon: _propTypes.default.string,
  footerIcon: _propTypes.default.string,
  extraIcon: _propTypes.default.string,
  path: _propTypes.default.string,
  children: _propTypes.default.arrayOf(_propTypes.default.shape({
    sequenceNbr: _propTypes.default.string.isRequired,
    permissionName: _propTypes.default.string,
    icon: _propTypes.default.string,
    path: _propTypes.default.string,
    isBlank: _propTypes.default.bool
  })),
  activeKey: _propTypes.default.string,
  activeSubKey: _propTypes.default.string,
  setMenuActive: _propTypes.default.func,
  setSubMenuActive: _propTypes.default.func,
  onSubMenuClick: _propTypes.default.func,
  onMenuClosed: _propTypes.default.func,
  toggleSubMenu: _propTypes.default.func,
  renderSubmenu: _propTypes.default.func,
  menuWidth: _propTypes.default.oneOfType([ _propTypes.default.string, _propTypes.default.number ])
};

var _default = MenuItem;

exports.default = _default;