// src/series-convergence.ts
function analyzeSeries(values, options = {}) {
  const window = options.window ?? 5;
  const stableCv = options.stableCv ?? 0.05;
  const driftRun = options.driftRun ?? 3;
  if (values.length < Math.max(2, Math.min(window, 3))) {
    return { state: "insufficient-data", windowMean: 0, windowCv: 0, tailRun: 0, stable: false };
  }
  const tail = values.slice(-window);
  const mean = tail.reduce((a, b) => a + b, 0) / tail.length;
  const variance = tail.reduce((acc, v) => acc + (v - mean) ** 2, 0) / tail.length;
  const stdDev = Math.sqrt(variance);
  const refMean = Math.abs(mean) > 1e-9 ? Math.abs(mean) : 1;
  const cv = stdDev / refMean;
  const stable = tail.length >= window && cv <= stableCv;
  let tailRun = 0;
  let direction = 0;
  for (let i = values.length - 1; i > 0; i--) {
    const delta = values[i] - values[i - 1];
    if (delta === 0) break;
    const dir = delta > 0 ? 1 : -1;
    if (direction === 0) direction = dir;
    if (dir !== direction) break;
    tailRun += dir;
  }
  let state;
  if (stable) {
    state = "stabilized";
  } else if (Math.abs(tailRun) >= driftRun) {
    state = tailRun > 0 ? "drifting-up" : "drifting-down";
  } else {
    state = "noisy";
  }
  return { state, windowMean: mean, windowCv: cv, tailRun, stable };
}

export {
  analyzeSeries
};
//# sourceMappingURL=chunk-BOD4O7OF.js.map