import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { withDirectives as _withDirectives, resolveDirective as _resolveDirective, vShow as _vShow, createVNode as _createVNode } from "vue";
import useVisibleStatus from './useVisibleStatus';
import useStretchStyle from './useStretchStyle';
import { computed, defineComponent, nextTick, ref, toRef, Transition, watch, withModifiers } from 'vue';
import Align from '../../vc-align/Align';
import { getMotion } from '../utils/motionUtil';
import { flattenChildren } from '../../_util/props-util';
import classNames from '../../_util/classNames';
import { innerProps } from './interface';
import { getTransitionProps } from '../../_util/transition';
import supportsPassive from '../../_util/supportsPassive';
export default defineComponent({
  name: 'PopupInner',
  inheritAttrs: false,
  props: innerProps,
  emits: ['mouseenter', 'mouseleave', 'mousedown', 'touchstart', 'align'],
  setup: function setup(props, _ref) {
    var expose = _ref.expose,
        attrs = _ref.attrs,
        slots = _ref.slots;
    var alignRef = ref();
    var elementRef = ref();
    var alignedClassName = ref(); // ======================= Measure ========================

    var _useStretchStyle = useStretchStyle(toRef(props, 'stretch')),
        _useStretchStyle2 = _slicedToArray(_useStretchStyle, 2),
        stretchStyle = _useStretchStyle2[0],
        measureStretchStyle = _useStretchStyle2[1];

    var doMeasure = function doMeasure() {
      if (props.stretch) {
        measureStretchStyle(props.getRootDomNode());
      }
    }; // ======================== Status ========================


    var _useVisibleStatus = useVisibleStatus(toRef(props, 'visible'), doMeasure),
        _useVisibleStatus2 = _slicedToArray(_useVisibleStatus, 2),
        status = _useVisibleStatus2[0],
        goNextStatus = _useVisibleStatus2[1]; // ======================== Aligns ========================


    var prepareResolveRef = ref(); // `target` on `rc-align` can accept as a function to get the bind element or a point.
    // ref: https://www.npmjs.com/package/rc-align

    var getAlignTarget = function getAlignTarget() {
      if (props.point) {
        return props.point;
      }

      return props.getRootDomNode;
    };

    var forceAlign = function forceAlign() {
      var _alignRef$value;

      (_alignRef$value = alignRef.value) === null || _alignRef$value === void 0 ? void 0 : _alignRef$value.forceAlign();
    };

    var onInternalAlign = function onInternalAlign(popupDomNode, matchAlign) {
      var nextAlignedClassName = props.getClassNameFromAlign(matchAlign);
      var preAlignedClassName = alignedClassName.value;

      if (alignedClassName.value !== nextAlignedClassName) {
        alignedClassName.value = nextAlignedClassName;
      }

      if (status.value === 'align') {
        var _props$onAlign;

        // Repeat until not more align needed
        if (preAlignedClassName !== nextAlignedClassName) {
          nextTick(function () {
            forceAlign();
          });
        } else {
          goNextStatus(function () {
            var _prepareResolveRef$va;

            (_prepareResolveRef$va = prepareResolveRef.value) === null || _prepareResolveRef$va === void 0 ? void 0 : _prepareResolveRef$va.call(prepareResolveRef);
          });
        }

        (_props$onAlign = props.onAlign) === null || _props$onAlign === void 0 ? void 0 : _props$onAlign.call(props, popupDomNode, matchAlign);
      }
    }; // ======================== Motion ========================


    var motion = computed(function () {
      var m = getMotion(props);
      ['onAfterEnter', 'onAfterLeave'].forEach(function (eventName) {
        m[eventName] = function () {
          goNextStatus();
        };
      });
      return m;
    });

    var onShowPrepare = function onShowPrepare() {
      return new Promise(function (resolve) {
        prepareResolveRef.value = resolve;
      });
    };

    watch([toRef(motion.value, 'name'), status], function () {
      if (!motion.value.name && status.value === 'motion') {
        goNextStatus();
      }
    }, {
      immediate: true
    });
    expose({
      forceAlign: forceAlign,
      getElement: function getElement() {
        return elementRef.value.$el || elementRef.value;
      }
    });
    return function () {
      var _slots$default;

      var _ref2 = props,
          zIndex = _ref2.zIndex,
          visible = _ref2.visible,
          align = _ref2.align,
          prefixCls = _ref2.prefixCls,
          destroyPopupOnHide = _ref2.destroyPopupOnHide,
          onMouseenter = _ref2.onMouseenter,
          onMouseleave = _ref2.onMouseleave,
          onTouchstart = _ref2.onTouchstart,
          onMousedown = _ref2.onMousedown;
      var statusValue = status.value; // ======================== Render ========================

      var mergedStyle = _objectSpread(_objectSpread({}, stretchStyle.value), {}, {
        zIndex: zIndex,
        opacity: statusValue === 'motion' || statusValue === 'stable' || !visible ? undefined : 0,
        pointerEvents: statusValue === 'stable' ? undefined : 'none'
      }, attrs.style); // Align statusValue


      var alignDisabled = true;

      if (align !== null && align !== void 0 && align.points && (statusValue === 'align' || statusValue === 'stable')) {
        alignDisabled = false;
      }

      var childNode = flattenChildren((_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots)); // Wrapper when multiple children

      if (childNode.length > 1) {
        var _childNode = function () {
          return childNode;
        }();

        childNode = _createVNode("div", {
          "class": "".concat(prefixCls, "-content")
        }, [childNode]);
      }

      var mergedClassName = classNames(prefixCls, attrs.class, alignedClassName.value);
      var transitionProps = getTransitionProps(motion.value.name, motion.value);
      return _createVNode(Transition, _objectSpread(_objectSpread({
        "ref": elementRef
      }, transitionProps), {}, {
        "onBeforeEnter": onShowPrepare
      }), {
        default: function _default() {
          return !destroyPopupOnHide || visible ? _withDirectives(_createVNode(Align, {
            "target": getAlignTarget(),
            "key": "popup",
            "ref": alignRef,
            "monitorWindowResize": true,
            "disabled": alignDisabled,
            "align": align,
            "onAlign": onInternalAlign
          }, {
            default: function _default() {
              return _createVNode("div", _objectSpread(_objectSpread({
                "class": mergedClassName,
                "onMouseenter": onMouseenter,
                "onMouseleave": onMouseleave,
                "onMousedown": withModifiers(onMousedown, ['capture'])
              }, _defineProperty({}, supportsPassive ? 'onTouchstartPassive' : 'onTouchstart', withModifiers(onTouchstart, ['capture']))), {}, {
                "style": mergedStyle
              }), [childNode]);
            }
          }), [[_vShow, visible]]) : null;
        }
      });
    };
  }
});