{"version":3,"file":"Timer.mjs","names":[],"sources":["../../../../../../../src/lib/timer/Timer.ts"],"sourcesContent":["import type { DurationLike } from \"luxon\";\nimport { DateTime, Duration } from \"luxon\";\n\nexport class Timer {\n  private duration: Duration = Duration.fromDurationLike(0);\n  private onDone: (() => void) | undefined;\n\n  private startedAt?: DateTime;\n  private pausedAt?: DateTime;\n  private runningTimeoutId?: number;\n\n  public start(duration: DurationLike, onDone?: () => void): void {\n    if (this.startedAt) {\n      throw new Error(\"Timer already started\");\n    }\n\n    this.duration = Duration.fromDurationLike(duration);\n    this.onDone = onDone;\n    this.startTimeout(this.duration);\n  }\n\n  public restart(): void {\n    this.stop();\n    this.startTimeout(this.duration);\n  }\n\n  public pause(): void {\n    this.clearTimeout();\n    const now = DateTime.now();\n    this.pausedAt = now;\n    if (!this.startedAt) {\n      this.startedAt = now;\n    }\n  }\n\n  public resume(): void {\n    if (!this.startedAt) {\n      throw new Error(\"Timer not paused\");\n    } else if (this.pausedAt) {\n      const elapsed = this.pausedAt.diff(this.startedAt);\n      const remainingDuration = this.duration.minus(elapsed);\n      this.startTimeout(remainingDuration);\n    }\n  }\n\n  public stop(): void {\n    this.clearTimeout();\n    this.startedAt = undefined;\n    this.pausedAt = undefined;\n  }\n\n  public get state(): \"idle\" | \"running\" | \"paused\" {\n    return this.pausedAt ? \"paused\" : this.startedAt ? \"running\" : \"idle\";\n  }\n\n  private clearTimeout(): void {\n    if (this.runningTimeoutId) {\n      window.clearTimeout(this.runningTimeoutId);\n    }\n    this.runningTimeoutId = undefined;\n  }\n\n  private startTimeout(duration: Duration): void {\n    this.startedAt = DateTime.now();\n    this.pausedAt = undefined;\n    this.runningTimeoutId = window.setTimeout(() => {\n      this.stop();\n      if (this.onDone) {\n        this.onDone();\n      }\n    }, duration.toMillis());\n  }\n}\n\nexport default Timer;\n"],"mappings":";;;;AAGA,IAAa,QAAb,MAAmB;CACjB,WAA6B,SAAS,iBAAiB,CAAC;CACxD;CAEA;CACA;CACA;CAEA,MAAa,UAAwB,QAA2B;EAC9D,IAAI,KAAK,WACP,MAAM,IAAI,MAAM,uBAAuB;EAGzC,KAAK,WAAW,SAAS,iBAAiB,QAAQ;EAClD,KAAK,SAAS;EACd,KAAK,aAAa,KAAK,QAAQ;CACjC;CAEA,UAAuB;EACrB,KAAK,KAAK;EACV,KAAK,aAAa,KAAK,QAAQ;CACjC;CAEA,QAAqB;EACnB,KAAK,aAAa;EAClB,MAAM,MAAM,SAAS,IAAI;EACzB,KAAK,WAAW;EAChB,IAAI,CAAC,KAAK,WACR,KAAK,YAAY;CAErB;CAEA,SAAsB;EACpB,IAAI,CAAC,KAAK,WACR,MAAM,IAAI,MAAM,kBAAkB;OAC7B,IAAI,KAAK,UAAU;GACxB,MAAM,UAAU,KAAK,SAAS,KAAK,KAAK,SAAS;GACjD,MAAM,oBAAoB,KAAK,SAAS,MAAM,OAAO;GACrD,KAAK,aAAa,iBAAiB;EACrC;CACF;CAEA,OAAoB;EAClB,KAAK,aAAa;EAClB,KAAK,YAAY,KAAA;EACjB,KAAK,WAAW,KAAA;CAClB;CAEA,IAAW,QAAuC;EAChD,OAAO,KAAK,WAAW,WAAW,KAAK,YAAY,YAAY;CACjE;CAEA,eAA6B;EAC3B,IAAI,KAAK,kBACP,OAAO,aAAa,KAAK,gBAAgB;EAE3C,KAAK,mBAAmB,KAAA;CAC1B;CAEA,aAAqB,UAA0B;EAC7C,KAAK,YAAY,SAAS,IAAI;EAC9B,KAAK,WAAW,KAAA;EAChB,KAAK,mBAAmB,OAAO,iBAAiB;GAC9C,KAAK,KAAK;GACV,IAAI,KAAK,QACP,KAAK,OAAO;EAEhB,GAAG,SAAS,SAAS,CAAC;CACxB;AACF"}