import Snapshot from './snapshot';

export default class UniformSnapshot extends Snapshot {
  private readonly values: Array<number>;

  constructor(values: Array<number>) {
    super();

    this.values = new Array<number>(...values).sort();
  }

  getValue(quantile: number): number {
    if (quantile < 0.0 || quantile > 1.0 || isNaN(quantile)) {
      throw new Error(`${quantile} is not in [0..1]`);
    }

    if (this.values.length === 0) {
      return 0;
    }

    const pos = quantile * (this.values.length + 1);
    // convert to integer
    const index = pos | 0;

    if (index < 1) {
      return this.values[0];
    }

    if (index >= this.values.length) {
      return this.values[this.values.length - 1];
    }

    const lower = this.values[index - 1];
    const upper = this.values[index];

    return lower + (pos - Math.floor(pos)) * (upper - lower);
  }

  getValues(): number[] {
    return new Array<number>(...this.values);
  }

  size(): number {
    return this.values.length;
  }

  getMax(): number {
    if (this.values.length === 0) {
      return 0;
    }

    return this.values[this.values.length - 1];
  }

  getMean(): number {
    if (this.values.length === 0) {
      return 0;
    }

    const sum = this.values.reduce((acc, val) => acc + val, 0);

    return sum / this.values.length;
  }

  getMin(): number {
    if (this.values.length === 0) 


    return this.values[0];
  }

  getStdDev(): number {
    if (this.values.length <= 1) {
      return 0;
    }

    const mean = this.getMean();

    const sum = this.values.reduce((acc, val) => {
      const diff = val - mean;

      return acc + diff * diff;
    }, 0);

    const variance = sum / (this.values.length - 1);

    return Math.sqrt(variance);
  }
}

{
      return 0;
    }