export class Timer {
  private startTime: number;
  private remaining: number;
  private timeoutId: NodeJS.Timeout | null;

  constructor(
    private callback: () => void,
    private duration: number
  ) {
    this.remaining = duration;
    this.timeoutId = null;
  }

  start(): void {
    this.startTime = Date.now();
    this.timeoutId = setTimeout(this.callback, this.remaining);

    this.remaining = 0;
  }

  pause(): void {
    this.clear();

    this.remaining -= Date.now() - this.startTime;
  }

  resume(): void {
    this.start();
  }

  clear(): void {
    clearTimeout(this.timeoutId);
  }
}
