import { defineComponent, shallowRef, computed, watch, nextTick, onMounted, onBeforeUnmount, createVNode, Transition, withDirectives, vShow } from 'vue';
import '../../transition/index.mjs';
import TreeNode from './TreeNode.mjs';
import { useInjectTreeContext } from './contextTypes.mjs';
import { treeNodeProps } from './props.mjs';
import { collapseMotion } from '../../transition/src/collapseMotion.mjs';

var MotionTreeNode = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "MotionTreeNode",
  inheritAttrs: false,
  props: {
    ...treeNodeProps,
    active: Boolean,
    motion: Object,
    motionNodes: {
      type: Array
    },
    onMotionStart: Function,
    onMotionEnd: Function,
    motionType: String
    // treeNodeRequiredProps: { type: Object as PropType<TreeNodeRequiredProps> },
  },
  setup(props, {
    attrs,
    slots
  }) {
    const visible = shallowRef(true);
    const context = useInjectTreeContext();
    const motionedRef = shallowRef(false);
    const transitionProps = computed(() => {
      if (props.motion)
        return props.motion;
      else
        return collapseMotion();
    });
    const onMotionEnd = (node, type) => {
      var _a, _b, _c, _d;
      if (type === "appear")
        (_b = (_a = transitionProps.value) == null ? void 0 : _a.onAfterEnter) == null ? void 0 : _b.call(_a, node);
      else if (type === "leave")
        (_d = (_c = transitionProps.value) == null ? void 0 : _c.onAfterLeave) == null ? void 0 : _d.call(_c, node);
      if (!motionedRef.value)
        props.onMotionEnd();
      motionedRef.value = true;
    };
    watch(() => props.motionNodes, () => {
      if (props.motionNodes && props.motionType === "hide" && visible.value) {
        nextTick(() => {
          visible.value = false;
        });
      }
    }, {
      immediate: true,
      flush: "post"
    });
    onMounted(() => {
      props.motionNodes && props.onMotionStart();
    });
    onBeforeUnmount(() => {
      props.motionNodes && onMotionEnd();
    });
    return () => {
      const {
        motion,
        motionNodes,
        motionType,
        active,
        eventKey,
        ...otherProps
      } = props;
      if (motionNodes) {
        return createVNode(Transition, {
          ...transitionProps.value,
          "appear": motionType === "show",
          "onAfterAppear": (node) => onMotionEnd(node, "appear"),
          "onAfterLeave": (node) => onMotionEnd(node, "leave")
        }, {
          default: () => [withDirectives(createVNode("div", {
            "class": `${context.value.prefixCls}-treenode-motion`
          }, [motionNodes.map((treeNode) => {
            const {
              data: {
                ...restProps
              },
              title,
              key,
              isStart,
              isEnd
            } = treeNode;
            delete restProps.children;
            return createVNode(TreeNode, {
              ...restProps,
              "title": title,
              "active": active,
              "data": treeNode.data,
              "key": key,
              "eventKey": key,
              "isStart": isStart,
              "isEnd": isEnd
            }, slots);
          })]), [[vShow, visible.value]])]
        });
      }
      return createVNode(TreeNode, {
        "class": attrs.class,
        "style": attrs.style,
        ...otherProps,
        "active": active,
        "eventKey": eventKey
      }, slots);
    };
  }
});

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