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

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, inject, computed, ref, nextTick, watch, toRefs, provide, reactive, onMounted, getCurrentInstance, createVNode, mergeProps } from 'vue';
import isFunction from 'lodash/isFunction';
import { renderContent, renderTNodeJSX } from '../utils/render-tnode.js';
import FakeArrow from '../common-components/fake-arrow.js';
import useRipple from '../hooks/useRipple.js';
import { usePrefixClass } from '../hooks/useConfig.js';
import { Popup } from '../popup/index.js';
import props from './submenu-props.js';
import 'lodash/isEmpty';
import 'lodash/isString';
import 'lodash/isObject';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../utils/set-style.js';
import '../hooks/useKeepAnimation.js';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import 'lodash/isArray';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-2e5d8948.js';
import 'dayjs';
import '../_chunks/dep-4580aabe.js';
import '../config-provider/useConfig.js';
import 'lodash/cloneDeep';
import '../config-provider/type.js';
import '../utils/withInstall.js';
import '../popup/popup.js';
import '@babel/runtime/helpers/slicedToArray';
import '@popperjs/core';
import '../hooks/index.js';
import '../hooks/icon.js';
import '../hooks/slot.js';
import '../hooks/tnode.js';
import '../hooks/useCommonClassName.js';
import '../hooks/useDefaultValue.js';
import '../hooks/useDestroyOnClose.js';
import '../hooks/useVirtualScroll.js';
import '../hooks/useVModel.js';
import '../utils/dom.js';
import '../utils/easing.js';
import '../_common/js/utils/set-style.js';
import '../popup/container.js';
import '../hooks/useResizeObserver.js';
import '../popup/props.js';
import '../popup/style';
import '../popup/type.js';

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(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 = defineComponent({
  name: "TSubmenu",
  props: props,
  setup: function setup(props2, ctx) {
    var classPrefix = usePrefixClass();
    var menu = inject("TdMenu");
    var theme = menu.theme,
      activeValues = menu.activeValues,
      expandValues = menu.expandValues,
      isHead = menu.isHead,
      open = menu.open;
    var submenu = inject("TdSubmenu", {});
    var setSubPopup = submenu.setSubPopup,
      closeParentPopup = submenu.closeParentPopup;
    var mode = computed(function () {
      return ctx.attrs.expandType || menu.mode.value;
    });
    var menuItems = ref([]);
    var isActive = computed(function () {
      return activeValues.value.includes(props2.value);
    });
    var popupVisible = ref(false);
    var isCursorInPopup = ref(false);
    var rippleColor = computed(function () {
      return theme.value === "light" ? "#E7E7E7" : "#383838";
    });
    var isOpen = 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 = ref(false);
    var popupWrapperRef = ref();
    var subPopupRef = ref();
    var submenuRef = ref();
    useRipple(submenuRef, rippleColor);
    var classes = computed(function () {
      var _ref;
      return ["".concat(classPrefix.value, "-submenu"), (_ref = {}, _defineProperty(_ref, "".concat(classPrefix.value, "-is-disabled"), props2.disabled), _defineProperty(_ref, "".concat(classPrefix.value, "-is-opened"), isOpen.value), _ref)];
    });
    var popupClass = computed(function () {
      return ["".concat(classPrefix.value, "-menu__popup"), "".concat(classPrefix.value, "-is-").concat(isHead ? "horizontal" : "vertical"), _defineProperty({}, "".concat(classPrefix.value, "-is-opened"), popupVisible.value), "narrow-scrollbar"];
    });
    var submenuClass = 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(_ref3, "".concat(classPrefix.value, "-is-disabled"), props2.disabled), _defineProperty(_ref3, "".concat(classPrefix.value, "-is-opened"), isOpen.value), _defineProperty(_ref3, "".concat(classPrefix.value, "-is-active"), isActive.value), _ref3)];
    });
    var subClass = computed(function () {
      return ["".concat(classPrefix.value, "-menu__sub"), _defineProperty({}, "".concat(classPrefix.value, "-is-opened"), isOpen.value)];
    });
    var arrowClass = computed(function () {
      return [_defineProperty({}, "".concat(classPrefix.value, "-fake-arrow--active"), isOpen.value)];
    });
    var passSubPopupRefToParent = function passSubPopupRefToParent(val) {
      if (isFunction(setSubPopup)) setSubPopup(val);
    };
    var handleMouseEnter = function handleMouseEnter() {
      if (props2.disabled) return;
      setTimeout(function () {
        if (!popupVisible.value) {
          open(props2.value);
          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);
    };
    watch(popupVisible, function (visible) {
      menu.open(props2.value, visible ? "add" : "remove");
    });
    var _toRefs = toRefs(props2),
      value = _toRefs.value;
    provide("TdSubmenu", 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);
      }
    }));
    watch(popupWrapperRef, function () {
      passSubPopupRefToParent(popupWrapperRef.value);
    });
    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 = 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 = 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
      }, [createVNode("ul", {
        "class": "".concat(this.classPrefix, "-menu__popup-wrapper narrow-scrollbar")
      }, [renderContent(this, "default", "content")])]);
      var slots = {
        content: function content() {
          return popupWrapper;
        }
      };
      var realPopup = createVNode(Popup, {
        "overlayInnerClassName": _toConsumableArray(this.popupClass),
        "overlayClassName": "".concat(this.classPrefix, "-menu--").concat(this.theme),
        "visible": this.popupVisible,
        "placement": placement,
        "overlayInnerStyle": overlayInnerStyle
      }, _objectSpread({
        "default": function _default() {
          return [createVNode("div", {
            "ref": "submenuRef",
            "class": _this.submenuClass
          }, [triggerElement])];
        }
      }, slots));
      return realPopup;
    },
    renderHeadSubmenu: function renderHeadSubmenu() {
      var icon = renderTNodeJSX(this, "icon");
      var normalSubmenu = [createVNode("div", {
        "ref": "submenuRef",
        "class": this.submenuClass,
        "onClick": this.handleSubmenuItemClick
      }, [icon, createVNode("span", {
        "class": ["".concat(this.classPrefix, "-menu__content")]
      }, [renderTNodeJSX(this, "title", {
        silent: true
      })])]), createVNode("ul", {
        "style": "opacity: 0; width: 0; height: 0; overflow: hidden"
      }, [renderContent(this, "default", "content")])];
      var needRotate = this.mode === "popup" && this.isNested;
      var triggerElement = [icon, createVNode("span", {
        "class": ["".concat(this.classPrefix, "-menu__content")]
      }, [renderTNodeJSX(this, "title", {
        silent: true
      })]), createVNode(FakeArrow, {
        "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 = renderTNodeJSX(this, "icon");
      var child = renderContent(this, "default", "content");
      var _getCurrentInstance = 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 = [createVNode("div", {
        "ref": "submenuRef",
        "class": this.submenuClass,
        "onClick": this.handleSubmenuItemClick
      }, [icon, createVNode("span", {
        "class": ["".concat(this.classPrefix, "-menu__content")]
      }, [renderTNodeJSX(this, "title", {
        silent: true
      })]), hasContent && createVNode(FakeArrow, {
        "overlayClassName": this.arrowClass,
        "overlayStyle": {
          transform: "rotate(".concat(needRotate ? -90 : 0, "deg)")
        }
      }, null)]), createVNode("ul", {
        "class": this.subClass,
        "style": {
          "--padding-left": "".concat(paddingLeft, "px")
        }
      }, [child])];
      var triggerElement = [icon, createVNode("span", {
        "class": ["".concat(this.classPrefix, "-menu__content")]
      }, [renderTNodeJSX(this, "title", {
        silent: true
      })]), createVNode(FakeArrow, {
        "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 createVNode("li", mergeProps({
      "class": this.classes
    }, events), [child]);
  }
});

export { _Submenu as default };
//# sourceMappingURL=submenu.js.map
