export class TimeWindow {
  static readonly DEFAULT_ONLINE_WINDOW_SECONDS = 300;
  static readonly DEFAULT_HEARTBEAT_INTERVAL_SECONDS = 60;

  private readonly _windowSeconds: number;

  private constructor(windowSeconds: number) {
    this._windowSeconds = windowSeconds;
  }

  get windowSeconds(): number {
    return this._windowSeconds;
  }

  static default(): TimeWindow {
    return new TimeWindow(TimeWindow.DEFAULT_ONLINE_WINDOW_SECONDS);
  }

  static ofSeconds(seconds: number): TimeWindow {
    if (seconds <= 0) throw new Error('TimeWindow must be positive');
    return new TimeWindow(seconds);
  }

  getThresholdTimestamp(now: Date = new Date()): number {
    return Math.floor(now.getTime() / 1000) - this._windowSeconds;
  }
}
