"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AverageWindow = void 0;
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
/**
 * Helper class to calculate running average of the most recent n values
 */
// @ts-nocheck
class AverageWindow {
  constructor(size, startValue) {
    _defineProperty(this, "currentAverage", void 0);
    _defineProperty(this, "currentCount", void 0);
    _defineProperty(this, "inputValues", void 0);
    _defineProperty(this, "nextIndex", 0);
    this.inputValues = new Array(Math.max(1, size));
    this.currentAverage = startValue !== null && startValue !== void 0 ? startValue : 0;
    this.currentCount = startValue === undefined ? 0 : 1;
    this.nextIndex = this.currentCount;
    this.inputValues[0] = startValue;
  }

  /**
   * Can be used to get the current average value
   */
  get currentValue() {
    return this.currentAverage;
  }

  /**
   *
   * @param value Add new value to the average window and updated current average
   */
  addValue(value) {
    const target = this.getNextIndex();
    const oldValue = this.inputValues[target];
    const newCount = oldValue === undefined ? this.currentCount + 1 : this.currentCount;
    this.inputValues[target] = value;
    this.currentAverage = this.currentAverage * (this.currentCount / newCount) + (value - (oldValue !== null && oldValue !== void 0 ? oldValue : 0)) / newCount;
    this.currentCount = newCount;
  }
  getNextIndex() {
    // starts from 0 once we reach end of the array
    const newTarget = this.nextIndex;
    this.nextIndex = (this.nextIndex + 1) % this.inputValues.length;
    return newTarget;
  }
}
exports.AverageWindow = AverageWindow;
//# sourceMappingURL=AverageWindow.js.map