'use strict';

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

var vue = require('vue');
require('../../../../utils/index.cjs');
var Align = require('../../../vc-align/src/Align.cjs');
var motionUtil = require('../utils/motionUtil.cjs');
require('../../../transition/index.cjs');
var _interface = require('./interface.cjs');
var useStretchStyle = require('./useStretchStyle.cjs');
var useVisibleStatus = require('./useVisibleStatus.cjs');
var props = require('../../../../utils/vue/props.cjs');
var _class = require('../../../../utils/dom/class.cjs');
var transition = require('../../../transition/src/transition.cjs');
var supportsPassive = require('../../../../utils/event/supportsPassive.cjs');

var PopupInner = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "PopupInner",
  inheritAttrs: false,
  props: _interface.innerProps,
  emits: ["mouseenter", "mouseleave", "mousedown", "touchstart", "align"],
  setup(props$1, {
    expose,
    attrs,
    slots
  }) {
    const alignRef = vue.shallowRef();
    const elementRef = vue.shallowRef();
    const alignedClassName = vue.shallowRef();
    const [stretchStyle, measureStretchStyle] = useStretchStyle.default(vue.toRef(props$1, "stretch"));
    const doMeasure = () => {
      if (props$1.stretch)
        measureStretchStyle(props$1.getRootDomNode());
    };
    const visible = vue.shallowRef(false);
    let timeoutId;
    vue.watch(() => props$1.visible, (val) => {
      clearTimeout(timeoutId);
      if (val) {
        timeoutId = setTimeout(() => {
          visible.value = props$1.visible;
        });
      } else {
        visible.value = false;
      }
    }, {
      immediate: true
    });
    const [status, goNextStatus] = useVisibleStatus.default(visible, doMeasure);
    const prepareResolveRef = vue.shallowRef();
    const getAlignTarget = () => {
      if (props$1.point)
        return props$1.point;
      return props$1.getRootDomNode;
    };
    const forceAlign = () => {
      var _a;
      (_a = alignRef.value) == null ? void 0 : _a.forceAlign();
    };
    const onInternalAlign = (popupDomNode, matchAlign) => {
      var _a;
      const nextAlignedClassName = props$1.getClassNameFromAlign(matchAlign);
      const preAlignedClassName = alignedClassName.value;
      if (alignedClassName.value !== nextAlignedClassName)
        alignedClassName.value = nextAlignedClassName;
      if (status.value === "align") {
        if (preAlignedClassName !== nextAlignedClassName) {
          Promise.resolve().then(() => {
            forceAlign();
          });
        } else {
          goNextStatus(() => {
            var _a2;
            (_a2 = prepareResolveRef.value) == null ? void 0 : _a2.call(prepareResolveRef);
          });
        }
        (_a = props$1.onAlign) == null ? void 0 : _a.call(props$1, popupDomNode, matchAlign);
      }
    };
    const motion = vue.computed(() => {
      const m = typeof props$1.animation === "object" ? props$1.animation : motionUtil.getMotion(props$1);
      ["onAfterEnter", "onAfterLeave"].forEach((eventName) => {
        const originFn = m[eventName];
        m[eventName] = (node) => {
          goNextStatus();
          status.value = "stable";
          originFn == null ? void 0 : originFn(node);
        };
      });
      return m;
    });
    const onShowPrepare = () => {
      return new Promise((resolve) => {
        prepareResolveRef.value = resolve;
      });
    };
    vue.watch([motion, status], () => {
      if (!motion.value && status.value === "motion")
        goNextStatus();
    }, {
      immediate: true
    });
    expose({
      forceAlign,
      getElement: () => {
        return elementRef.value.$el || elementRef.value;
      }
    });
    const alignDisabled = vue.computed(() => {
      var _a;
      if (((_a = props$1.align) == null ? void 0 : _a.points) && (status.value === "align" || status.value === "stable"))
        return false;
      return true;
    });
    return () => {
      var _a;
      const {
        zIndex,
        align,
        prefixCls,
        destroyPopupOnHide,
        onMouseenter,
        onMouseleave,
        onTouchstart = () => {
        },
        onMousedown
      } = props$1;
      const statusValue = status.value;
      const mergedStyle = [{
        ...stretchStyle.value,
        zIndex,
        opacity: statusValue === "motion" || statusValue === "stable" || !visible.value ? null : 0,
        // pointerEvents: statusValue === 'stable' ? null : 'none',
        pointerEvents: !visible.value && statusValue !== "stable" ? "none" : null
      }, attrs.style];
      let childNode = props.flattenChildren((_a = slots.default) == null ? void 0 : _a.call(slots, {
        visible: props$1.visible
      }));
      if (childNode.length > 1) {
        const _childNode = function() {
          return childNode;
        }();
        childNode = vue.createVNode("div", {
          "class": `${prefixCls}-content`
        }, [childNode]);
      }
      const mergedClassName = _class.classNames(prefixCls, attrs.class, alignedClassName.value);
      const hasAnimate = visible.value || !props$1.visible;
      const transitionProps = hasAnimate ? transition.getTransitionProps(motion.value.name, motion.value) : {};
      return vue.createVNode(vue.Transition, {
        "ref": elementRef,
        ...transitionProps,
        "onBeforeEnter": onShowPrepare
      }, {
        default: () => {
          return !destroyPopupOnHide || props$1.visible ? vue.withDirectives(vue.createVNode(Align.default, {
            "target": getAlignTarget(),
            "key": "popup",
            "ref": alignRef,
            "monitorWindowResize": true,
            "disabled": alignDisabled.value,
            "align": align,
            "onAlign": onInternalAlign
          }, {
            default: () => vue.createVNode("div", {
              "class": mergedClassName,
              "onMouseenter": onMouseenter,
              "onMouseleave": onMouseleave,
              "onMousedown": vue.withModifiers(onMousedown, ["capture"]),
              [supportsPassive.supportsPassive ? "onTouchstartPassive" : "onTouchstart"]: vue.withModifiers(onTouchstart, ["capture"]),
              "style": mergedStyle
            }, [childNode])
          }), [[vue.vShow, visible.value]]) : null;
        }
      });
    };
  }
});

exports.default = PopupInner;
//# sourceMappingURL=PopupInner.cjs.map
