import { eachProp as m, is as i, hasFluidValue as v, getFluidValue as $, each as o, isAnimatedString as N, defineHidden as j, useForceUpdate as B, useIsomorphicLayoutEffect as D, addFluidObserver as K, removeFluidObserver as P, useOnce as L, createInterpolator as b } from "./index-react-spring_shared.modern.js";
import * as M from "react";
import { forwardRef as q, useRef as S, useCallback as z, useEffect as G } from "react";
import { raf as O } from "./index-react-spring_rafz.modern.js";
var h = Symbol.for("Animated:node"), J = (e) => !!e && e[h] === e, Q = (e) => e && e[h], W = (e, t) => j(e, h, t), X = (e) => e && e[h] && e[h].getPayload(), R = class {
  constructor() {
    W(this, this);
  }
  /** Get every `AnimatedValue` used by this node. */
  getPayload() {
    return this.payload || [];
  }
}, p = class F extends R {
  constructor(t) {
    super(), this._value = t, this.done = !0, this.durationProgress = 0, i.num(this._value) && (this.lastPosition = this._value);
  }
  /** @internal */
  static create(t) {
    return new F(t);
  }
  getPayload() {
    return [this];
  }
  getValue() {
    return this._value;
  }
  setValue(t, s) {
    return i.num(t) && (this.lastPosition = t, s && (t = Math.round(t / s) * s, this.done && (this.lastPosition = t))), this._value === t ? !1 : (this._value = t, !0);
  }
  reset() {
    const { done: t } = this;
    this.done = !1, i.num(this._value) && (this.elapsedTime = 0, this.durationProgress = 0, this.lastPosition = this._value, t && (this.lastVelocity = null), this.v0 = null);
  }
}, U = class E extends p {
  constructor(t) {
    super(0), this._string = null, this._toString = b({
      output: [t, t]
    });
  }
  /** @internal */
  static create(t) {
    return new E(t);
  }
  getValue() {
    const t = this._string;
    return t ?? (this._string = this._toString(this._value));
  }
  setValue(t) {
    if (i.str(t)) {
      if (t == this._string)
        return !1;
      this._string = t, this._value = 1;
    } else if (super.setValue(t))
      this._string = null;
    else
      return !1;
    return !0;
  }
  reset(t) {
    t && (this._toString = b({
      output: [this.getValue(), t]
    })), this._value = 0, super.reset();
  }
}, f = { dependencies: null }, y = class extends R {
  constructor(e) {
    super(), this.source = e, this.setValue(e);
  }
  getValue(e) {
    const t = {};
    return m(this.source, (s, u) => {
      J(s) ? t[u] = s.getValue(e) : v(s) ? t[u] = $(s) : e || (t[u] = s);
    }), t;
  }
  /** Replace the raw object data */
  setValue(e) {
    this.source = e, this.payload = this._makePayload(e);
  }
  reset() {
    this.payload && o(this.payload, (e) => e.reset());
  }
  /** Create a payload set. */
  _makePayload(e) {
    if (e) {
      const t = /* @__PURE__ */ new Set();
      return m(e, this._addToPayload, t), Array.from(t);
    }
  }
  /** Add to a payload set. */
  _addToPayload(e) {
    f.dependencies && v(e) && f.dependencies.add(e);
    const t = X(e);
    t && o(t, (s) => this.add(s));
  }
}, Y = class I extends y {
  constructor(t) {
    super(t);
  }
  /** @internal */
  static create(t) {
    return new I(t);
  }
  getValue() {
    return this.source.map((t) => t.getValue());
  }
  setValue(t) {
    const s = this.getPayload();
    return t.length == s.length ? s.map((u, n) => u.setValue(t[n])).some(Boolean) : (super.setValue(t.map(Z)), !0);
  }
};
function Z(e) {
  return (N(e) ? U : p).create(e);
}
function ie(e) {
  const t = Q(e);
  return t ? t.constructor : i.arr(e) ? Y : N(e) ? U : p;
}
var w = (e, t) => {
  const s = (
    // Function components must use "forwardRef" to avoid being
    // re-rendered on every animation frame.
    !i.fun(e) || e.prototype && e.prototype.isReactComponent
  );
  return q((u, n) => {
    const d = S(null), r = s && // eslint-disable-next-line react-hooks/rules-of-hooks
    z(
      (a) => {
        d.current = te(n, a);
      },
      [n]
    ), [l, g] = ee(u, t), k = B(), A = () => {
      const a = d.current;
      if (s && !a)
        return;
      (a ? t.applyAnimatedValues(a, l.getValue(!0)) : !1) === !1 && k();
    }, _ = new C(A, g), c = S(void 0);
    D(() => (c.current = _, o(g, (a) => K(a, _)), () => {
      c.current && (o(
        c.current.deps,
        (a) => P(a, c.current)
      ), O.cancel(c.current.update));
    })), G(A, []), L(() => () => {
      const a = c.current;
      o(a.deps, (V) => P(V, a));
    });
    const H = t.getComponentProps(l.getValue());
    return /* @__PURE__ */ M.createElement(e, { ...H, ref: r });
  });
}, C = class {
  constructor(e, t) {
    this.update = e, this.deps = t;
  }
  eventObserved(e) {
    e.type == "change" && O.write(this.update);
  }
};
function ee(e, t) {
  const s = /* @__PURE__ */ new Set();
  return f.dependencies = s, e.style && (e = {
    ...e,
    style: t.createAnimatedStyle(e.style)
  }), e = new y(e), f.dependencies = null, [e, s];
}
function te(e, t) {
  return e && (i.fun(e) ? e(t) : e.current = t), t;
}
var T = Symbol.for("AnimatedComponent"), ne = (e, {
  applyAnimatedValues: t = () => !1,
  createAnimatedStyle: s = (n) => new y(n),
  getComponentProps: u = (n) => n
} = {}) => {
  const n = {
    applyAnimatedValues: t,
    createAnimatedStyle: s,
    getComponentProps: u
  }, d = (r) => {
    const l = x(r) || "Anonymous";
    return i.str(r) ? r = d[r] || (d[r] = w(r, n)) : r = r[T] || (r[T] = w(r, n)), r.displayName = `Animated(${l})`, r;
  };
  return m(e, (r, l) => {
    i.arr(e) && (l = x(r)), d[l] = d(r);
  }), {
    animated: d
  };
}, x = (e) => i.str(e) ? e : e && i.str(e.displayName) ? e.displayName : i.fun(e) && e.name || null;
export {
  R as Animated,
  Y as AnimatedArray,
  y as AnimatedObject,
  U as AnimatedString,
  p as AnimatedValue,
  ne as createHost,
  Q as getAnimated,
  ie as getAnimatedType,
  X as getPayload,
  J as isAnimated,
  W as setAnimated
};
