export class TimeWindow {
  static readonly DEFAULT_ONLINE_WINDOW_SECONDS = 300; // 5分钟（行业标准）
  static readonly DEFAULT_HEARTBEAT_INTERVAL_SECONDS = 60; // 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;
  }
}
