import { useEffect as Y } from "react";
import { a as _, f as Z, c as $, m as tt, b as et, S as nt, H as at, v as x, s as st, d as it, e as rt, g as ot, h as lt, j as ut, r as ct, k as q, l as ft, n as mt, p as ht, o as pt, q as z, t as dt, u as I, w as gt } from "../SVGVisualElement-aqO4D4Z5.js";
import { u as yt } from "../use-unmount-effect-Wy5iLAex.js";
function C(t, e, a) {
  var n;
  if (typeof t == "string") {
    let s = document;
    e && (_(!!e.current, "Scope provided, but no element detected."), s = e.current), a ? ((n = a[t]) !== null && n !== void 0 || (a[t] = s.querySelectorAll(t)), t = a[t]) : t = s.querySelectorAll(t);
  } else
    t instanceof Element && (t = [t]);
  return Array.from(t || []);
}
function At(t, e) {
  let a;
  const n = () => {
    const { currentTime: s } = e, r = (s === null ? 0 : s.value) / 100;
    a !== r && t(r), a = r;
  };
  return Z.update(n, !0), () => $(n);
}
const vt = tt(() => window.ScrollTimeline !== void 0);
class R {
  constructor(e) {
    this.animations = e.filter(Boolean);
  }
  then(e, a) {
    return Promise.all(this.animations).then(e).catch(a);
  }
  /**
   * TODO: Filter out cancelled or stopped animations before returning
   */
  getAll(e) {
    return this.animations[0][e];
  }
  setAll(e, a) {
    for (let n = 0; n < this.animations.length; n++)
      this.animations[n][e] = a;
  }
  attachTimeline(e) {
    const a = this.animations.map((n) => {
      if (vt() && n.attachTimeline)
        n.attachTimeline(e);
      else
        return n.pause(), At((s) => {
          n.time = n.duration * s;
        }, e);
    });
    return () => {
      a.forEach((n, s) => {
        n && n(), this.animations[s].stop();
      });
    };
  }
  get time() {
    return this.getAll("time");
  }
  set time(e) {
    this.setAll("time", e);
  }
  get speed() {
    return this.getAll("speed");
  }
  set speed(e) {
    this.setAll("speed", e);
  }
  get duration() {
    let e = 0;
    for (let a = 0; a < this.animations.length; a++)
      e = Math.max(e, this.animations[a].duration);
    return e;
  }
  runAll(e) {
    this.animations.forEach((a) => a[e]());
  }
  play() {
    this.runAll("play");
  }
  pause() {
    this.runAll("pause");
  }
  stop() {
    this.runAll("stop");
  }
  cancel() {
    this.runAll("cancel");
  }
  complete() {
    this.runAll("complete");
  }
}
function St(t) {
  return typeof t == "object" && !Array.isArray(t);
}
function Et(t) {
  const e = {
    presenceContext: null,
    props: {},
    visualState: {
      renderState: {
        transform: {},
        transformOrigin: {},
        style: {},
        vars: {},
        attrs: {}
      },
      latestValues: {}
    }
  }, a = et(t) ? new nt(e, {
    enableHardwareAcceleration: !1
  }) : new at(e, {
    enableHardwareAcceleration: !0
  });
  a.mount(t), x.set(t, a);
}
function wt(t, e = 100) {
  const a = st({ keyframes: [0, e], ...t }), n = Math.min(it(a), rt);
  return {
    type: "keyframes",
    ease: (s) => a.next(n * s).value / e,
    duration: ot(n)
  };
}
function j(t, e, a, n) {
  var s;
  return typeof e == "number" ? e : e.startsWith("-") || e.startsWith("+") ? Math.max(0, t + parseFloat(e)) : e === "<" ? a : (s = n.get(e)) !== null && s !== void 0 ? s : t;
}
const Tt = (t, e, a) => {
  const n = e - t;
  return ((a - t) % n + n) % n + t;
};
function Mt(t, e) {
  return lt(t) ? t[Tt(0, t.length, e)] : t;
}
function bt(t, e, a) {
  for (let n = 0; n < t.length; n++) {
    const s = t[n];
    s.at > e && s.at < a && (ct(t, s), n--);
  }
}
function Dt(t, e, a, n, s, i) {
  bt(t, s, i);
  for (let r = 0; r < e.length; r++)
    t.push({
      value: e[r],
      at: ut(s, i, n[r]),
      easing: Mt(a, r)
    });
}
function Vt(t, e) {
  return t.at === e.at ? t.value === null ? 1 : e.value === null ? -1 : 0 : t.at - e.at;
}
const xt = "easeInOut";
function Ft(t, { defaultTransition: e = {}, ...a } = {}, n) {
  const s = e.duration || 0.3, i = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map(), l = {}, o = /* @__PURE__ */ new Map();
  let T = 0, c = 0, D = 0;
  for (let S = 0; S < t.length; S++) {
    const h = t[S];
    if (typeof h == "string") {
      o.set(h, c);
      continue;
    } else if (!Array.isArray(h)) {
      o.set(h.name, j(c, h.at, T, o));
      continue;
    }
    let [v, p, f = {}] = h;
    f.at !== void 0 && (c = j(c, f.at, T, o));
    let m = 0;
    const E = (d, g, A, M = 0, b = 0) => {
      const u = Gt(d), { delay: V = 0, times: w = ft(u), type: J = "keyframes", ...Q } = g;
      let { ease: F = e.ease || "easeOut", duration: y } = g;
      const G = typeof V == "function" ? V(M, b) : V, K = u.length;
      if (K <= 2 && J === "spring") {
        let H = 100;
        if (K === 2 && kt(u)) {
          const X = u[1] - u[0];
          H = Math.abs(X);
        }
        const L = { ...Q };
        y !== void 0 && (L.duration = pt(y));
        const N = wt(L, H);
        F = N.ease, y = N.duration;
      }
      y ?? (y = s);
      const P = c + G, k = P + y;
      w.length === 1 && w[0] === 0 && (w[1] = 1);
      const B = w.length - u.length;
      B > 0 && mt(w, B), u.length === 1 && u.unshift(null), Dt(A, u, F, w, P, k), m = Math.max(G + y, m), D = Math.max(k, D);
    };
    if (q(v)) {
      const d = O(v, r);
      E(p, f, W("default", d));
    } else {
      const d = C(v, n, l), g = d.length;
      for (let A = 0; A < g; A++) {
        p = p, f = f;
        const M = d[A], b = O(M, r);
        for (const u in p)
          E(p[u], Kt(f, u), W(u, b), A, g);
      }
      T = c, c += m;
    }
  }
  return r.forEach((S, h) => {
    for (const v in S) {
      const p = S[v];
      p.sort(Vt);
      const f = [], m = [], E = [];
      for (let g = 0; g < p.length; g++) {
        const { at: A, value: M, easing: b } = p[g];
        f.push(M), m.push(ht(0, D, A)), E.push(b || "easeOut");
      }
      m[0] !== 0 && (m.unshift(0), f.unshift(f[0]), E.unshift(xt)), m[m.length - 1] !== 1 && (m.push(1), f.push(null)), i.has(h) || i.set(h, {
        keyframes: {},
        transition: {}
      });
      const d = i.get(h);
      d.keyframes[v] = f, d.transition[v] = {
        ...e,
        duration: D,
        ease: E,
        times: m,
        ...a
      };
    }
  }), i;
}
function O(t, e) {
  return !e.has(t) && e.set(t, {}), e.get(t);
}
function W(t, e) {
  return e[t] || (e[t] = []), e[t];
}
function Gt(t) {
  return Array.isArray(t) ? t : [t];
}
function Kt(t, e) {
  return t[e] ? { ...t, ...t[e] } : { ...t };
}
const Pt = (t) => typeof t == "number", kt = (t) => t.every(Pt);
function U(t, e, a, n) {
  const s = C(t, n), i = s.length;
  _(!!i, "No valid element provided.");
  const r = [];
  for (let l = 0; l < i; l++) {
    const o = s[l];
    x.has(o) || Et(o);
    const T = x.get(o), c = { ...a };
    typeof c.delay == "function" && (c.delay = c.delay(l, i)), r.push(...dt(T, { ...e, transition: c }, {}));
  }
  return new R(r);
}
const Bt = (t) => Array.isArray(t) && Array.isArray(t[0]);
function Ht(t, e, a) {
  const n = [];
  return Ft(t, e, a).forEach(({ keyframes: i, transition: r }, l) => {
    let o;
    q(l) ? o = z(l, i.default, r.default) : o = U(l, i, r), n.push(o);
  }), new R(n);
}
const Lt = (t) => {
  function e(a, n, s) {
    let i;
    return Bt(a) ? i = Ht(a, n, t) : St(n) ? i = U(a, n, s, t) : i = z(a, n, s), t && t.animations.push(i), i;
  }
  return e;
};
function Nt() {
  const t = I(() => ({
    current: null,
    animations: []
  })), e = I(() => Lt(t));
  return yt(() => {
    t.animations.forEach((a) => a.stop());
  }), [t, e];
}
const Wt = ({ enterAnimation: t, exitAnimation: e, exitOptions: a, enterOptions: n }) => {
  const [s, i] = gt(), [r, l] = Nt();
  return Y(() => {
    s ? (async () => await l(r.current, t, n))() : (async () => (await l(r.current, e, a), i()))();
  }, [s]), {
    scope: r
  };
};
export {
  Wt as useAnimatePresence
};
