import { useIsomorphicLayoutEffect as ue, useOnce as Xe, each as S, is as o, useForceUpdate as Ze, usePrev as we, toArray as j, createInterpolator as It, isEqual as K, getFluidValue as Q, Globals as W, frameLoop as Oe, hasFluidValue as Y, addFluidObserver as ye, removeFluidObserver as et, deprecateDirectCall as wt, eachProp as se, flush as pe, FluidValue as At, deprecateInterpolate as Rt, callFluidObservers as _e, flushCalls as ce, isAnimatedString as tt, getFluidObservers as Ne, easings as Ct, noop as Qe, createStringInterpolator as Tt } from "./index-react-spring_shared.modern.js";
import * as fe from "react";
import { useMemo as le, useRef as Z, useContext as it } from "react";
import { getAnimatedType as Ae, setAnimated as st, getAnimated as $, getPayload as je, AnimatedValue as xt, AnimatedString as ze } from "./index-react-spring_animated.modern.js";
import { raf as N } from "./index-react-spring_rafz.modern.js";
function O(e, ...t) {
  return o.fun(e) ? e(...t) : e;
}
var he = (e, t) => e === !0 || !!(t && e && (o.fun(e) ? e(t) : j(e).includes(t))), nt = (e, t) => o.obj(e) ? t && e[t] : e, at = (e, t) => e.default === !0 ? e[t] : e.default ? e.default[t] : void 0, Vt = (e) => e, ve = (e, t = Vt) => {
  let i = Ft;
  e.default && e.default !== !0 && (e = e.default, i = Object.keys(e));
  const s = {};
  for (const n of i) {
    const a = t(e[n], n);
    o.und(a) || (s[n] = a);
  }
  return s;
}, Ft = [
  "config",
  "onProps",
  "onStart",
  "onChange",
  "onPause",
  "onResume",
  "onRest"
], Mt = {
  config: 1,
  from: 1,
  to: 1,
  ref: 1,
  loop: 1,
  reset: 1,
  pause: 1,
  cancel: 1,
  reverse: 1,
  immediate: 1,
  default: 1,
  delay: 1,
  onProps: 1,
  onStart: 1,
  onChange: 1,
  onPause: 1,
  onResume: 1,
  onRest: 1,
  onResolve: 1,
  // Transition props
  items: 1,
  trail: 1,
  sort: 1,
  expires: 1,
  initial: 1,
  enter: 1,
  update: 1,
  leave: 1,
  children: 1,
  onDestroyed: 1,
  // Internal props
  keys: 1,
  callId: 1,
  parentId: 1
};
function Et(e) {
  const t = {};
  let i = 0;
  if (se(e, (s, n) => {
    Mt[n] || (t[n] = s, i++);
  }), i)
    return t;
}
function qe(e) {
  const t = Et(e);
  if (t) {
    const i = { to: t };
    return se(e, (s, n) => n in t || (i[n] = s)), i;
  }
  return { ...e };
}
function de(e) {
  return e = Q(e), o.arr(e) ? e.map(de) : tt(e) ? W.createStringInterpolator({
    range: [0, 1],
    output: [e, e]
  })(1) : e;
}
function ot(e) {
  for (const t in e) return !0;
  return !1;
}
function Re(e) {
  return o.fun(e) || o.arr(e) && o.obj(e[0]);
}
function Ce(e, t) {
  var i;
  (i = e.ref) == null || i.delete(e), t == null || t.delete(e);
}
function rt(e, t) {
  var i;
  t && e.ref !== t && ((i = e.ref) == null || i.delete(e), t.add(e), e.ref = t);
}
var Ot = {
  default: { tension: 170, friction: 26 },
  wobbly: { tension: 180, friction: 12 }
}, Te = {
  ...Ot.default,
  mass: 1,
  damping: 1,
  easing: Ct.linear,
  clamp: !1
}, jt = class {
  constructor() {
    this.velocity = 0, Object.assign(this, Te);
  }
};
function qt(e, t, i) {
  i && (i = { ...i }, Le(i, t), t = { ...i, ...t }), Le(e, t), Object.assign(e, t);
  for (const c in Te)
    e[c] == null && (e[c] = Te[c]);
  let { frequency: s, damping: n } = e;
  const { mass: a } = e;
  return o.und(s) || (s < 0.01 && (s = 0.01), n < 0 && (n = 0), e.tension = Math.pow(2 * Math.PI / s, 2) * a, e.friction = 4 * Math.PI * n * a / s), e;
}
function Le(e, t) {
  if (!o.und(t.decay))
    e.duration = void 0;
  else {
    const i = !o.und(t.tension) || !o.und(t.friction);
    (i || !o.und(t.frequency) || !o.und(t.damping) || !o.und(t.mass)) && (e.duration = void 0, e.decay = void 0), i && (e.frequency = void 0);
  }
}
var Be = [], Ut = class {
  constructor() {
    this.changed = !1, this.values = Be, this.toValues = null, this.fromValues = Be, this.config = new jt(), this.immediate = !1;
  }
};
function ut(e, { key: t, props: i, defaultProps: s, state: n, actions: a }) {
  return new Promise((c, u) => {
    let g, f, b = he(i.cancel ?? (s == null ? void 0 : s.cancel), t);
    if (b)
      y();
    else {
      o.und(i.pause) || (n.paused = he(i.pause, t));
      let d = s == null ? void 0 : s.pause;
      d !== !0 && (d = n.paused || he(d, t)), g = O(i.delay || 0, t), d ? (n.resumeQueue.add(_), a.pause()) : (a.resume(), _());
    }
    function h() {
      n.resumeQueue.add(_), n.timeouts.delete(f), f.cancel(), g = f.time - N.now();
    }
    function _() {
      g > 0 && !W.skipAnimation ? (n.delayed = !0, f = N.setTimeout(y, g), n.pauseQueue.add(h), n.timeouts.add(f)) : y();
    }
    function y() {
      n.delayed && (n.delayed = !1), n.pauseQueue.delete(h), n.timeouts.delete(f), e <= (n.cancelId || 0) && (b = !0);
      try {
        a.start({ ...i, callId: e, cancel: b }, c);
      } catch (d) {
        u(d);
      }
    }
  });
}
var Ue = (e, t) => t.length == 1 ? t[0] : t.some((i) => i.cancelled) ? ie(e.get()) : t.every((i) => i.noop) ? ct(e.get()) : z(
  e.get(),
  t.every((i) => i.finished)
), ct = (e) => ({
  value: e,
  noop: !0,
  finished: !0,
  cancelled: !1
}), z = (e, t, i = !1) => ({
  value: e,
  finished: t,
  cancelled: i
}), ie = (e) => ({
  value: e,
  cancelled: !0,
  finished: !1
});
function ft(e, t, i, s) {
  const { callId: n, parentId: a, onRest: c } = t, { asyncTo: u, promise: g } = i;
  return !a && e === u && !t.reset ? g : i.promise = (async () => {
    i.asyncId = n, i.asyncTo = e;
    const f = ve(
      t,
      (r, l) => (
        // The `onRest` prop is only called when the `runAsync` promise is resolved.
        l === "onRest" ? void 0 : r
      )
    );
    let b, h;
    const _ = new Promise(
      (r, l) => (b = r, h = l)
    ), y = (r) => {
      const l = (
        // The `cancel` prop or `stop` method was used.
        n <= (i.cancelId || 0) && ie(s) || // The async `to` prop was replaced.
        n !== i.asyncId && z(s, !1)
      );
      if (l)
        throw r.result = l, h(r), r;
    };
    let d = 0;
    const A = 1024, I = (r, l) => {
      const p = new Ge(), R = new Ke();
      return (async () => {
        y(p);
        const w = o.obj(r) ? { ...r } : { ...l, to: r };
        if (w.parentId = n, se(f, (E, T) => {
          o.und(w[T]) && (w[T] = E);
        }), W.skipAnimation) {
          if (++d > A)
            throw Pe(i), R.result = z(s, !1), h(R), R;
          return w.immediate = !0, await s.start(w);
        }
        const x = await s.start(w);
        return y(p), i.paused && await new Promise((E) => {
          i.resumeQueue.add(E);
        }), x;
      })();
    };
    let v;
    try {
      let r;
      o.arr(e) ? r = (async (l) => {
        for (const p of l)
          await I(p);
      })(e) : r = Promise.resolve(e(I, s.stop.bind(s))), await Promise.all([r.then(b), _]), v = z(s.get(), !0, !1);
    } catch (r) {
      if (r instanceof Ge)
        v = r.result;
      else if (r instanceof Ke)
        v = r.result;
      else
        throw r;
    } finally {
      n == i.asyncId && (i.asyncId = a, i.asyncTo = a ? u : void 0, i.promise = a ? g : void 0);
    }
    return o.fun(c) && N.batchedUpdates(() => {
      c(v, s, s.item);
    }), v;
  })();
}
function Pe(e, t) {
  pe(e.timeouts, (i) => i.cancel()), e.pauseQueue.clear(), e.resumeQueue.clear(), e.asyncId = e.asyncTo = e.promise = void 0, t && (e.cancelId = t);
}
var Ge = class extends Error {
  constructor() {
    super(
      "An async animation has been interrupted. You see this error because you forgot to use `await` or `.catch(...)` on its returned promise."
    );
  }
}, Ke = class extends Error {
  constructor() {
    super("SkipAnimationSignal");
  }
}, xe = (e) => e instanceof ke, kt = 1, ke = class extends At {
  constructor() {
    super(...arguments), this.id = kt++, this._priority = 0;
  }
  get priority() {
    return this._priority;
  }
  set priority(e) {
    this._priority != e && (this._priority = e, this._onPriorityChange(e));
  }
  /** Get the current value */
  get() {
    const e = $(this);
    return e && e.getValue();
  }
  /** Create a spring that maps our value to another value */
  to(...e) {
    return W.to(this, e);
  }
  /** @deprecated Use the `to` method instead. */
  interpolate(...e) {
    return Rt(), W.to(this, e);
  }
  toJSON() {
    return this.get();
  }
  observerAdded(e) {
    e == 1 && this._attach();
  }
  observerRemoved(e) {
    e == 0 && this._detach();
  }
  /** Called when the first child is added. */
  _attach() {
  }
  /** Called when the last child is removed. */
  _detach() {
  }
  /** Tell our children about our new value */
  _onChange(e, t = !1) {
    _e(this, {
      type: "change",
      parent: this,
      value: e,
      idle: t
    });
  }
  /** Tell our children about our new priority */
  _onPriorityChange(e) {
    this.idle || Oe.sort(this), _e(this, {
      type: "priority",
      parent: this,
      priority: e
    });
  }
}, ee = Symbol.for("SpringPhase"), lt = 1, Ve = 2, Fe = 4, Se = (e) => (e[ee] & lt) > 0, J = (e) => (e[ee] & Ve) > 0, ae = (e) => (e[ee] & Fe) > 0, $e = (e, t) => t ? e[ee] |= Ve | lt : e[ee] &= ~Ve, He = (e, t) => t ? e[ee] |= Fe : e[ee] &= ~Fe, Dt = class extends ke {
  constructor(e, t) {
    if (super(), this.animation = new Ut(), this.defaultProps = {}, this._state = {
      paused: !1,
      delayed: !1,
      pauseQueue: /* @__PURE__ */ new Set(),
      resumeQueue: /* @__PURE__ */ new Set(),
      timeouts: /* @__PURE__ */ new Set()
    }, this._pendingCalls = /* @__PURE__ */ new Set(), this._lastCallId = 0, this._lastToId = 0, this._memoizedDuration = 0, !o.und(e) || !o.und(t)) {
      const i = o.obj(e) ? { ...e } : { ...t, from: e };
      o.und(i.default) && (i.default = !0), this.start(i);
    }
  }
  /** Equals true when not advancing on each frame. */
  get idle() {
    return !(J(this) || this._state.asyncTo) || ae(this);
  }
  get goal() {
    return Q(this.animation.to);
  }
  get velocity() {
    const e = $(this);
    return e instanceof xt ? e.lastVelocity || 0 : e.getPayload().map((t) => t.lastVelocity || 0);
  }
  /**
   * When true, this value has been animated at least once.
   */
  get hasAnimated() {
    return Se(this);
  }
  /**
   * When true, this value has an unfinished animation,
   * which is either active or paused.
   */
  get isAnimating() {
    return J(this);
  }
  /**
   * When true, all current and future animations are paused.
   */
  get isPaused() {
    return ae(this);
  }
  /**
   *
   *
   */
  get isDelayed() {
    return this._state.delayed;
  }
  /** Advance the current animation by a number of milliseconds */
  advance(e) {
    let t = !0, i = !1;
    const s = this.animation;
    let { toValues: n } = s;
    const { config: a } = s, c = je(s.to);
    !c && Y(s.to) && (n = j(Q(s.to))), s.values.forEach((f, b) => {
      if (f.done) return;
      const h = (
        // Animated strings always go from 0 to 1.
        f.constructor == ze ? 1 : c ? c[b].lastPosition : n[b]
      );
      let _ = s.immediate, y = h;
      if (!_) {
        if (y = f.lastPosition, a.tension <= 0) {
          f.done = !0;
          return;
        }
        let d = f.elapsedTime += e;
        const A = s.fromValues[b], I = f.v0 != null ? f.v0 : f.v0 = o.arr(a.velocity) ? a.velocity[b] : a.velocity;
        let v;
        const r = a.precision || (A == h ? 5e-3 : Math.min(1, Math.abs(h - A) * 1e-3));
        if (o.und(a.duration))
          if (a.decay) {
            const l = a.decay === !0 ? 0.998 : a.decay, p = Math.exp(-(1 - l) * d);
            y = A + I / (1 - l) * (1 - p), _ = Math.abs(f.lastPosition - y) <= r, v = I * p;
          } else {
            v = f.lastVelocity == null ? I : f.lastVelocity;
            const l = a.restVelocity || r / 10, p = a.clamp ? 0 : a.bounce, R = !o.und(p), w = A == h ? f.v0 > 0 : A < h;
            let x, E = !1;
            const T = 1, U = Math.ceil(e / T);
            for (let q = 0; q < U && (x = Math.abs(v) > l, !(!x && (_ = Math.abs(h - y) <= r, _))); ++q) {
              R && (E = y == h || y > h == w, E && (v = -v * p, y = h));
              const L = -a.tension * 1e-6 * (y - h), B = -a.friction * 1e-3 * v, m = (L + B) / a.mass;
              v = v + m * T, y = y + v * T;
            }
          }
        else {
          let l = 1;
          a.duration > 0 && (this._memoizedDuration !== a.duration && (this._memoizedDuration = a.duration, f.durationProgress > 0 && (f.elapsedTime = a.duration * f.durationProgress, d = f.elapsedTime += e)), l = (a.progress || 0) + d / this._memoizedDuration, l = l > 1 ? 1 : l < 0 ? 0 : l, f.durationProgress = l), y = A + a.easing(l) * (h - A), v = (y - f.lastPosition) / e, _ = l == 1;
        }
        f.lastVelocity = v, Number.isNaN(y) && (_ = !0);
      }
      c && !c[b].done && (_ = !1), _ ? f.done = !0 : t = !1, f.setValue(y, a.round) && (i = !0);
    });
    const u = $(this), g = u.getValue();
    if (t) {
      const f = Q(s.to);
      (g !== f || i) && !a.decay ? (u.setValue(f), this._onChange(f)) : i && a.decay && this._onChange(g), this._stop();
    } else i && this._onChange(g);
  }
  /** Set the current value, while stopping the current animation */
  set(e) {
    return N.batchedUpdates(() => {
      this._stop(), this._focus(e), this._set(e);
    }), this;
  }
  /**
   * Freeze the active animation in time, as well as any updates merged
   * before `resume` is called.
   */
  pause() {
    this._update({ pause: !0 });
  }
  /** Resume the animation if paused. */
  resume() {
    this._update({ pause: !1 });
  }
  /** Skip to the end of the current animation. */
  finish() {
    if (J(this)) {
      const { to: e, config: t } = this.animation;
      N.batchedUpdates(() => {
        this._onStart(), t.decay || this._set(e, !1), this._stop();
      });
    }
    return this;
  }
  /** Push props into the pending queue. */
  update(e) {
    return (this.queue || (this.queue = [])).push(e), this;
  }
  start(e, t) {
    let i;
    return o.und(e) ? (i = this.queue || [], this.queue = []) : i = [o.obj(e) ? e : { ...t, to: e }], Promise.all(
      i.map((s) => this._update(s))
    ).then((s) => Ue(this, s));
  }
  /**
   * Stop the current animation, and cancel any delayed updates.
   *
   * Pass `true` to call `onRest` with `cancelled: true`.
   */
  stop(e) {
    const { to: t } = this.animation;
    return o.und(t) || this._focus(this.get()), Pe(this._state, e && this._lastCallId), N.batchedUpdates(() => this._stop(t, e)), this;
  }
  /** Restart the animation. */
  reset() {
    this._update({ reset: !0 });
  }
  /** @internal */
  eventObserved(e) {
    e.type == "change" ? this._start() : e.type == "priority" && (this.priority = e.priority + 1);
  }
  /**
   * Parse the `to` and `from` range from the given `props` object.
   *
   * This also ensures the initial value is available to animated components
   * during the render phase.
   */
  _prepareNode(e) {
    const t = this.key || "";
    let { to: i, from: s } = e;
    i = o.obj(i) ? i[t] : i, (i == null || Re(i)) && (i = void 0), s = o.obj(s) ? s[t] : s, s == null && (s = void 0);
    const n = { to: i, from: s };
    return Se(this) || (e.reverse && ([i, s] = [s, i]), s = Q(s), o.und(s) ? $(this) || this._set(i) : this._set(s)), n;
  }
  /** Every update is processed by this method before merging. */
  _update({ ...e }, t) {
    const { key: i, defaultProps: s } = this;
    e.default && Object.assign(
      s,
      ve(
        e,
        (c, u) => /^on/.test(u) ? nt(c, i) : c
      )
    ), Ye(this, e, "onProps"), re(this, "onProps", e, this);
    const n = this._prepareNode(e);
    if (Object.isFrozen(this))
      throw Error(
        "Cannot animate a `SpringValue` object that is frozen. Did you forget to pass your component to `animated(...)` before animating its props?"
      );
    const a = this._state;
    return ut(++this._lastCallId, {
      key: i,
      props: e,
      defaultProps: s,
      state: a,
      actions: {
        pause: () => {
          ae(this) || (He(this, !0), ce(a.pauseQueue), re(
            this,
            "onPause",
            z(this, oe(this, this.animation.to)),
            this
          ));
        },
        resume: () => {
          ae(this) && (He(this, !1), J(this) && this._resume(), ce(a.resumeQueue), re(
            this,
            "onResume",
            z(this, oe(this, this.animation.to)),
            this
          ));
        },
        start: this._merge.bind(this, n)
      }
    }).then((c) => {
      if (e.loop && c.finished && !(t && c.noop)) {
        const u = ht(e);
        if (u)
          return this._update(u, !0);
      }
      return c;
    });
  }
  /** Merge props into the current animation */
  _merge(e, t, i) {
    if (t.cancel)
      return this.stop(!0), i(ie(this));
    const s = !o.und(e.to), n = !o.und(e.from);
    if (s || n)
      if (t.callId > this._lastToId)
        this._lastToId = t.callId;
      else
        return i(ie(this));
    const { key: a, defaultProps: c, animation: u } = this, { to: g, from: f } = u;
    let { to: b = g, from: h = f } = e;
    n && !s && (!t.default || o.und(b)) && (b = h), t.reverse && ([b, h] = [h, b]);
    const _ = !K(h, f);
    _ && (u.from = h), h = Q(h);
    const y = !K(b, g);
    y && this._focus(b);
    const d = Re(t.to), { config: A } = u, { decay: I, velocity: v } = A;
    (s || n) && (A.velocity = 0), t.config && !d && qt(
      A,
      O(t.config, a),
      // Avoid calling the same "config" prop twice.
      t.config !== c.config ? O(c.config, a) : void 0
    );
    let r = $(this);
    if (!r || o.und(b))
      return i(z(this, !0));
    const l = (
      // When `reset` is undefined, the `from` prop implies `reset: true`,
      // except for declarative updates. When `reset` is defined, there
      // must exist a value to animate from.
      o.und(t.reset) ? n && !t.default : !o.und(h) && he(t.reset, a)
    ), p = l ? h : this.get(), R = de(b), w = o.num(R) || o.arr(R) || tt(R), x = !d && (!w || he(c.immediate || t.immediate, a));
    if (y) {
      const q = Ae(b);
      if (q !== r.constructor)
        if (x)
          r = this._set(R);
        else
          throw Error(
            `Cannot animate between ${r.constructor.name} and ${q.name}, as the "to" prop suggests`
          );
    }
    const E = r.constructor;
    let T = Y(b), U = !1;
    if (!T) {
      const q = l || !Se(this) && _;
      (y || q) && (U = K(de(p), R), T = !U), (!K(u.immediate, x) && !x || !K(A.decay, I) || !K(A.velocity, v)) && (T = !0);
    }
    if (U && J(this) && (u.changed && !l ? T = !0 : T || this._stop(g)), !d && ((T || Y(g)) && (u.values = r.getPayload(), u.toValues = Y(b) ? null : E == ze ? [1] : j(R)), u.immediate != x && (u.immediate = x, !x && !l && this._set(g)), T)) {
      const { onRest: q } = u;
      S(Qt, (B) => Ye(this, t, B));
      const L = z(this, oe(this, g));
      ce(this._pendingCalls, L), this._pendingCalls.add(i), u.changed && N.batchedUpdates(() => {
        var B;
        u.changed = !l, q == null || q(L, this), l ? O(c.onRest, L) : (B = u.onStart) == null || B.call(u, L, this);
      });
    }
    l && this._set(p), d ? i(ft(t.to, t, this._state, this)) : T ? this._start() : J(this) && !y ? this._pendingCalls.add(i) : i(ct(p));
  }
  /** Update the `animation.to` value, which might be a `FluidValue` */
  _focus(e) {
    const t = this.animation;
    e !== t.to && (Ne(this) && this._detach(), t.to = e, Ne(this) && this._attach());
  }
  _attach() {
    let e = 0;
    const { to: t } = this.animation;
    Y(t) && (ye(t, this), xe(t) && (e = t.priority + 1)), this.priority = e;
  }
  _detach() {
    const { to: e } = this.animation;
    Y(e) && et(e, this);
  }
  /**
   * Update the current value from outside the frameloop,
   * and return the `Animated` node.
   */
  _set(e, t = !0) {
    const i = Q(e);
    if (!o.und(i)) {
      const s = $(this);
      if (!s || !K(i, s.getValue())) {
        const n = Ae(i);
        !s || s.constructor != n ? st(this, n.create(i)) : s.setValue(i), s && N.batchedUpdates(() => {
          this._onChange(i, t);
        });
      }
    }
    return $(this);
  }
  _onStart() {
    const e = this.animation;
    e.changed || (e.changed = !0, re(
      this,
      "onStart",
      z(this, oe(this, e.to)),
      this
    ));
  }
  _onChange(e, t) {
    t || (this._onStart(), O(this.animation.onChange, e, this)), O(this.defaultProps.onChange, e, this), super._onChange(e, t);
  }
  // This method resets the animation state (even if already animating) to
  // ensure the latest from/to range is used, and it also ensures this spring
  // is added to the frameloop.
  _start() {
    const e = this.animation;
    $(this).reset(Q(e.to)), e.immediate || (e.fromValues = e.values.map((t) => t.lastPosition)), J(this) || ($e(this, !0), ae(this) || this._resume());
  }
  _resume() {
    W.skipAnimation ? this.finish() : Oe.start(this);
  }
  /**
   * Exit the frameloop and notify `onRest` listeners.
   *
   * Always wrap `_stop` calls with `batchedUpdates`.
   */
  _stop(e, t) {
    if (J(this)) {
      $e(this, !1);
      const i = this.animation;
      S(i.values, (n) => {
        n.done = !0;
      }), i.toValues && (i.onChange = i.onPause = i.onResume = void 0), _e(this, {
        type: "idle",
        parent: this
      });
      const s = t ? ie(this.get()) : z(this.get(), oe(this, e ?? i.to));
      ce(this._pendingCalls, s), i.changed && (i.changed = !1, re(this, "onRest", s, this));
    }
  }
};
function oe(e, t) {
  const i = de(t), s = de(e.get());
  return K(s, i);
}
function ht(e, t = e.loop, i = e.to) {
  const s = O(t);
  if (s) {
    const n = s !== !0 && qe(s), a = (n || e).reverse, c = !n || n.reset;
    return me({
      ...e,
      loop: t,
      // Avoid updating default props when looping.
      default: !1,
      // Never loop the `pause` prop.
      pause: void 0,
      // For the "reverse" prop to loop as expected, the "to" prop
      // must be undefined. The "reverse" prop is ignored when the
      // "to" prop is an array or function.
      to: !a || Re(i) ? i : void 0,
      // Ignore the "from" prop except on reset.
      from: c ? e.from : void 0,
      reset: c,
      // The "loop" prop can return a "useSpring" props object to
      // override any of the original props.
      ...n
    });
  }
}
function me(e) {
  const { to: t, from: i } = e = qe(e), s = /* @__PURE__ */ new Set();
  return o.obj(t) && Je(t, s), o.obj(i) && Je(i, s), e.keys = s.size ? Array.from(s) : null, e;
}
function Nt(e) {
  const t = me(e);
  return o.und(t.default) && (t.default = ve(t)), t;
}
function Je(e, t) {
  se(e, (i, s) => i != null && t.add(s));
}
var Qt = [
  "onStart",
  "onRest",
  "onChange",
  "onPause",
  "onResume"
];
function Ye(e, t, i) {
  e.animation[i] = t[i] !== at(t, i) ? nt(t[i], e.key) : void 0;
}
function re(e, t, ...i) {
  var s, n, a, c;
  (n = (s = e.animation)[t]) == null || n.call(s, ...i), (c = (a = e.defaultProps)[t]) == null || c.call(a, ...i);
}
var zt = ["onStart", "onChange", "onRest"], Lt = 1, dt = class {
  constructor(e, t) {
    this.id = Lt++, this.springs = {}, this.queue = [], this._lastAsyncId = 0, this._active = /* @__PURE__ */ new Set(), this._changed = /* @__PURE__ */ new Set(), this._started = !1, this._state = {
      paused: !1,
      pauseQueue: /* @__PURE__ */ new Set(),
      resumeQueue: /* @__PURE__ */ new Set(),
      timeouts: /* @__PURE__ */ new Set()
    }, this._events = {
      onStart: /* @__PURE__ */ new Map(),
      onChange: /* @__PURE__ */ new Map(),
      onRest: /* @__PURE__ */ new Map()
    }, this._onFrame = this._onFrame.bind(this), t && (this._flush = t), e && this.start({ default: !0, ...e });
  }
  /**
   * Equals `true` when no spring values are in the frameloop, and
   * no async animation is currently active.
   */
  get idle() {
    return !this._state.asyncTo && Object.values(this.springs).every((e) => e.idle && !e.isDelayed && !e.isPaused);
  }
  get item() {
    return this._item;
  }
  set item(e) {
    this._item = e;
  }
  /** Get the current values of our springs */
  get() {
    const e = {};
    return this.each((t, i) => e[i] = t.get()), e;
  }
  /** Set the current values without animating. */
  set(e) {
    for (const t in e) {
      const i = e[t];
      o.und(i) || this.springs[t].set(i);
    }
  }
  /** Push an update onto the queue of each value. */
  update(e) {
    return e && this.queue.push(me(e)), this;
  }
  /**
   * Start the queued animations for every spring, and resolve the returned
   * promise once all queued animations have finished or been cancelled.
   *
   * When you pass a queue (instead of nothing), that queue is used instead of
   * the queued animations added with the `update` method, which are left alone.
   */
  start(e) {
    let { queue: t } = this;
    return e ? t = j(e).map(me) : this.queue = [], this._flush ? this._flush(this, t) : (yt(this, t), Me(this, t));
  }
  /** @internal */
  stop(e, t) {
    if (e !== !!e && (t = e), t) {
      const i = this.springs;
      S(j(t), (s) => i[s].stop(!!e));
    } else
      Pe(this._state, this._lastAsyncId), this.each((i) => i.stop(!!e));
    return this;
  }
  /** Freeze the active animation in time */
  pause(e) {
    if (o.und(e))
      this.start({ pause: !0 });
    else {
      const t = this.springs;
      S(j(e), (i) => t[i].pause());
    }
    return this;
  }
  /** Resume the animation if paused. */
  resume(e) {
    if (o.und(e))
      this.start({ pause: !1 });
    else {
      const t = this.springs;
      S(j(e), (i) => t[i].resume());
    }
    return this;
  }
  /** Call a function once per spring value */
  each(e) {
    se(this.springs, e);
  }
  /**
   * Subscribe to loop iteration restarts on this controller. Returns an
   * unsubscribe function. Listeners fire synchronously inside `flushUpdate`
   * just before the next iteration is dispatched.
   * @internal
   */
  onLoopReset(e) {
    const t = this._onLoopReset ?? (this._onLoopReset = /* @__PURE__ */ new Set());
    return t.add(e), () => {
      t.delete(e);
    };
  }
  /** @internal Called at the end of every animation frame */
  _onFrame() {
    const { onStart: e, onChange: t, onRest: i } = this._events, s = this._active.size > 0, n = this._changed.size > 0;
    (s && !this._started || n && !this._started) && (this._started = !0, pe(e, ([u, g]) => {
      g.value = this.get(), u(g, this, this._item);
    }));
    const a = !s && this._started, c = n || a && i.size ? this.get() : null;
    n && t.size && pe(t, ([u, g]) => {
      g.value = c, u(g, this, this._item);
    }), a && (this._started = !1, pe(i, ([u, g]) => {
      g.value = c, u(g, this, this._item);
    }));
  }
  /** @internal */
  eventObserved(e) {
    if (e.type == "change")
      this._changed.add(e.parent), e.idle || this._active.add(e.parent);
    else if (e.type == "idle")
      this._active.delete(e.parent);
    else return;
    N.onFrame(this._onFrame);
  }
};
function Me(e, t) {
  return Promise.all(t.map((i) => mt(e, i))).then(
    (i) => Ue(e, i)
  );
}
async function mt(e, t, i) {
  var A;
  const { keys: s, to: n, from: a, loop: c, onRest: u, onResolve: g } = t, f = o.obj(t.default) && t.default;
  c && (t.loop = !1), n === !1 && (t.to = null), a === !1 && (t.from = null);
  const b = o.arr(n) || o.fun(n) ? n : void 0;
  b ? (t.to = void 0, t.onRest = void 0, f && (f.onRest = void 0)) : S(zt, (I) => {
    const v = t[I];
    if (o.fun(v)) {
      const r = e._events[I];
      t[I] = ({ finished: l, cancelled: p }) => {
        const R = r.get(v);
        R ? (l || (R.finished = !1), p && (R.cancelled = !0)) : r.set(v, {
          value: null,
          finished: l || !1,
          cancelled: p || !1
        });
      }, f && (f[I] = t[I]);
    }
  });
  const h = e._state;
  t.pause === !h.paused ? (h.paused = t.pause, ce(t.pause ? h.pauseQueue : h.resumeQueue)) : h.paused && (t.pause = !0);
  const _ = (s || Object.keys(e.springs)).map(
    (I) => e.springs[I].start(t)
  ), y = t.cancel === !0 || at(t, "cancel") === !0;
  (b || y && h.asyncId) && _.push(
    ut(++e._lastAsyncId, {
      props: t,
      state: h,
      actions: {
        pause: Qe,
        resume: Qe,
        start(I, v) {
          y ? (Pe(h, e._lastAsyncId), v(ie(e))) : (I.onRest = u, v(
            ft(
              b,
              I,
              h,
              e
            )
          ));
        }
      }
    })
  ), h.paused && await new Promise((I) => {
    h.resumeQueue.add(I);
  });
  const d = Ue(e, await Promise.all(_));
  if (c && d.finished && !(i && d.noop)) {
    const I = ht(t, c, n);
    if (I)
      return (A = e._onLoopReset) == null || A.forEach((v) => v()), yt(e, [I]), mt(e, I, !0);
  }
  return g && N.batchedUpdates(() => g(d, e, e.item)), d;
}
function Ee(e, t) {
  const i = { ...e.springs };
  return t && S(j(t), (s) => {
    o.und(s.keys) && (s = me(s)), o.obj(s.to) || (s = { ...s, to: void 0 }), _t(i, s, (n) => pt(n));
  }), gt(e, i), i;
}
function gt(e, t) {
  se(t, (i, s) => {
    e.springs[s] || (e.springs[s] = i, ye(i, e));
  });
}
function pt(e, t) {
  const i = new Dt();
  return i.key = e, t && ye(i, t), i;
}
function _t(e, t, i) {
  t.keys && S(t.keys, (s) => {
    (e[s] || (e[s] = i(s)))._prepareNode(t);
  });
}
function yt(e, t) {
  S(t, (i) => {
    _t(e.springs, i, (s) => pt(s, e));
  });
}
var vt = fe.createContext({
  pause: !1,
  immediate: !1
}), Pt = () => {
  const e = [], t = function(s) {
    wt();
    const n = [];
    return S(e, (a, c) => {
      if (o.und(s))
        n.push(a.start());
      else {
        const u = i(s, a, c);
        u && n.push(a.start(u));
      }
    }), n;
  };
  t.current = e, t.add = function(s) {
    e.includes(s) || e.push(s);
  }, t.delete = function(s) {
    const n = e.indexOf(s);
    ~n && e.splice(n, 1);
  }, t.pause = function() {
    return S(e, (s) => s.pause(...arguments)), this;
  }, t.resume = function() {
    return S(e, (s) => s.resume(...arguments)), this;
  }, t.set = function(s) {
    S(e, (n, a) => {
      const c = o.fun(s) ? s(a, n) : s;
      c && n.set(c);
    });
  }, t.start = function(s) {
    const n = [];
    return S(e, (a, c) => {
      if (o.und(s))
        n.push(a.start());
      else {
        const u = this._getProps(s, a, c);
        u && n.push(a.start(u));
      }
    }), n;
  }, t.stop = function() {
    return S(e, (s) => s.stop(...arguments)), this;
  }, t.update = function(s) {
    return S(e, (n, a) => n.update(this._getProps(s, n, a))), this;
  };
  const i = function(s, n, a) {
    return o.fun(s) ? s(a, n) : s;
  };
  return t._getProps = i, t;
};
function Bt(e, t, i) {
  const s = o.fun(t) && t;
  s && !i && (i = []);
  const n = le(
    () => s || arguments.length == 3 ? Pt() : void 0,
    []
  ), a = Z(0), c = Ze(), u = le(
    () => ({
      ctrls: [],
      queue: [],
      flush(r, l) {
        const p = Ee(r, l);
        return a.current > 0 && !u.queue.length && !Object.keys(p).some((w) => !r.springs[w]) ? Me(r, l) : new Promise((w) => {
          gt(r, p), u.queue.push(() => {
            w(Me(r, l));
          }), c();
        });
      }
    }),
    []
  ), g = Z([...u.ctrls]), f = Z([]), b = Z([]);
  b.current = [];
  const h = we(e) || 0;
  le(() => {
    S(g.current.slice(e, h), (r) => {
      Ce(r, n), r.stop(!0);
    }), g.current.length = e, _(h, e);
  }, [e]), le(() => {
    _(0, Math.min(h, e));
  }, i);
  function _(r, l) {
    for (let p = r; p < l; p++) {
      const R = g.current[p] || (g.current[p] = new dt(null, u.flush)), w = s ? s(p, R) : t[p];
      w && (f.current[p] = Nt(w));
    }
  }
  const y = g.current.map(
    (r, l) => Ee(r, f.current[l])
  ), d = it(vt), A = we(d), I = d !== A && ot(d);
  ue(() => {
    a.current++, u.ctrls = g.current;
    const { queue: r } = u;
    r.length && (u.queue = [], S(r, (p) => p()));
    const l = f.current.length > 0 ? f.current : b.current;
    S(g.current, (p, R) => {
      n == null || n.add(p), I && p.start({ default: d });
      const w = l[R];
      w && (rt(p, w.ref), p.ref ? p.queue.push({
        ...w,
        default: o.obj(w.default) ? { ...w.default } : w.default
      }) : p.start(w));
    }), f.current.length > 0 && (b.current = f.current), f.current = [];
  }), Xe(() => () => {
    S(u.ctrls, (r) => r.stop(!0));
  });
  const v = y.map((r) => ({ ...r }));
  return n ? [v, n] : v;
}
function Xt(e, t) {
  const i = o.fun(e), [[s], n] = Bt(
    1,
    i ? e : [e],
    i ? [] : t
  );
  return i || arguments.length == 2 ? [s, n] : s;
}
function Zt(e, t, i) {
  const s = o.fun(t) && t, {
    reset: n,
    sort: a,
    trail: c = 0,
    expires: u = !0,
    exitBeforeEnter: g = !1,
    onDestroyed: f,
    ref: b,
    config: h
  } = s ? s() : t, _ = le(
    () => s || arguments.length == 3 ? Pt() : void 0,
    []
  ), y = j(e), d = [], A = Z(null), I = n ? null : A.current;
  ue(() => {
    A.current = d;
  }), Xe(() => (S(d, (m) => {
    _ == null || _.add(m.ctrl), m.ctrl.ref = _;
  }), () => {
    S(A.current, (m) => {
      m.expired && clearTimeout(m.expirationId), Ce(m.ctrl, _), m.ctrl.stop(!0);
    });
  }));
  const v = Kt(y, s ? s() : t, I), r = n && A.current || [];
  ue(
    () => S(r, ({ ctrl: m, item: P, key: V }) => {
      Ce(m, _), O(f, P, V);
    })
  );
  const l = [];
  if (I && S(I, (m, P) => {
    m.expired ? (clearTimeout(m.expirationId), r.push(m)) : (P = l[P] = v.indexOf(m.key), ~P && (d[P] = m));
  }), S(y, (m, P) => {
    d[P] || (d[P] = {
      key: v[P],
      item: m,
      phase: "mount",
      ctrl: new dt()
    }, d[P].ctrl.item = m);
  }), l.length) {
    let m = -1;
    const { leave: P } = s ? s() : t;
    S(l, (V, F) => {
      const C = I[F];
      ~V ? (m = d.indexOf(C), d[m] = { ...C, item: y[V] }) : P && d.splice(++m, 0, C);
    });
  }
  o.fun(a) && d.sort((m, P) => a(m.item, P.item));
  let p = -c;
  const R = Ze(), w = ve(t), x = /* @__PURE__ */ new Map(), E = Z(/* @__PURE__ */ new Map()), T = Z(!1);
  S(d, (m, P) => {
    const V = m.key, F = m.phase, C = s ? s() : t;
    let M, k;
    const te = O(C.delay || 0, V);
    if (F == "mount")
      M = C.enter, k = "enter";
    else {
      const D = v.indexOf(V) < 0;
      if (F != "leave")
        if (D)
          M = C.leave, k = "leave";
        else if (M = C.update)
          k = "update";
        else return;
      else if (!D)
        M = C.enter, k = "enter";
      else return;
    }
    if (M = O(M, m.item, P), M = o.obj(M) ? qe(M) : { to: M }, !M.config) {
      const D = h || w.config;
      M.config = O(D, m.item, P, k);
    }
    p += c;
    const G = {
      ...w,
      // we need to add our props.delay value you here.
      delay: te + p,
      ref: b,
      immediate: C.immediate,
      // This prevents implied resets.
      reset: !1,
      // Merge any phase-specific props.
      ...M
    };
    if (k == "enter" && o.und(G.from)) {
      const D = s ? s() : t, ne = o.und(D.initial) || I ? D.from : D.initial;
      G.from = O(ne, m.item, P);
    }
    const { onResolve: St } = G;
    G.onResolve = (D) => {
      O(St, D);
      const ne = A.current, H = ne.find((ge) => ge.key === V);
      if (H && !(D.cancelled && H.phase != "update") && H.ctrl.idle) {
        const ge = ne.every((X) => X.ctrl.idle);
        if (H.phase == "leave") {
          E.current.delete(H);
          const X = O(u, H.item);
          if (X !== !1) {
            const be = X === !0 ? 0 : X;
            if (H.expired = !0, !ge && be > 0) {
              be <= 2147483647 && (H.expirationId = setTimeout(R, be));
              return;
            }
          }
        }
        ge && ne.some((X) => X.expired) && (g && (T.current = !0), R());
      }
    };
    const De = Ee(m.ctrl, G);
    k === "leave" && g ? E.current.set(m, { phase: k, springs: De, payload: G }) : x.set(m, { phase: k, springs: De, payload: G });
  });
  const U = it(vt), q = we(U), L = U !== q && ot(U);
  ue(() => {
    L && S(d, (m) => {
      m.ctrl.start({ default: U });
    });
  }, [U]), S(x, (m, P) => {
    if (E.current.size) {
      const V = d.findIndex((F) => F.key === P.key);
      d.splice(V, 1);
    }
  }), ue(
    () => {
      S(
        E.current.size ? E.current : x,
        ({ phase: m, payload: P }, V) => {
          const { ctrl: F } = V;
          V.phase = m, _ == null || _.add(F), L && m == "enter" && F.start({ default: U }), P && (rt(F, P.ref), (F.ref || _) && !T.current ? F.update(P) : (F.start(P), T.current && (T.current = !1)));
        }
      );
    },
    n ? void 0 : i
  );
  const B = (m) => /* @__PURE__ */ fe.createElement(fe.Fragment, null, d.map((P, V) => {
    const { springs: F } = x.get(P) || P.ctrl, C = m({ ...F }, P.item, P, V), M = o.str(P.key) || o.num(P.key) ? P.key : P.ctrl.id, k = fe.version < "19.0.0", te = (C == null ? void 0 : C.props) ?? {}, G = k ? C == null ? void 0 : C.ref : te == null ? void 0 : te.ref;
    return C && C.type ? /* @__PURE__ */ fe.createElement(C.type, { ...te, key: M, ref: G }) : C;
  }));
  return _ ? [B, _] : B;
}
var Gt = 1;
function Kt(e, { key: t, keys: i = t }, s) {
  if (i === null) {
    const n = /* @__PURE__ */ new Set();
    return e.map((a) => {
      const c = s && s.find(
        (u) => u.item === a && u.phase !== "leave" && !n.has(u)
      );
      return c ? (n.add(c), c.key) : Gt++;
    });
  }
  return o.und(i) ? e : o.fun(i) ? e.map(i) : j(i);
}
var bt = class extends ke {
  constructor(e, t) {
    super(), this.source = e, this.idle = !0, this._active = /* @__PURE__ */ new Set(), this.calc = It(...t);
    const i = this._get(), s = Ae(i);
    st(this, s.create(i));
  }
  advance(e) {
    const t = this._get(), i = this.get();
    K(t, i) || ($(this).setValue(t), this._onChange(t, this.idle)), !this.idle && We(this._active) && Ie(this);
  }
  _get() {
    const e = o.arr(this.source) ? this.source.map(Q) : j(Q(this.source));
    return this.calc(...e);
  }
  _start() {
    this.idle && !We(this._active) && (this.idle = !1, S(je(this), (e) => {
      e.done = !1;
    }), W.skipAnimation ? (N.batchedUpdates(() => this.advance()), Ie(this)) : Oe.start(this));
  }
  // Observe our sources only when we're observed.
  _attach() {
    let e = 1;
    S(j(this.source), (t) => {
      Y(t) && ye(t, this), xe(t) && (t.idle || this._active.add(t), e = Math.max(e, t.priority + 1));
    }), this.priority = e, this._start();
  }
  // Stop observing our sources once we have no observers.
  _detach() {
    S(j(this.source), (e) => {
      Y(e) && et(e, this);
    }), this._active.clear(), Ie(this);
  }
  /** @internal */
  eventObserved(e) {
    e.type == "change" ? e.idle ? this.advance() : (this._active.add(e.parent), this._start()) : e.type == "idle" ? this._active.delete(e.parent) : e.type == "priority" && (this.priority = j(this.source).reduce(
      (t, i) => Math.max(t, (xe(i) ? i.priority : 0) + 1),
      0
    ));
  }
};
function $t(e) {
  return e.idle !== !1;
}
function We(e) {
  return !e.size || Array.from(e).every($t);
}
function Ie(e) {
  e.idle || (e.idle = !0, S(je(e), (t) => {
    t.done = !0;
  }), _e(e, {
    type: "idle",
    parent: e
  }));
}
var ei = (e, ...t) => new bt(e, t);
W.assign({
  createStringInterpolator: Tt,
  to: (e, t) => new bt(e, t)
});
export {
  Ge as BailSignal,
  dt as Controller,
  ke as FrameValue,
  W as Globals,
  bt as Interpolation,
  vt as SpringContext,
  Pt as SpringRef,
  Dt as SpringValue,
  Ot as config,
  It as createInterpolator,
  Ct as easings,
  qe as inferTo,
  ei as to,
  ue as useIsomorphicLayoutEffect,
  Xt as useSpring,
  Bt as useSprings,
  Zt as useTransition
};
