import { defineComponent, shallowRef, computed, watch, onBeforeUnmount, createVNode } from 'vue';
import '../../../utils/index.mjs';
import '../../../vue-components/vc-trigger/index.mjs';
import '../../../vue-components/transition/index.mjs';
import { useInjectMenu, useInjectForceRender } from './hooks/useMenuContext.mjs';
import { placementsRtl, placements } from './placements.mjs';
import { raf } from '../../../utils/vue/raf.mjs';
import { getTransitionProps } from '../../../vue-components/transition/src/transition.mjs';
import Trigger from '../../../vue-components/vc-trigger/src/Trigger.mjs';
import { classNames } from '../../../utils/dom/class.mjs';

const popupPlacementMap = {
  "horizontal": "bottomLeft",
  "vertical": "rightTop",
  "vertical-left": "rightTop",
  "vertical-right": "leftTop"
};
var PopupTrigger = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "PopupTrigger",
  inheritAttrs: false,
  props: {
    prefixCls: String,
    mode: String,
    visible: Boolean,
    // popup: React.ReactNode;
    popupClassName: String,
    popupOffset: Array,
    disabled: Boolean,
    onVisibleChange: Function
  },
  slots: Object,
  emits: ["visibleChange"],
  setup(props, {
    slots,
    emit
  }) {
    const innerVisible = shallowRef(false);
    const {
      getPopupContainer,
      rtl,
      subMenuOpenDelay,
      subMenuCloseDelay,
      builtinPlacements,
      triggerSubMenuAction,
      forceSubMenuRender,
      motion,
      defaultMotions,
      rootClassName
    } = useInjectMenu();
    const forceRender = useInjectForceRender();
    const placement = computed(() => rtl.value ? {
      ...placementsRtl,
      ...builtinPlacements.value
    } : {
      ...placements,
      ...builtinPlacements.value
    });
    const popupPlacement = computed(() => popupPlacementMap[props.mode]);
    const visibleRef = shallowRef();
    watch(() => props.visible, (visible) => {
      raf.cancel(visibleRef.value);
      visibleRef.value = raf(() => {
        innerVisible.value = visible;
      });
    }, {
      immediate: true
    });
    onBeforeUnmount(() => {
      raf.cancel(visibleRef.value);
    });
    const onVisibleChange = (visible) => {
      emit("visibleChange", visible);
    };
    const mergedMotion = computed(() => {
      var _a, _b;
      const m = motion.value || ((_a = defaultMotions.value) == null ? void 0 : _a[props.mode]) || ((_b = defaultMotions.value) == null ? void 0 : _b.other);
      const res = typeof m === "function" ? m() : m;
      return res ? getTransitionProps(res.name, {
        css: true
      }) : void 0;
    });
    return () => {
      const {
        prefixCls,
        popupClassName,
        mode,
        popupOffset,
        disabled
      } = props;
      return createVNode(Trigger, {
        "prefixCls": prefixCls,
        "popupClassName": classNames(`${prefixCls}-popup`, {
          [`${prefixCls}-rtl`]: rtl.value
        }, popupClassName, rootClassName.value),
        "stretch": mode === "horizontal" ? "minWidth" : null,
        "getPopupContainer": getPopupContainer.value,
        "builtinPlacements": placement.value,
        "popupPlacement": popupPlacement.value,
        "popupVisible": innerVisible.value,
        "popupAlign": popupOffset && {
          offset: popupOffset
        },
        "action": disabled ? [] : [triggerSubMenuAction.value],
        "mouseEnterDelay": subMenuOpenDelay.value,
        "mouseLeaveDelay": subMenuCloseDelay.value,
        "onPopupVisibleChange": onVisibleChange,
        "forceRender": forceRender || forceSubMenuRender.value,
        "popupAnimation": mergedMotion.value
      }, {
        popup: slots.popup,
        default: slots.default
      });
    };
  }
});

export { PopupTrigger as default };
//# sourceMappingURL=PopupTrigger.mjs.map
