'use strict';

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

var vue = require('vue');
var motionUtil = require('../utils/motionUtil.js');
var Align = require('../../vc-align/Align.js');
var index = require('../../_util/props-util/index.js');
var classNames = require('../../_util/classNames.js');
var transition = require('../../_util/components/transition.js');
var supportsPassive = require('../../_util/supportsPassive.js');
var _interface = require('./interface.js');
var useStretchStyle = require('./useStretchStyle.js');
var useVisibleStatus = require('./useVisibleStatus.js');

var PopupInner = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "PopupInner",
  inheritAttrs: false,
  props: _interface.innerProps,
  emits: ["mouseenter", "mouseleave", "mousedown", "touchstart", "align"],
  setup(props, {
    expose,
    attrs,
    slots
  }) {
    const alignRef = vue.shallowRef();
    const elementRef = vue.shallowRef();
    const alignedClassName = vue.shallowRef();
    const [stretchStyle, measureStretchStyle] = useStretchStyle.default(vue.toRef(props, "stretch"));
    const doMeasure = () => {
      if (props.stretch)
        measureStretchStyle(props.getRootDomNode());
    };
    const visible = vue.shallowRef(false);
    let timeoutId;
    vue.watch(() => props.visible, (val) => {
      clearTimeout(timeoutId);
      if (val) {
        timeoutId = setTimeout(() => {
          visible.value = props.visible;
        });
      } else {
        visible.value = false;
      }
    }, {
      immediate: true
    });
    const [status, goNextStatus] = useVisibleStatus.default(visible, doMeasure);
    const prepareResolveRef = vue.shallowRef();
    const getAlignTarget = () => {
      if (props.point)
        return props.point;
      return props.getRootDomNode;
    };
    const forceAlign = () => {
      var _a;
      (_a = alignRef.value) == null ? void 0 : _a.forceAlign();
    };
    const onInternalAlign = (popupDomNode, matchAlign) => {
      var _a;
      const nextAlignedClassName = props.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.onAlign) == null ? void 0 : _a.call(props, popupDomNode, matchAlign);
      }
    };
    const motion = vue.computed(() => {
      const m = typeof props.animation === "object" ? props.animation : motionUtil.getMotion(props);
      ["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.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;
      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 = index.flattenChildren((_a = slots.default) == null ? void 0 : _a.call(slots, {
        visible: props.visible
      }));
      if (childNode.length > 1) {
        const _childNode = function() {
          return childNode;
        }();
        childNode = vue.createVNode("div", {
          "class": `${prefixCls}-content`
        }, [childNode]);
      }
      const mergedClassName = classNames.default(prefixCls, attrs.class, alignedClassName.value);
      const hasAnimate = visible.value || !props.visible;
      const transitionProps = hasAnimate ? transition.getTransitionProps(motion.value.name, motion.value) : {};
      return vue.createVNode(vue.Transition, vue.mergeProps({
        "ref": elementRef
      }, transitionProps, {
        "onBeforeEnter": onShowPrepare
      }), {
        default: () => {
          return !destroyPopupOnHide || props.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", vue.mergeProps({
              "class": mergedClassName,
              "onMouseenter": onMouseenter,
              "onMouseleave": onMouseleave,
              "onMousedown": vue.withModifiers(onMousedown, ["capture"])
            }, {
              [supportsPassive.default ? "onTouchstartPassive" : "onTouchstart"]: vue.withModifiers(onTouchstart, ["capture"])
            }, {
              "style": mergedStyle
            }), [childNode])
          }), [[vue.vShow, visible.value]]) : null;
        }
      });
    };
  }
});

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