import { o as v } from "./rolldown-runtime.js";
import { i as _, r as h } from "./vendor/react.js";
import { n as S, t as m } from "./useIsoLayoutEffect.js";
import { n as b, t as f } from "./useAnimationFrame.js";
var d = /* @__PURE__ */ v(_()), O = /* @__PURE__ */ v(h());
function p(t) {
  return t == null ? t : "current" in t ? t.current : t;
}
var g = /* @__PURE__ */ (function(t) {
  return t.startingStyle = "data-starting-style", t.endingStyle = "data-ending-style", t;
})({}), y = { [g.startingStyle]: "" }, C = { [g.endingStyle]: "" }, T = { transitionStatus(t) {
  return t === "starting" ? y : t === "ending" ? C : null;
} };
function q(t, s = !1, o = !0) {
  const e = b();
  return S((i, r = null) => {
    e.cancel();
    const a = p(t);
    if (a == null) return;
    const n = a, A = () => {
      O.flushSync(i);
    };
    if (typeof n.getAnimations != "function" || globalThis.BASE_UI_ANIMATIONS_DISABLED) {
      i();
      return;
    }
    function l() {
      Promise.all(n.getAnimations().map((u) => u.finished)).then(() => {
        r?.aborted || A();
      }).catch(() => {
        if (o) {
          r?.aborted || A();
          return;
        }
        const u = n.getAnimations();
        !r?.aborted && u.length > 0 && u.some((c) => c.pending || c.playState !== "finished") && l();
      });
    }
    if (s) {
      const u = g.startingStyle;
      if (!n.hasAttribute(u)) {
        e.request(l);
        return;
      }
      const c = new MutationObserver(() => {
        n.hasAttribute(u) || (c.disconnect(), l());
      });
      c.observe(n, {
        attributes: !0,
        attributeFilter: [u]
      }), r?.addEventListener("abort", () => c.disconnect(), { once: !0 });
      return;
    }
    e.request(l);
  });
}
function I(t) {
  const { enabled: s = !0, open: o, ref: e, onComplete: i } = t, r = S(i), a = q(e, o, !1);
  d.useEffect(() => {
    if (!s) return;
    const n = new AbortController();
    return a(r, n.signal), () => {
      n.abort();
    };
  }, [
    s,
    o,
    r,
    a
  ]);
}
function L(t, s = !1, o = !1) {
  const [e, i] = d.useState(t && s ? "idle" : void 0), [r, a] = d.useState(t);
  return t && !r && (a(!0), i("starting")), !t && r && e !== "ending" && !o && i("ending"), !t && !r && e === "ending" && i(void 0), m(() => {
    if (!t && r && e !== "ending" && o) {
      const n = f.request(() => {
        i("ending");
      });
      return () => {
        f.cancel(n);
      };
    }
  }, [
    t,
    r,
    e,
    o
  ]), m(() => {
    if (!t || s) return;
    const n = f.request(() => {
      i(void 0);
    });
    return () => {
      f.cancel(n);
    };
  }, [s, t]), m(() => {
    if (!t || !s) return;
    t && r && e !== "idle" && i("starting");
    const n = f.request(() => {
      i("idle");
    });
    return () => {
      f.cancel(n);
    };
  }, [
    s,
    t,
    r,
    e
  ]), {
    mounted: r,
    setMounted: a,
    transitionStatus: e
  };
}
export {
  T as a,
  g as i,
  I as n,
  p as o,
  q as r,
  L as t
};

//# sourceMappingURL=useTransitionStatus.js.map