export class DataSource {
  private constructor(
    public readonly sources: string[],
    public readonly queryTime: Date,
    public readonly dataFreshness: number,
  ) {}

  static create(sources: string[]): DataSource {
    return new DataSource(sources, new Date(), 0);
  }

  static withFreshness(sources: string[], freshnessSeconds: number): DataSource {
    return new DataSource(sources, new Date(), freshnessSeconds);
  }

  isFresh(maxAgeSeconds: number): boolean {
    const age = (Date.now() - this.queryTime.getTime()) / 1000;
    return age <= maxAgeSeconds;
  }

  addSource(source: string): DataSource {
    return new DataSource(
      [...this.sources, source],
      this.queryTime,
      this.dataFreshness,
    );
  }
}
