import { eachProp as g, defineHidden as H, hasFluidValue as S, getFluidValue as $, each as c, is as o, isAnimatedString as F, createInterpolator as O, useForceUpdate as B, useIsomorphicLayoutEffect as D, addFluidObserver as K, removeFluidObserver as w, useOnce as L } from "../../shared/dist/react-spring-shared.esm.mjs";
import * as M from "react";
import { forwardRef as k, useRef as T, useCallback as q, useEffect as z } from "react";
import { raf as j } from "../../rafz/dist/react-spring-rafz.esm.mjs";
const h = Symbol.for("Animated:node"), G = (e) => !!e && e[h] === e, J = (e) => e && e[h], Q = (e, t) => H(e, h, t), W = (e) => e && e[h] && e[h].getPayload();
class U {
  constructor() {
    this.payload = void 0, Q(this, this);
  }
  getPayload() {
    return this.payload || [];
  }
}
class f extends U {
  constructor(t) {
    super(), this.done = !0, this.elapsedTime = void 0, this.lastPosition = void 0, this.lastVelocity = void 0, this.v0 = void 0, this.durationProgress = 0, this._value = t, o.num(this._value) && (this.lastPosition = this._value);
  }
  static create(t) {
    return new f(t);
  }
  getPayload() {
    return [this];
  }
  getValue() {
    return this._value;
  }
  setValue(t, s) {
    return o.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, o.num(this._value) && (this.elapsedTime = 0, this.durationProgress = 0, this.lastPosition = this._value, t && (this.lastVelocity = null), this.v0 = null);
  }
}
class y extends f {
  constructor(t) {
    super(0), this._string = null, this._toString = void 0, this._toString = O({
      output: [t, t]
    });
  }
  static create(t) {
    return new y(t);
  }
  getValue() {
    let t = this._string;
    return t == null ? this._string = this._toString(this._value) : t;
  }
  setValue(t) {
    if (o.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 = O({
      output: [this.getValue(), t]
    })), this._value = 0, super.reset();
  }
}
const p = {
  dependencies: null
};
class V extends U {
  constructor(t) {
    super(), this.source = t, this.setValue(t);
  }
  getValue(t) {
    const s = {};
    return g(this.source, (i, r) => {
      G(i) ? s[r] = i.getValue(t) : S(i) ? s[r] = $(i) : t || (s[r] = i);
    }), s;
  }
  setValue(t) {
    this.source = t, this.payload = this._makePayload(t);
  }
  reset() {
    this.payload && c(this.payload, (t) => t.reset());
  }
  _makePayload(t) {
    if (t) {
      const s = /* @__PURE__ */ new Set();
      return g(t, this._addToPayload, s), Array.from(s);
    }
  }
  _addToPayload(t) {
    p.dependencies && S(t) && p.dependencies.add(t);
    const s = W(t);
    s && c(s, (i) => this.add(i));
  }
}
class P extends V {
  constructor(t) {
    super(t);
  }
  static create(t) {
    return new P(t);
  }
  getValue() {
    return this.source.map((t) => t.getValue());
  }
  setValue(t) {
    const s = this.getPayload();
    return t.length == s.length ? s.map((i, r) => i.setValue(t[r])).some(Boolean) : (super.setValue(t.map(X)), !0);
  }
}
function X(e) {
  return (F(e) ? y : f).create(e);
}
function it(e) {
  const t = J(e);
  return t ? t.constructor : o.arr(e) ? P : F(e) ? y : f;
}
function m() {
  return m = Object.assign ? Object.assign.bind() : function(e) {
    for (var t = 1; t < arguments.length; t++) {
      var s = arguments[t];
      for (var i in s)
        Object.prototype.hasOwnProperty.call(s, i) && (e[i] = s[i]);
    }
    return e;
  }, m.apply(this, arguments);
}
const x = (e, t) => {
  const s = !o.fun(e) || e.prototype && e.prototype.isReactComponent;
  return k((i, r) => {
    const l = T(null), n = s && q((a) => {
      l.current = C(r, a);
    }, [r]), [d, _] = Z(i, t), E = B(), A = () => {
      const a = l.current;
      if (s && !a)
        return;
      (a ? t.applyAnimatedValues(a, d.getValue(!0)) : !1) === !1 && E();
    }, b = new Y(A, _), u = T();
    D(() => (u.current = b, c(_, (a) => K(a, b)), () => {
      u.current && (c(u.current.deps, (a) => w(a, u.current)), j.cancel(u.current.update));
    })), z(A, []), L(() => () => {
      const a = u.current;
      c(a.deps, (v) => w(v, a));
    });
    const I = t.getComponentProps(d.getValue());
    return M.createElement(e, m({}, I, {
      ref: n
    }));
  });
};
class Y {
  constructor(t, s) {
    this.update = t, this.deps = s;
  }
  eventObserved(t) {
    t.type == "change" && j.write(this.update);
  }
}
function Z(e, t) {
  const s = /* @__PURE__ */ new Set();
  return p.dependencies = s, e.style && (e = m({}, e, {
    style: t.createAnimatedStyle(e.style)
  })), e = new V(e), p.dependencies = null, [e, s];
}
function C(e, t) {
  return e && (o.fun(e) ? e(t) : e.current = t), t;
}
const N = Symbol.for("AnimatedComponent"), nt = (e, {
  applyAnimatedValues: t = () => !1,
  createAnimatedStyle: s = (r) => new V(r),
  getComponentProps: i = (r) => r
} = {}) => {
  const r = {
    applyAnimatedValues: t,
    createAnimatedStyle: s,
    getComponentProps: i
  }, l = (n) => {
    const d = R(n) || "Anonymous";
    return o.str(n) ? n = l[n] || (l[n] = x(n, r)) : n = n[N] || (n[N] = x(n, r)), n.displayName = `Animated(${d})`, n;
  };
  return g(e, (n, d) => {
    o.arr(e) && (d = R(n)), l[d] = l(n);
  }), {
    animated: l
  };
}, R = (e) => o.str(e) ? e : e && o.str(e.displayName) ? e.displayName : o.fun(e) && e.name || null;
export {
  U as Animated,
  P as AnimatedArray,
  V as AnimatedObject,
  y as AnimatedString,
  f as AnimatedValue,
  nt as createHost,
  J as getAnimated,
  it as getAnimatedType,
  W as getPayload,
  G as isAnimated,
  Q as setAnimated
};
