import * as A from "react-dom";
import { useAnimationFrame as d } from "../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useAnimationFrame.js";
import { useStableCallback as l } from "../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useStableCallback.js";
import { resolveRef as S } from "./resolveRef.js";
import { TransitionStatusDataAttributes as h } from "./stateAttributesMapping.js";
function F(f, a = !1, m = !0) {
  const i = d();
  return l((u, t = null) => {
    i.cancel();
    function s() {
      A.flushSync(u);
    }
    const c = S(f);
    if (c == null)
      return;
    const r = c;
    if (typeof r.getAnimations != "function" || globalThis.BASE_UI_ANIMATIONS_DISABLED)
      u();
    else {
      let b = function() {
        const e = h.startingStyle;
        if (!r.hasAttribute(e)) {
          i.request(o);
          return;
        }
        const n = new MutationObserver(() => {
          r.hasAttribute(e) || (n.disconnect(), o());
        });
        n.observe(r, {
          attributes: !0,
          attributeFilter: [e]
        }), t == null || t.addEventListener("abort", () => n.disconnect(), {
          once: !0
        });
      }, o = function() {
        Promise.all(r.getAnimations().map((e) => e.finished)).then(() => {
          t != null && t.aborted || s();
        }).catch(() => {
          const e = r.getAnimations();
          if (m) {
            if (t != null && t.aborted)
              return;
            s();
          } else e.length > 0 && e.some((n) => n.pending || n.playState !== "finished") && o();
        });
      };
      if (a) {
        b();
        return;
      }
      i.request(o);
    }
  });
}
export {
  F as useAnimationsFinished
};
