/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var isFunction = require('lodash/isFunction');
var utils_renderTnode = require('../utils/render-tnode.js');
var commonComponents_fakeArrow = require('../common-components/fake-arrow.js');
var hooks_useRipple = require('../hooks/useRipple.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var popup_index = require('../popup/index.js');
var menu_submenuProps = require('./submenu-props.js');
require('lodash/isEmpty');
require('lodash/isString');
require('lodash/isObject');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../utils/set-style.js');
require('../hooks/useKeepAnimation.js');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('lodash/isArray');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../_chunks/dep-f4db2590.js');
require('dayjs');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('../utils/withInstall.js');
require('../popup/popup.js');
require('@babel/runtime/helpers/slicedToArray');
require('@popperjs/core');
require('../utils/dom.js');
require('../utils/easing.js');
require('../_common/js/utils/set-style.js');
require('../hooks/useVModel.js');
require('../popup/props.js');
require('../popup/container.js');
require('lodash/isUndefined');
require('../hooks/useResizeObserver.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var _Submenu = vue.defineComponent({
  name: "TSubmenu",
  props: menu_submenuProps["default"],
  setup: function setup(props2, ctx) {
    var classPrefix = hooks_useConfig.usePrefixClass();
    var menu = vue.inject("TdMenu");
    var theme = menu.theme,
      activeValues = menu.activeValues,
      expandValues = menu.expandValues,
      isHead = menu.isHead,
      open = menu.open;
    var submenu = vue.inject("TdSubmenu", {});
    var setSubPopup = submenu.setSubPopup,
      closeParentPopup = submenu.closeParentPopup;
    var mode = vue.computed(function () {
      return ctx.attrs.expandType || menu.mode.value;
    });
    var menuItems = vue.ref([]);
    var isActive = vue.computed(function () {
      return activeValues.value.includes(props2.value);
    });
    var popupVisible = vue.ref(false);
    var isCursorInPopup = vue.ref(false);
    var rippleColor = vue.computed(function () {
      return theme.value === "light" ? "#E7E7E7" : "#383838";
    });
    var isOpen = vue.computed(function () {
      var _expandValues$value;
      if (mode.value === "popup") return popupVisible.value;
      return expandValues ? (_expandValues$value = expandValues.value) === null || _expandValues$value === void 0 ? void 0 : _expandValues$value.includes(props2.value) : false;
    });
    var isNested = vue.ref(false);
    var popupWrapperRef = vue.ref();
    var subPopupRef = vue.ref();
    var submenuRef = vue.ref();
    hooks_useRipple["default"](submenuRef, rippleColor);
    var classes = vue.computed(function () {
      var _ref;
      return ["".concat(classPrefix.value, "-submenu"), (_ref = {}, _defineProperty__default["default"](_ref, "".concat(classPrefix.value, "-is-disabled"), props2.disabled), _defineProperty__default["default"](_ref, "".concat(classPrefix.value, "-is-opened"), isOpen.value), _ref)];
    });
    var popupClass = vue.computed(function () {
      return ["".concat(classPrefix.value, "-menu__popup"), "".concat(classPrefix.value, "-is-").concat(isHead ? "horizontal" : "vertical"), _defineProperty__default["default"]({}, "".concat(classPrefix.value, "-is-opened"), popupVisible.value), "narrow-scrollbar"];
    });
    var submenuClass = vue.computed(function () {
      var _ref3;
      return ["".concat(classPrefix.value, "-menu__item"), "".concat(classPrefix.value, "-menu__item-spacer"), "".concat(classPrefix.value, "-menu__item-spacer--").concat(isHead && !isNested.value ? "bottom" : "right"), (_ref3 = {}, _defineProperty__default["default"](_ref3, "".concat(classPrefix.value, "-is-disabled"), props2.disabled), _defineProperty__default["default"](_ref3, "".concat(classPrefix.value, "-is-opened"), isOpen.value), _defineProperty__default["default"](_ref3, "".concat(classPrefix.value, "-is-active"), isActive.value), _ref3)];
    });
    var subClass = vue.computed(function () {
      return ["".concat(classPrefix.value, "-menu__sub"), _defineProperty__default["default"]({}, "".concat(classPrefix.value, "-is-opened"), isOpen.value)];
    });
    var arrowClass = vue.computed(function () {
      return [_defineProperty__default["default"]({}, "".concat(classPrefix.value, "-fake-arrow--active"), isOpen.value)];
    });
    var passSubPopupRefToParent = function passSubPopupRefToParent(val) {
      if (isFunction__default["default"](setSubPopup)) setSubPopup(val);
    };
    var handleMouseEnter = function handleMouseEnter() {
      if (props2.disabled) return;
      setTimeout(function () {
        if (!popupVisible.value) {
          open(props2.value);
          vue.nextTick().then(function () {
            passSubPopupRefToParent(popupWrapperRef.value);
          });
        }
        popupVisible.value = true;
      }, 0);
    };
    var targetInPopup = function targetInPopup(el) {
      return el === null || el === void 0 ? void 0 : el.classList.contains("".concat(classPrefix.value, "-menu__popup"));
    };
    var loopInPopup = function loopInPopup(el) {
      if (!el) return false;
      return targetInPopup(el) || loopInPopup(el.parentElement);
    };
    var handleMouseLeave = function handleMouseLeave(e) {
      setTimeout(function () {
        var inPopup = targetInPopup(e.relatedTarget);
        if (isCursorInPopup.value || inPopup) return;
        popupVisible.value = false;
      }, 0);
    };
    var handleMouseLeavePopup = function handleMouseLeavePopup(e) {
      var toElement = e.toElement,
        relatedTarget = e.relatedTarget;
      var target = toElement || relatedTarget;
      if (target === subPopupRef.value) return;
      var isSubmenu = function isSubmenu(el) {
        return el === submenuRef.value;
      };
      while (target !== null && target !== document && !isSubmenu(target)) target = target.parentNode;
      isCursorInPopup.value = false;
      if (!isSubmenu(target)) popupVisible.value = false;
      closeParentPopup === null || closeParentPopup === void 0 ? void 0 : closeParentPopup(e);
    };
    var handleEnterPopup = function handleEnterPopup() {
      isCursorInPopup.value = true;
    };
    var handleSubmenuItemClick = function handleSubmenuItemClick() {
      if (props2.disabled) return;
      open(props2.value);
    };
    vue.watch(popupVisible, function (visible) {
      menu.open(props2.value, visible ? "add" : "remove");
    });
    var _toRefs = vue.toRefs(props2),
      value = _toRefs.value;
    vue.provide("TdSubmenu", vue.reactive({
      value: value,
      addMenuItem: function addMenuItem(item) {
        menuItems.value.push(item);
        if (submenu) submenu.addMenuItem(item);
      },
      setSubPopup: function setSubPopup(ref2) {
        subPopupRef.value = ref2;
      },
      closeParentPopup: function closeParentPopup(e) {
        var related = e.relatedTarget;
        if (loopInPopup(related)) return;
        handleMouseLeavePopup(e);
      }
    }));
    vue.onMounted(function () {
      var _menu$vMenu;
      menu === null || menu === void 0 ? void 0 : (_menu$vMenu = menu.vMenu) === null || _menu$vMenu === void 0 ? void 0 : _menu$vMenu.add({
        value: props2.value,
        parent: submenu === null || submenu === void 0 ? void 0 : submenu.value,
        vnode: ctx.slots["default"]
      });
      var instance = vue.getCurrentInstance();
      var node = instance.parent;
      while (node && !/^t(head)?menu/i.test((_node = node) === null || _node === void 0 ? void 0 : _node.type.name)) {
        var _node, _node2, _node3;
        if (/submenu/i.test((_node2 = node) === null || _node2 === void 0 ? void 0 : _node2.type.name)) {
          isNested.value = true;
          break;
        }
        node = (_node3 = node) === null || _node3 === void 0 ? void 0 : _node3.parent;
      }
    });
    return {
      classPrefix: classPrefix,
      menuItems: menuItems,
      mode: mode,
      theme: theme,
      isHead: isHead,
      isNested: isNested,
      classes: classes,
      subClass: subClass,
      arrowClass: arrowClass,
      popupClass: popupClass,
      submenuClass: submenuClass,
      submenuRef: submenuRef,
      popupWrapperRef: popupWrapperRef,
      popupVisible: popupVisible,
      isCursorInPopup: isCursorInPopup,
      handleEnterPopup: handleEnterPopup,
      handleMouseEnter: handleMouseEnter,
      handleMouseLeave: handleMouseLeave,
      handleMouseLeavePopup: handleMouseLeavePopup,
      handleSubmenuItemClick: handleSubmenuItemClick
    };
  },
  methods: {
    renderPopup: function renderPopup(triggerElement) {
      var _this = this;
      var placement = "right-top";
      if (!this.isNested && this.isHead) placement = "bottom-left";
      var overlayInnerStyle = this.isNested && this.isHead ? {
        marginLeft: "0px"
      } : {
        "margin-top': ": "12px"
      };
      var popupWrapper = vue.createVNode("div", {
        "ref": "popupWrapperRef",
        "class": ["".concat(this.classPrefix, "-menu__spacer"), "".concat(this.classPrefix, "-menu__spacer--").concat(!this.isNested && this.isHead ? "top" : "left")],
        "onMouseenter": this.handleEnterPopup,
        "onMouseleave": this.handleMouseLeavePopup
      }, [vue.createVNode("ul", {
        "class": "".concat(this.classPrefix, "-menu__popup-wrapper narrow-scrollbar")
      }, [utils_renderTnode.renderContent(this, "default", "content")])]);
      var slots = {
        content: function content() {
          return popupWrapper;
        }
      };
      var realPopup = vue.createVNode(popup_index.Popup, {
        "overlayInnerClassName": _toConsumableArray__default["default"](this.popupClass),
        "overlayClassName": "".concat(this.classPrefix, "-menu--").concat(this.theme),
        "visible": this.popupVisible,
        "placement": placement,
        "overlayInnerStyle": overlayInnerStyle
      }, _objectSpread({
        "default": function _default() {
          return [vue.createVNode("div", {
            "ref": "submenuRef",
            "class": _this.submenuClass
          }, [triggerElement])];
        }
      }, slots));
      return realPopup;
    },
    renderHeadSubmenu: function renderHeadSubmenu() {
      var icon = utils_renderTnode.renderTNodeJSX(this, "icon");
      var normalSubmenu = [vue.createVNode("div", {
        "ref": "submenuRef",
        "class": this.submenuClass,
        "onClick": this.handleSubmenuItemClick
      }, [icon, vue.createVNode("span", {
        "class": ["".concat(this.classPrefix, "-menu__content")]
      }, [utils_renderTnode.renderTNodeJSX(this, "title", {
        silent: true
      })])]), vue.createVNode("ul", {
        "style": "opacity: 0; width: 0; height: 0; overflow: hidden"
      }, [utils_renderTnode.renderContent(this, "default", "content")])];
      var needRotate = this.mode === "popup" && this.isNested;
      var triggerElement = [icon, vue.createVNode("span", {
        "class": ["".concat(this.classPrefix, "-menu__content")]
      }, [utils_renderTnode.renderTNodeJSX(this, "title", {
        silent: true
      })]), vue.createVNode(commonComponents_fakeArrow["default"], {
        "overlayClassName": /menu/i.test(this.$parent.$options.name) ? this.arrowClass : null,
        "overlayStyle": {
          transform: "rotate(".concat(needRotate ? -90 : 0, "deg)")
        }
      }, null)];
      return this.mode === "normal" ? normalSubmenu : this.renderPopup(triggerElement);
    },
    renderSubmenu: function renderSubmenu() {
      var hasContent = this.$slots.content || this.$slots["default"];
      var icon = utils_renderTnode.renderTNodeJSX(this, "icon");
      var child = utils_renderTnode.renderContent(this, "default", "content");
      var _getCurrentInstance = vue.getCurrentInstance(),
        parent = _getCurrentInstance.parent;
      var paddingLeft = 44;
      while (parent && parent.type.name !== "TMenu") {
        if (parent.type.name === "TSubmenu") paddingLeft += 16;
        parent = parent.parent;
      }
      var needRotate = this.mode === "popup" && this.isNested;
      var normalSubmenu = [vue.createVNode("div", {
        "ref": "submenuRef",
        "class": this.submenuClass,
        "onClick": this.handleSubmenuItemClick
      }, [icon, vue.createVNode("span", {
        "class": ["".concat(this.classPrefix, "-menu__content")]
      }, [utils_renderTnode.renderTNodeJSX(this, "title", {
        silent: true
      })]), hasContent && vue.createVNode(commonComponents_fakeArrow["default"], {
        "overlayClassName": this.arrowClass,
        "overlayStyle": {
          transform: "rotate(".concat(needRotate ? -90 : 0, "deg)")
        }
      }, null)]), vue.createVNode("ul", {
        "class": this.subClass,
        "style": {
          "--padding-left": "".concat(paddingLeft, "px")
        }
      }, [child])];
      var triggerElement = [icon, vue.createVNode("span", {
        "class": ["".concat(this.classPrefix, "-menu__content")]
      }, [utils_renderTnode.renderTNodeJSX(this, "title", {
        silent: true
      })]), vue.createVNode(commonComponents_fakeArrow["default"], {
        "overlayClassName": /menu/i.test(this.$parent.$options.name) ? this.arrowClass : null,
        "overlayStyle": {
          "transform": "rotate(".concat(needRotate ? -90 : 0, "deg)"),
          "margin-left": "auto"
        }
      }, null)];
      return this.mode === "normal" ? normalSubmenu : this.renderPopup(triggerElement);
    }
  },
  render: function render() {
    var child = null;
    var events = {};
    if (this.mode === "popup") {
      events = {
        onMouseenter: this.handleMouseEnter,
        onMouseleave: this.handleMouseLeave
      };
    }
    if (Object.keys(this.$slots).length > 0) child = this.isHead ? this.renderHeadSubmenu() : this.renderSubmenu();
    return vue.createVNode("li", vue.mergeProps({
      "class": this.classes
    }, events), [child]);
  }
});

exports["default"] = _Submenu;
//# sourceMappingURL=submenu.js.map
