"use strict";
Object.defineProperty(exports, "__esModule", {
  value: !0
}), exports.default = void 0;
var _class, _temp, _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 _interopRequireDefault(e) {
  return e && e.__esModule ? e : {
    default: e
  }
}

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

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

function _typeof(e) {
  return (_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
    return typeof e
  } : function(e) {
    return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e
  })(e)
}

function _objectWithoutProperties(e, t) {
  if (null == e) return {};
  var r, n = _objectWithoutPropertiesLoose(e, t);
  if (Object.getOwnPropertySymbols)
    for (var o = Object.getOwnPropertySymbols(e), i = 0; i < o.length; i++) r = o[i], 0 <= t.indexOf(r) || Object.prototype.propertyIsEnumerable.call(e, r) && (n[r] = e[r]);
  return n
}

function _objectWithoutPropertiesLoose(e, t) {
  if (null == e) return {};
  for (var r, n = {}, o = Object.keys(e), i = 0; i < o.length; i++) r = o[i], 0 <= t.indexOf(r) || (n[r] = e[r]);
  return n
}

function _extends() {
  return (_extends = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
      var r, n = arguments[t];
      for (r in n) Object.prototype.hasOwnProperty.call(n, r) && (e[r] = n[r])
    }
    return e
  }).apply(this, arguments)
}

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

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

function _classCallCheck(e, t) {
  if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function")
}

function _defineProperties(e, t) {
  for (var r = 0; r < t.length; r++) {
    var n = t[r];
    n.enumerable = n.enumerable || !1, n.configurable = !0, "value" in n && (n.writable = !0), Object.defineProperty(e, n.key, n)
  }
}

function _createClass(e, t, r) {
  return t && _defineProperties(e.prototype, t), r && _defineProperties(e, r), e
}

function _inherits(e, t) {
  if ("function" != typeof t && null !== t) throw new TypeError("Super expression must either be null or a function");
  e.prototype = Object.create(t && t.prototype, {
    constructor: {
      value: e,
      writable: !0,
      configurable: !0
    }
  }), t && _setPrototypeOf(e, t)
}

function _setPrototypeOf(e, t) {
  return (_setPrototypeOf = Object.setPrototypeOf || function(e, t) {
    return e.__proto__ = t, e
  })(e, t)
}

function _createSuper(r) {
  var n = _isNativeReflectConstruct();
  return function() {
    var e, t = _getPrototypeOf(r);
    return _possibleConstructorReturn(this, n ? (e = _getPrototypeOf(this).constructor, Reflect.construct(t, arguments, e)) : t.apply(this, arguments))
  }
}

function _possibleConstructorReturn(e, t) {
  return !t || "object" !== _typeof(t) && "function" != typeof t ? _assertThisInitialized(e) : t
}

function _assertThisInitialized(e) {
  if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  return e
}

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

function _getPrototypeOf(e) {
  return (_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function(e) {
    return e.__proto__ || Object.getPrototypeOf(e)
  })(e)
}

function _defineProperty(e, t, r) {
  return t in e ? Object.defineProperty(e, t, {
    value: r,
    enumerable: !0,
    configurable: !0,
    writable: !0
  }) : e[t] = r, e
}
var MenuItem = (0, _connectMenu.default)(_class = _temp = function() {
  _inherits(r, _react.PureComponent);
  var t = _createSuper(r);

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