"use strict";
const common_vendor = require("../../../../common/vendor.js");
const uni_modules_uviewPro_components_uTransition_types = require("./types.js");
const uni_modules_uviewPro_libs_index = require("../../libs/index.js");
require("../../libs/util/logger.js");
require("../../libs/config/theme-tokens.js");
const __default__ = {
  name: "u-transition",
  options: {
    addGlobalClass: true,
    virtualHost: true,
    styleIsolation: "shared"
  }
};
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
  ...__default__,
  props: uni_modules_uviewPro_components_uTransition_types.TransitionProps,
  emits: [
    "before-enter",
    "enter",
    "after-enter",
    "enter-cancelled",
    "before-leave",
    "leave",
    "after-leave",
    "leave-cancelled"
  ],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const normalizeDuration = (duration) => {
      if (typeof duration === "number") {
        return {
          enter: duration,
          leave: duration
        };
      }
      return {
        enter: (duration == null ? void 0 : duration.enter) ?? 300,
        leave: (duration == null ? void 0 : duration.leave) ?? (duration == null ? void 0 : duration.enter) ?? 300
      };
    };
    const rootRef = common_vendor.ref();
    const rendered = common_vendor.ref(props.show);
    const animationPhase = common_vendor.ref("");
    const animating = common_vendor.ref(false);
    const initialized = common_vendor.ref(false);
    const transitionDuration = common_vendor.computed(() => normalizeDuration(props.duration));
    const animationStyle = common_vendor.computed(() => {
      const currentDuration = animationPhase.value === "leave" ? transitionDuration.value.leave : transitionDuration.value.enter;
      return {
        "--u-transition-duration-enter": `${transitionDuration.value.enter}ms`,
        "--u-transition-duration-leave": `${transitionDuration.value.leave}ms`,
        "--u-transition-delay": `${props.delay}ms`,
        "--u-transition-timing": props.timingFunction,
        animationDuration: `${currentDuration}ms`,
        animationDelay: `${props.delay}ms`,
        animationTimingFunction: props.timingFunction
      };
    });
    const animationClass = common_vendor.computed(() => {
      if (!animationPhase.value) {
        return "";
      }
      return `u-transition-${props.name}-${animationPhase.value}`;
    });
    const getEl = () => rootRef.value;
    const startEnter = () => {
      if (animating.value && animationPhase.value === "enter") {
        return;
      }
      if (animating.value && animationPhase.value === "leave") {
        emit("leave-cancelled", getEl());
      }
      rendered.value = true;
      animationPhase.value = "enter";
      animating.value = true;
      emit("before-enter", getEl());
    };
    const startLeave = () => {
      if (!rendered.value) {
        return;
      }
      if (animating.value && animationPhase.value === "leave") {
        return;
      }
      if (animating.value && animationPhase.value === "enter") {
        emit("enter-cancelled", getEl());
      }
      animationPhase.value = "leave";
      animating.value = true;
      emit("before-leave", getEl());
    };
    const handleAnimationStart = () => {
      if (animationPhase.value === "enter") {
        emit("enter", getEl());
      } else if (animationPhase.value === "leave") {
        emit("leave", getEl());
      }
    };
    const handleAnimationEnd = () => {
      if (animationPhase.value === "enter") {
        animating.value = false;
        animationPhase.value = "";
        emit("after-enter", getEl());
        return;
      }
      if (animationPhase.value === "leave") {
        animating.value = false;
        animationPhase.value = "";
        rendered.value = false;
        emit("after-leave", getEl());
      }
    };
    const shouldWaitForAnimation = (newPhase) => {
      if (!animating.value)
        return false;
      const currentPhase = animationPhase.value;
      if (props.mode === "out-in" && currentPhase === "leave" && newPhase === "enter") {
        return true;
      }
      if (props.mode === "in-out" && currentPhase === "enter" && newPhase === "leave") {
        return true;
      }
      return false;
    };
    common_vendor.watch(
      () => props.show,
      (value) => {
        if (!initialized.value) {
          initialized.value = true;
          if (value) {
            rendered.value = true;
            if (props.appear) {
              common_vendor.nextTick$1(() => startEnter());
            }
          } else {
            rendered.value = false;
          }
          return;
        }
        if (value) {
          if (shouldWaitForAnimation("enter")) {
            common_vendor.nextTick$1(() => {
              if (!animating.value) {
                startEnter();
              }
            });
          } else {
            startEnter();
          }
        } else {
          if (shouldWaitForAnimation("leave")) {
            common_vendor.nextTick$1(() => {
              if (!animating.value) {
                startLeave();
              }
            });
          } else {
            startLeave();
          }
        }
      },
      { immediate: true }
    );
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: rendered.value
      }, rendered.value ? {
        b: common_vendor.n(_ctx.customClass),
        c: common_vendor.n(animationClass.value),
        d: common_vendor.s(common_vendor.unref(uni_modules_uviewPro_libs_index.$u).toStyle(animationStyle.value, _ctx.customStyle)),
        e: common_vendor.o(handleAnimationStart, "e3"),
        f: common_vendor.o(handleAnimationEnd, "be")
      } : {});
    };
  }
});
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-1098449d"]]);
wx.createComponent(Component);
//# sourceMappingURL=../../../../../.sourcemap/mp-weixin/uni_modules/uview-pro/components/u-transition/u-transition.js.map
