import { useState as y, useRef as m } from "react";
import { disposables as v } from "../utils/disposables.js";
import { useDisposables as A } from "./use-disposables.js";
import { useFlags as E } from "./use-flags.js";
import { useIsoMorphicEffect as F } from "./use-iso-morphic-effect.js";
var c, g;
typeof process < "u" && typeof globalThis < "u" && typeof Element < "u" && ((c = process == null ? void 0 : process.env) == null ? void 0 : c.NODE_ENV) === "test" && typeof ((g = Element == null ? void 0 : Element.prototype) == null ? void 0 : g.getAnimations) > "u" && (Element.prototype.getAnimations = function() {
  return console.warn(["Headless UI has polyfilled `Element.prototype.getAnimations` for your tests.", "Please install a proper polyfill e.g. `jsdom-testing-mocks`, to silence these warnings.", "", "Example usage:", "```js", "import { mockAnimationsApi } from 'jsdom-testing-mocks'", "mockAnimationsApi()", "```"].join(`
`)), [];
});
var b = ((e) => (e[e.None = 0] = "None", e[e.Closed = 1] = "Closed", e[e.Enter = 2] = "Enter", e[e.Leave = 4] = "Leave", e))(b || {});
function L(e) {
  let n = {};
  for (let t in e) e[t] === !0 && (n[`data-${t}`] = "");
  return n;
}
function w(e, n, t, o) {
  let [s, r] = y(t), { hasFlag: l, addFlag: i, removeFlag: a } = E(e && s ? 3 : 0), p = m(!1), u = m(!1), h = A();
  return F(() => {
    var d;
    if (e) {
      if (t && r(!0), !n) {
        t && i(3);
        return;
      }
      return (d = o == null ? void 0 : o.start) == null || d.call(o, t), j(n, { inFlight: p, prepare() {
        u.current ? u.current = !1 : u.current = p.current, p.current = !0, !u.current && (t ? (i(3), a(4)) : (i(4), a(2)));
      }, run() {
        u.current ? t ? (a(3), i(4)) : (a(4), i(3)) : t ? a(1) : i(1);
      }, done() {
        var f;
        u.current && typeof n.getAnimations == "function" && n.getAnimations().length > 0 || (p.current = !1, a(7), t || r(!1), (f = o == null ? void 0 : o.end) == null || f.call(o, t));
      } });
    }
  }, [e, t, n, h]), e ? [s, { closed: l(1), enter: l(2), leave: l(4), transition: l(2) || l(4) }] : [t, { closed: void 0, enter: void 0, leave: void 0, transition: void 0 }];
}
function j(e, { prepare: n, run: t, done: o, inFlight: s }) {
  let r = v();
  return x(e, { prepare: n, inFlight: s }), r.nextFrame(() => {
    t(), r.requestAnimationFrame(() => {
      r.add(k(e, o));
    });
  }), r.dispose;
}
function k(e, n) {
  var t, o;
  let s = v();
  if (!e) return s.dispose;
  let r = !1;
  s.add(() => {
    r = !0;
  });
  let l = (o = (t = e.getAnimations) == null ? void 0 : t.call(e).filter((i) => i instanceof CSSTransition)) != null ? o : [];
  return l.length === 0 ? (n(), s.dispose) : (Promise.allSettled(l.map((i) => i.finished)).then(() => {
    r || n();
  }), s.dispose);
}
function x(e, { inFlight: n, prepare: t }) {
  if (n != null && n.current) {
    t();
    return;
  }
  let o = e.style.transition;
  e.style.transition = "none", t(), e.offsetHeight, e.style.transition = o;
}
export {
  L as transitionDataAttributes,
  w as useTransition
};
