/**
 * The `Timer` class.
 *
 * @public
 */
export class Timer {
    // #region Fields
    _callback;
    _timerId;
    _startTime;
    _duration;
    _remaining;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `Timer` class.
     *
     * @public
     */
    constructor(callback, duration) {
        this._callback = callback;
        this._timerId = null;
        this._remaining = null;
        this.setDuration(duration);
    }
    // #endregion
    // #region Methods
    static empty(callback) {
        return new Timer(callback, 0);
    }
    setDuration(duration) {
        this._duration = duration;
        // TODO: Should calculate true offset of _remaining
        if (this._timerId !== null && this._duration) {
            this.restart();
        }
    }
    /**
     * Starts the timer.
     */
    start() {
        if (this._timerId === null) {
            this.resume();
        }
    }
    /**
     * Restarts the timer by setting remaining time to duration.
     */
    restart() {
        this._remaining = this._duration;
        this.resume();
    }
    pause() {
        if (this._timerId !== null) {
            window.clearTimeout(this._timerId);
            this._timerId = null;
            if (this._remaining !== null) {
                this._remaining -= Date.now() - this._startTime;
            }
        }
    }
    resume() {
        if (this._timerId !== null) {
            window.clearTimeout(this._timerId);
        }
        this._remaining ??= this._duration;
        this._startTime = Date.now();
        this._timerId = window.setTimeout(() => {
            this._remaining = null;
            this._callback();
        }, this._remaining);
    }
    destroy() {
        this.pause();
    }
}
//# sourceMappingURL=Timer.js.map