"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const errors = require("../motion-utils/dist-es-errors.cjs");
const timeConversion = require("../motion-utils/dist-es-time-conversion.cjs");
const clamp = require("./dist-es-utils-clamp.cjs");
const defaults = require("./dist-es-animation-generators-spring-defaults.cjs");
const safeMin = 1e-3;
function findSpring({ duration = defaults.springDefaults.duration, bounce = defaults.springDefaults.bounce, velocity = defaults.springDefaults.velocity, mass = defaults.springDefaults.mass }) {
  let envelope;
  let derivative;
  errors.warning(duration <= timeConversion.secondsToMilliseconds(defaults.springDefaults.maxDuration), "Spring duration must be 10 seconds or less");
  let dampingRatio = 1 - bounce;
  dampingRatio = clamp.clamp(defaults.springDefaults.minDamping, defaults.springDefaults.maxDamping, dampingRatio);
  duration = clamp.clamp(defaults.springDefaults.minDuration, defaults.springDefaults.maxDuration, timeConversion.millisecondsToSeconds(duration));
  if (dampingRatio < 1) {
    envelope = (undampedFreq2) => {
      const exponentialDecay = undampedFreq2 * dampingRatio;
      const delta = exponentialDecay * duration;
      const a = exponentialDecay - velocity;
      const b = calcAngularFreq(undampedFreq2, dampingRatio);
      const c = Math.exp(-delta);
      return safeMin - a / b * c;
    };
    derivative = (undampedFreq2) => {
      const exponentialDecay = undampedFreq2 * dampingRatio;
      const delta = exponentialDecay * duration;
      const d = delta * velocity + velocity;
      const e = Math.pow(dampingRatio, 2) * Math.pow(undampedFreq2, 2) * duration;
      const f = Math.exp(-delta);
      const g = calcAngularFreq(Math.pow(undampedFreq2, 2), dampingRatio);
      const factor = -envelope(undampedFreq2) + safeMin > 0 ? -1 : 1;
      return factor * ((d - e) * f) / g;
    };
  } else {
    envelope = (undampedFreq2) => {
      const a = Math.exp(-undampedFreq2 * duration);
      const b = (undampedFreq2 - velocity) * duration + 1;
      return -safeMin + a * b;
    };
    derivative = (undampedFreq2) => {
      const a = Math.exp(-undampedFreq2 * duration);
      const b = (velocity - undampedFreq2) * (duration * duration);
      return a * b;
    };
  }
  const initialGuess = 5 / duration;
  const undampedFreq = approximateRoot(envelope, derivative, initialGuess);
  duration = timeConversion.secondsToMilliseconds(duration);
  if (isNaN(undampedFreq)) {
    return {
      stiffness: defaults.springDefaults.stiffness,
      damping: defaults.springDefaults.damping,
      duration
    };
  } else {
    const stiffness = Math.pow(undampedFreq, 2) * mass;
    return {
      stiffness,
      damping: dampingRatio * 2 * Math.sqrt(mass * stiffness),
      duration
    };
  }
}
const rootIterations = 12;
function approximateRoot(envelope, derivative, initialGuess) {
  let result = initialGuess;
  for (let i = 1; i < rootIterations; i++) {
    result = result - envelope(result) / derivative(result);
  }
  return result;
}
function calcAngularFreq(undampedFreq, dampingRatio) {
  return undampedFreq * Math.sqrt(1 - dampingRatio * dampingRatio);
}
exports.calcAngularFreq = calcAngularFreq;
exports.findSpring = findSpring;
//# sourceMappingURL=dist-es-animation-generators-spring-find.cjs.map
