import { generateLinearEasing as L } from "../waapi/utils/linear.es.js";
import { calcGeneratorDuration as z, maxGeneratorDuration as B } from "./utils/calc-duration.es.js";
import { createGeneratorEasing as H } from "./utils/create-generator-easing.es.js";
import { millisecondsToSeconds as K, secondsToMilliseconds as q } from "../../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/time-conversion.es.js";
import { clamp as N } from "../../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/clamp.es.js";
import { warning as J } from "../../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/errors.es.js";
const n = {
  // Default spring physics
  stiffness: 100,
  damping: 10,
  mass: 1,
  velocity: 0,
  // Default duration/bounce-based options
  duration: 800,
  // in ms
  bounce: 0.3,
  visualDuration: 0.3,
  // in seconds
  // Rest thresholds
  restSpeed: {
    granular: 0.01,
    default: 2
  },
  restDelta: {
    granular: 5e-3,
    default: 0.5
  },
  // Limits
  minDuration: 0.01,
  // in seconds
  maxDuration: 10,
  // in seconds
  minDamping: 0.05,
  maxDamping: 1
};
function O(e, s) {
  return e * Math.sqrt(1 - s * s);
}
const Q = 12;
function U(e, s, a) {
  let l = a;
  for (let f = 1; f < Q; f++)
    l = l - e(l) / s(l);
  return l;
}
const I = 1e-3;
function W({ duration: e = n.duration, bounce: s = n.bounce, velocity: a = n.velocity, mass: l = n.mass }) {
  let f, v;
  J(e <= q(n.maxDuration), "Spring duration must be 10 seconds or less", "spring-duration-limit");
  let r = 1 - s;
  r = N(n.minDamping, n.maxDamping, r), e = N(n.minDuration, n.maxDuration, K(e)), r < 1 ? (f = (c) => {
    const g = c * r, h = g * e, k = g - a, x = O(c, r), M = Math.exp(-h);
    return I - k / x * M;
  }, v = (c) => {
    const h = c * r * e, k = h * a + a, x = Math.pow(r, 2) * Math.pow(c, 2) * e, M = Math.exp(-h), o = O(Math.pow(c, 2), r);
    return (-f(c) + I > 0 ? -1 : 1) * ((k - x) * M) / o;
  }) : (f = (c) => {
    const g = Math.exp(-c * e), h = (c - a) * e + 1;
    return -I + g * h;
  }, v = (c) => {
    const g = Math.exp(-c * e), h = (a - c) * (e * e);
    return g * h;
  });
  const p = 5 / e, T = U(f, v, p);
  if (e = q(e), isNaN(T))
    return {
      stiffness: n.stiffness,
      damping: n.damping,
      duration: e
    };
  {
    const c = Math.pow(T, 2) * l;
    return {
      stiffness: c,
      damping: r * 2 * Math.sqrt(l * c),
      duration: e
    };
  }
}
const X = ["duration", "bounce"], Y = ["stiffness", "damping", "mass"];
function V(e, s) {
  return s.some((a) => e[a] !== void 0);
}
function Z(e) {
  let s = {
    velocity: n.velocity,
    stiffness: n.stiffness,
    damping: n.damping,
    mass: n.mass,
    isResolvedFromDuration: !1,
    ...e
  };
  if (!V(e, Y) && V(e, X))
    if (s.velocity = 0, e.visualDuration) {
      const a = e.visualDuration, l = 2 * Math.PI / (a * 1.2), f = l * l, v = 2 * N(0.05, 1, 1 - (e.bounce || 0)) * Math.sqrt(f);
      s = {
        ...s,
        mass: n.mass,
        stiffness: f,
        damping: v
      };
    } else {
      const a = W({ ...e, velocity: 0 });
      s = {
        ...s,
        ...a,
        mass: n.mass
      }, s.isResolvedFromDuration = !0;
    }
  return s;
}
function j(e = n.visualDuration, s = n.bounce) {
  const a = typeof e != "object" ? {
    visualDuration: e,
    keyframes: [0, 1],
    bounce: s
  } : e;
  let { restSpeed: l, restDelta: f } = a;
  const v = a.keyframes[0], r = a.keyframes[a.keyframes.length - 1], p = { done: !1, value: v }, { stiffness: T, damping: c, mass: g, duration: h, velocity: k, isResolvedFromDuration: x } = Z({
    ...a,
    velocity: -K(a.velocity || 0)
  }), M = k || 0, o = c / (2 * Math.sqrt(T * g)), m = r - v, i = K(Math.sqrt(T / g)), P = Math.abs(m) < 5;
  l || (l = P ? n.restSpeed.granular : n.restSpeed.default), f || (f = P ? n.restDelta.granular : n.restDelta.default);
  let G, w, d, C, A, F;
  if (o < 1)
    d = O(i, o), C = (M + o * i * m) / d, G = (t) => {
      const u = Math.exp(-o * i * t);
      return r - u * (C * Math.sin(d * t) + m * Math.cos(d * t));
    }, A = o * i * C + m * d, F = o * i * m - C * d, w = (t) => Math.exp(-o * i * t) * (A * Math.sin(d * t) + F * Math.cos(d * t));
  else if (o === 1) {
    G = (u) => r - Math.exp(-i * u) * (m + (M + i * m) * u);
    const t = M + i * m;
    w = (u) => Math.exp(-i * u) * (i * t * u - M);
  } else {
    const t = i * Math.sqrt(o * o - 1);
    G = (y) => {
      const S = Math.exp(-o * i * y), b = Math.min(t * y, 300);
      return r - S * ((M + o * i * m) * Math.sinh(b) + t * m * Math.cosh(b)) / t;
    };
    const u = (M + o * i * m) / t, D = o * i * u - m * t, R = o * i * m - u * t;
    w = (y) => {
      const S = Math.exp(-o * i * y), b = Math.min(t * y, 300);
      return S * (D * Math.sinh(b) + R * Math.cosh(b));
    };
  }
  const E = {
    calculatedDuration: x && h || null,
    velocity: (t) => q(w(t)),
    next: (t) => {
      if (!x && o < 1) {
        const D = Math.exp(-o * i * t), R = Math.sin(d * t), y = Math.cos(d * t), S = r - D * (C * R + m * y), b = q(D * (A * R + F * y));
        return p.done = Math.abs(b) <= l && Math.abs(r - S) <= f, p.value = p.done ? r : S, p;
      }
      const u = G(t);
      if (x)
        p.done = t >= h;
      else {
        const D = q(w(t));
        p.done = Math.abs(D) <= l && Math.abs(r - u) <= f;
      }
      return p.value = p.done ? r : u, p;
    },
    toString: () => {
      const t = Math.min(z(E), B), u = L((D) => E.next(t * D).value, t, 30);
      return t + "ms " + u;
    },
    toTransition: () => {
    }
  };
  return E;
}
j.applyToOptions = (e) => {
  const s = H(e, 100, j);
  return e.ease = s.ease, e.duration = q(s.duration), e.type = "keyframes", e;
};
export {
  j as spring
};
//# sourceMappingURL=spring.es.js.map
