export class RemainingTimeEstimator {
    /**
     *
     * @param {number} sampleCount
     * @constructor
     */
    constructor(sampleCount) {
        this.sampleCount = sampleCount;
        this.rates = new Float32Array(sampleCount);
        this.cursor = 0;
    }

    /**
     *
     * @param {number} timeElapsed
     * @param {number} progress
     */
    update(timeElapsed, progress) {
        const index = this.cursor;
        this.cursor = (this.cursor + 1) % this.sampleCount;

        const rate = progress / timeElapsed;

        this.rates[index] = rate;
    }

    /**
     *
     * @param {number} currentProgress
     * @returns {number}
     */
    estimate(currentProgress) {
        let rateSum = 0;

        for (let i = 0; i < this.sampleCount; i++) {
            rateSum += this.rates[i];
        }

        const rate = rateSum / this.sampleCount;

        if (rate === 0) {
            return Infinity;
        }

        const result = (1 - currentProgress) / rate;

        return result;
    }
}
