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