"use strict";

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

var _vue = _interopRequireWildcard(require("vue"));

var _omit = _interopRequireDefault(require("omit.js"));

var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));

var _vcTrigger = _interopRequireDefault(require("../vc-trigger"));

var _KeyCode = _interopRequireDefault(require("../_util/KeyCode"));

var _store = require("../_util/store");

var _SubPopupMenu = _interopRequireDefault(require("./SubPopupMenu"));

var _placements = _interopRequireDefault(require("./placements"));

var _BaseMixin = _interopRequireDefault(require("../_util/BaseMixin"));

var _propsUtil = require("../_util/props-util");

var _requestAnimationTimeout = require("../_util/requestAnimationTimeout");

var _util = require("./util");

var _getTransitionProps = _interopRequireDefault(require("../_util/getTransitionProps"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var guid = 0;
var popupPlacementMap = {
  horizontal: 'bottomLeft',
  vertical: 'rightTop',
  'vertical-left': 'rightTop',
  'vertical-right': 'leftTop'
};

var updateDefaultActiveFirst = function updateDefaultActiveFirst(store, eventKey, defaultActiveFirst) {
  var menuId = (0, _util.getMenuIdFromSubMenuEventKey)(eventKey);
  var state = store.getState();
  store.setState({
    defaultActiveFirst: _objectSpread(_objectSpread({}, state.defaultActiveFirst), {}, _defineProperty({}, menuId, defaultActiveFirst))
  });
};

var SubMenu = {
  name: 'SubMenu',
  inheritAttrs: false,
  props: {
    title: _vueTypes.default.any,
    selectedKeys: _vueTypes.default.array.def([]),
    openKeys: _vueTypes.default.array.def([]),
    openChange: _vueTypes.default.func.def(_util.noop),
    rootPrefixCls: _vueTypes.default.string,
    eventKey: _vueTypes.default.oneOfType([_vueTypes.default.string, _vueTypes.default.number]),
    multiple: _vueTypes.default.bool,
    active: _vueTypes.default.bool,
    // TODO: remove
    isRootMenu: _vueTypes.default.bool.def(false),
    index: _vueTypes.default.number,
    triggerSubMenuAction: _vueTypes.default.string,
    popupClassName: _vueTypes.default.string,
    getPopupContainer: _vueTypes.default.func,
    forceSubMenuRender: _vueTypes.default.bool,
    openAnimation: _vueTypes.default.oneOfType([_vueTypes.default.string, _vueTypes.default.object]),
    disabled: _vueTypes.default.bool,
    subMenuOpenDelay: _vueTypes.default.number.def(0.1),
    subMenuCloseDelay: _vueTypes.default.number.def(0.1),
    level: _vueTypes.default.number.def(1),
    inlineIndent: _vueTypes.default.number.def(24),
    openTransitionName: _vueTypes.default.string,
    popupOffset: _vueTypes.default.array,
    isOpen: _vueTypes.default.bool,
    store: _vueTypes.default.object,
    mode: _vueTypes.default.oneOf(['horizontal', 'vertical', 'vertical-left', 'vertical-right', 'inline']).def('vertical'),
    manualRef: _vueTypes.default.func.def(_util.noop),
    builtinPlacements: _vueTypes.default.object.def(function () {
      return {};
    }),
    itemIcon: _vueTypes.default.any,
    expandIcon: _vueTypes.default.any,
    subMenuKey: _vueTypes.default.string,
    theme: _vueTypes.default.string
  },
  mixins: [_BaseMixin.default],
  isSubMenu: true,
  setup: function setup() {
    return {
      parentMenu: (0, _vue.inject)('parentMenu', undefined)
    };
  },
  created: function created() {
    (0, _vue.provide)('parentMenu', this);
  },
  data: function data() {
    var props = this.$props;
    var store = props.store;
    var eventKey = props.eventKey;
    var defaultActiveFirst = store.getState().defaultActiveFirst;
    var value = false;

    if (defaultActiveFirst) {
      value = defaultActiveFirst[eventKey];
    }

    updateDefaultActiveFirst(store, eventKey, value);
    this.internalMenuId = undefined;
    this.haveRendered = undefined;
    this.haveOpened = undefined;
    this.subMenuTitle = undefined;
    return {// defaultActiveFirst: false,
    };
  },
  mounted: function mounted() {
    var _this = this;

    this.$nextTick(function () {
      _this.handleUpdated();
    });
  },
  updated: function updated() {
    var _this2 = this;

    this.$nextTick(function () {
      _this2.handleUpdated();
    });
  },
  beforeUnmount: function beforeUnmount() {
    var eventKey = this.eventKey;

    this.__emit('destroy', eventKey);
    /* istanbul ignore if */


    if (this.minWidthTimeout) {
      (0, _requestAnimationTimeout.cancelAnimationTimeout)(this.minWidthTimeout);
      this.minWidthTimeout = null;
    }
    /* istanbul ignore if */


    if (this.mouseenterTimeout) {
      (0, _requestAnimationTimeout.cancelAnimationTimeout)(this.mouseenterTimeout);
      this.mouseenterTimeout = null;
    }
  },
  methods: {
    handleUpdated: function handleUpdated() {
      var _this3 = this;

      var mode = this.mode,
          parentMenu = this.parentMenu,
          manualRef = this.manualRef; // invoke customized ref to expose component to mixin

      if (manualRef) {
        manualRef(this);
      }

      if (mode !== 'horizontal' || !parentMenu.isRootMenu || !this.isOpen) {
        return;
      }

      this.minWidthTimeout = (0, _requestAnimationTimeout.requestAnimationTimeout)(function () {
        return _this3.adjustWidth();
      }, 0);
    },
    onKeyDown: function onKeyDown(e) {
      var keyCode = e.keyCode;
      var menu = this.menuInstance;
      var _this$$props = this.$props,
          store = _this$$props.store,
          isOpen = _this$$props.isOpen;

      if (keyCode === _KeyCode.default.ENTER) {
        this.onTitleClick(e);
        updateDefaultActiveFirst(store, this.eventKey, true);
        return true;
      }

      if (keyCode === _KeyCode.default.RIGHT) {
        if (isOpen) {
          menu.onKeyDown(e);
        } else {
          this.triggerOpenChange(true); // need to update current menu's defaultActiveFirst value

          updateDefaultActiveFirst(store, this.eventKey, true);
        }

        return true;
      }

      if (keyCode === _KeyCode.default.LEFT) {
        var handled;

        if (isOpen) {
          handled = menu.onKeyDown(e);
        } else {
          return undefined;
        }

        if (!handled) {
          this.triggerOpenChange(false);
          handled = true;
        }

        return handled;
      }

      if (isOpen && (keyCode === _KeyCode.default.UP || keyCode === _KeyCode.default.DOWN)) {
        return menu.onKeyDown(e);
      }

      return undefined;
    },
    onPopupVisibleChange: function onPopupVisibleChange(visible) {
      this.triggerOpenChange(visible, visible ? 'mouseenter' : 'mouseleave');
    },
    onMouseEnter: function onMouseEnter(e) {
      var _this$$props2 = this.$props,
          key = _this$$props2.eventKey,
          store = _this$$props2.store;
      updateDefaultActiveFirst(store, key, false);

      this.__emit('mouseenter', {
        key: key,
        domEvent: e
      });
    },
    onMouseLeave: function onMouseLeave(e) {
      var eventKey = this.eventKey;

      this.__emit('mouseleave', {
        key: eventKey,
        domEvent: e
      });
    },
    onTitleMouseEnter: function onTitleMouseEnter(domEvent) {
      var key = this.$props.eventKey;

      this.__emit('itemHover', {
        key: key,
        hover: true
      });

      this.__emit('titleMouseenter', {
        key: key,
        domEvent: domEvent
      });
    },
    onTitleMouseLeave: function onTitleMouseLeave(e) {
      var eventKey = this.eventKey;

      this.__emit('itemHover', {
        key: eventKey,
        hover: false
      });

      this.__emit('titleMouseleave', {
        key: eventKey,
        domEvent: e
      });
    },
    onTitleClick: function onTitleClick(e) {
      var _this$$props3 = this.$props,
          triggerSubMenuAction = _this$$props3.triggerSubMenuAction,
          eventKey = _this$$props3.eventKey,
          isOpen = _this$$props3.isOpen,
          store = _this$$props3.store;

      this.__emit('titleClick', {
        key: eventKey,
        domEvent: e
      });

      if (triggerSubMenuAction === 'hover') {
        return;
      }

      this.triggerOpenChange(!isOpen, 'click');
      updateDefaultActiveFirst(store, eventKey, false);
    },
    onSubMenuClick: function onSubMenuClick(info) {
      this.__emit('click', this.addKeyPath(info));
    },
    getPrefixCls: function getPrefixCls() {
      return "".concat(this.$props.rootPrefixCls, "-submenu");
    },
    getActiveClassName: function getActiveClassName() {
      return "".concat(this.getPrefixCls(), "-active");
    },
    getDisabledClassName: function getDisabledClassName() {
      return "".concat(this.getPrefixCls(), "-disabled");
    },
    getSelectedClassName: function getSelectedClassName() {
      return "".concat(this.getPrefixCls(), "-selected");
    },
    getOpenClassName: function getOpenClassName() {
      return "".concat(this.$props.rootPrefixCls, "-submenu-open");
    },
    saveMenuInstance: function saveMenuInstance(c) {
      // children menu instance
      this.menuInstance = c;
    },
    addKeyPath: function addKeyPath(info) {
      return _objectSpread(_objectSpread({}, info), {}, {
        keyPath: (info.keyPath || []).concat(this.$props.eventKey)
      });
    },
    // triggerOpenChange (open, type) {
    //   const key = this.$props.eventKey
    //   this.__emit('openChange', {
    //     key,
    //     item: this,
    //     trigger: type,
    //     open,
    //   })
    // },
    triggerOpenChange: function triggerOpenChange(open, type) {
      var _this4 = this;

      var key = this.$props.eventKey;

      var openChange = function openChange() {
        _this4.__emit('openChange', {
          key: key,
          item: _this4,
          trigger: type,
          open: open
        });
      };

      if (type === 'mouseenter') {
        // make sure mouseenter happen after other menu item's mouseleave
        this.mouseenterTimeout = (0, _requestAnimationTimeout.requestAnimationTimeout)(function () {
          openChange();
        }, 0);
      } else {
        openChange();
      }
    },
    isChildrenSelected: function isChildrenSelected() {
      var ret = {
        find: false
      };
      (0, _util.loopMenuItemRecursively)((0, _propsUtil.getSlot)(this), this.$props.selectedKeys, ret);
      return ret.find;
    },
    // isOpen () {
    //   return this.$props.openKeys.indexOf(this.$props.eventKey) !== -1
    // },
    adjustWidth: function adjustWidth() {
      /* istanbul ignore if */
      if (!this.subMenuTitle || !this.menuInstance) {
        return;
      }

      var popupMenu = (0, _propsUtil.findDOMNode)(this.menuInstance);

      if (popupMenu.offsetWidth >= this.subMenuTitle.offsetWidth) {
        return;
      }
      /* istanbul ignore next */


      popupMenu.style.minWidth = "".concat(this.subMenuTitle.offsetWidth, "px");
    },
    saveSubMenuTitle: function saveSubMenuTitle(subMenuTitle) {
      this.subMenuTitle = subMenuTitle;
    },
    renderChildren: function renderChildren(children) {
      var props = _objectSpread(_objectSpread({}, this.$props), this.$attrs);

      var subPopupMenuProps = {
        mode: props.mode === 'horizontal' ? 'vertical' : props.mode,
        visible: props.isOpen,
        level: props.level + 1,
        inlineIndent: props.inlineIndent,
        focusable: false,
        selectedKeys: props.selectedKeys,
        eventKey: "".concat(props.eventKey, "-menu-"),
        openKeys: props.openKeys,
        openTransitionName: props.openTransitionName,
        openAnimation: props.openAnimation,
        subMenuOpenDelay: props.subMenuOpenDelay,
        subMenuCloseDelay: props.subMenuCloseDelay,
        forceSubMenuRender: props.forceSubMenuRender,
        triggerSubMenuAction: props.triggerSubMenuAction,
        builtinPlacements: props.builtinPlacements,
        defaultActiveFirst: props.store.getState().defaultActiveFirst[(0, _util.getMenuIdFromSubMenuEventKey)(props.eventKey)],
        multiple: props.multiple,
        prefixCls: props.rootPrefixCls,
        manualRef: this.saveMenuInstance,
        itemIcon: (0, _propsUtil.getComponent)(this, 'itemIcon'),
        expandIcon: (0, _propsUtil.getComponent)(this, 'expandIcon'),
        children: children,
        onClick: this.onSubMenuClick,
        onSelect: props.onSelect || _util.noop,
        onDeselect: props.onDeselect || _util.noop,
        onOpenChange: props.onOpenChange || _util.noop,
        id: this.internalMenuId
      };
      var haveRendered = this.haveRendered;
      this.haveRendered = true;
      this.haveOpened = this.haveOpened || subPopupMenuProps.visible || subPopupMenuProps.forceSubMenuRender; // never rendered not planning to, don't render

      if (!this.haveOpened) {
        return _vue.createVNode("div", null, null);
      } // don't show transition on first rendering (no animation for opened menu)
      // show appear transition if it's not visible (not sure why)
      // show appear transition if it's not inline mode


      var transitionAppear = haveRendered || !subPopupMenuProps.visible || !subPopupMenuProps.mode === 'inline';
      subPopupMenuProps.class = " ".concat(subPopupMenuProps.prefixCls, "-sub");
      var transitionProps = {
        appear: transitionAppear,
        css: false
      };

      if (subPopupMenuProps.openTransitionName) {
        transitionProps = (0, _getTransitionProps.default)(subPopupMenuProps.openTransitionName, {
          appear: transitionAppear
        });
      } else if (_typeof(subPopupMenuProps.openAnimation) === 'object') {
        transitionProps = _objectSpread(_objectSpread({}, transitionProps), subPopupMenuProps.openAnimation || {});

        if (!transitionAppear) {
          transitionProps.appear = false;
        }
      } else if (typeof subPopupMenuProps.openAnimation === 'string') {
        transitionProps = (0, _getTransitionProps.default)(subPopupMenuProps.openAnimation, {
          appear: transitionAppear
        });
      }

      return _vue.createVNode(_vue.Transition, transitionProps, {
        default: function _default() {
          return [_vue.withDirectives(_vue.createVNode(_SubPopupMenu.default, subPopupMenuProps, null), [[_vue.vShow, props.isOpen]])];
        }
      });
    }
  },
  render: function render() {
    var _className;

    var props = _objectSpread(_objectSpread({}, this.$props), this.$attrs);

    var _splitAttrs = (0, _propsUtil.splitAttrs)(props),
        onEvents = _splitAttrs.onEvents;

    var rootPrefixCls = this.rootPrefixCls,
        parentMenu = this.parentMenu;
    var isOpen = props.isOpen;
    var prefixCls = this.getPrefixCls();
    var isInlineMode = props.mode === 'inline';
    var className = (_className = {}, _defineProperty(_className, prefixCls, true), _defineProperty(_className, "".concat(prefixCls, "-").concat(props.mode), true), _defineProperty(_className, props.class, !!props.class), _defineProperty(_className, this.getOpenClassName(), isOpen), _defineProperty(_className, this.getActiveClassName(), props.active || isOpen && !isInlineMode), _defineProperty(_className, this.getDisabledClassName(), props.disabled), _defineProperty(_className, this.getSelectedClassName(), this.isChildrenSelected()), _className);

    if (!this.internalMenuId) {
      if (props.eventKey) {
        this.internalMenuId = "".concat(props.eventKey, "$Menu");
      } else {
        this.internalMenuId = "$__$".concat(++guid, "$Menu");
      }
    }

    var mouseEvents = {};
    var titleClickEvents = {};
    var titleMouseEvents = {};

    if (!props.disabled) {
      mouseEvents = {
        onMouseleave: this.onMouseLeave,
        onMouseenter: this.onMouseEnter
      }; // only works in title, not outer li

      titleClickEvents = {
        onClick: this.onTitleClick
      };
      titleMouseEvents = {
        onMouseenter: this.onTitleMouseEnter,
        onMouseleave: this.onTitleMouseLeave
      };
    }

    var style = {};

    if (isInlineMode) {
      style.paddingLeft = "".concat(props.inlineIndent * props.level, "px");
    }

    var ariaOwns = {}; // only set aria-owns when menu is open
    // otherwise it would be an invalid aria-owns value
    // since corresponding node cannot be found

    if (isOpen) {
      ariaOwns = {
        'aria-owns': this.internalMenuId
      };
    }

    var titleProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread({
      'aria-expanded': isOpen
    }, ariaOwns), {}, {
      'aria-haspopup': 'true',
      title: typeof props.title === 'string' ? props.title : undefined
    }, titleMouseEvents), titleClickEvents), {}, {
      style: style,
      class: "".concat(prefixCls, "-title"),
      ref: this.saveSubMenuTitle
    }); // expand custom icon should NOT be displayed in menu with horizontal mode.


    var icon = null;

    if (props.mode !== 'horizontal') {
      icon = (0, _propsUtil.getComponent)(this, 'expandIcon', props);
    }

    var title = _vue.createVNode("div", titleProps, [(0, _propsUtil.getComponent)(this, 'title'), icon || _vue.createVNode("i", {
      "class": "".concat(prefixCls, "-arrow")
    }, null)]);

    var children = this.renderChildren((0, _propsUtil.filterEmpty)((0, _propsUtil.getSlot)(this)));
    var getPopupContainer = this.parentMenu.isRootMenu ? this.parentMenu.getPopupContainer : function (triggerNode) {
      return triggerNode.parentNode;
    };
    var popupPlacement = popupPlacementMap[props.mode];
    var popupAlign = props.popupOffset ? {
      offset: props.popupOffset
    } : {};
    var popupClassName = props.mode === 'inline' ? '' : props.popupClassName || '';
    popupClassName = "".concat(prefixCls, "-popup ").concat(rootPrefixCls, "-").concat(parentMenu.theme, " ").concat(popupClassName);

    var liProps = _objectSpread(_objectSpread(_objectSpread({}, (0, _omit.default)(onEvents, ['onClick'])), mouseEvents), {}, {
      class: className,
      style: props.style
    });

    return _vue.createVNode("li", _vue.mergeProps(liProps, {
      "role": "menuitem"
    }), [isInlineMode && title, isInlineMode && children, !isInlineMode && _vue.createVNode(_vcTrigger.default, {
      "prefixCls": prefixCls,
      "popupClassName": popupClassName,
      "getPopupContainer": getPopupContainer,
      "builtinPlacements": _placements.default,
      "popupPlacement": popupPlacement,
      "popupVisible": isOpen,
      "popupAlign": popupAlign,
      "action": props.disabled ? [] : [props.triggerSubMenuAction],
      "mouseEnterDelay": props.subMenuOpenDelay,
      "mouseLeaveDelay": props.subMenuCloseDelay,
      "onPopupVisibleChange": this.onPopupVisibleChange,
      "forceRender": props.forceSubMenuRender,
      "popup": children
    }, {
      default: function _default() {
        return [title];
      }
    })]);
  }
};
var connected = (0, _store.connect)(function (_ref, _ref2) {
  var openKeys = _ref.openKeys,
      activeKey = _ref.activeKey,
      selectedKeys = _ref.selectedKeys;
  var eventKey = _ref2.eventKey,
      subMenuKey = _ref2.subMenuKey;
  return {
    isOpen: openKeys.indexOf(eventKey) > -1,
    active: activeKey[subMenuKey] === eventKey,
    selectedKeys: selectedKeys
  };
})(SubMenu);
connected.isSubMenu = true;
var _default2 = connected;
exports.default = _default2;