"use strict";
"use client";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Timeout = void 0;
exports.useTimeout = useTimeout;
const react_1 = require("react");
const useRefWithInit_1 = require("./useRefWithInit");
const EMPTY = 0;
class Timeout {
    constructor() {
        this.currentId = EMPTY;
        this.clear = () => {
            if (this.currentId !== EMPTY) {
                clearTimeout(this.currentId);
                this.currentId = EMPTY;
            }
        };
        this.disposeEffect = () => {
            return this.clear;
        };
    }
    static create() {
        return new Timeout();
    }
    /**
     * Executes `fn` after `delay`, clearing any previously scheduled call.
     */
    start(delay, fn) {
        this.clear();
        this.currentId = setTimeout(() => {
            this.currentId = EMPTY;
            fn();
        }, delay); /* Node.js types are enabled in development */
    }
    isStarted() {
        return this.currentId !== EMPTY;
    }
}
exports.Timeout = Timeout;
/**
 * A `setTimeout` with automatic cleanup and guard.
 */
function useTimeout() {
    const timeout = (0, useRefWithInit_1.useRefWithInit)(Timeout.create).current;
    // eslint-disable-next-line react-hooks/exhaustive-deps
    (0, react_1.useEffect)(timeout.disposeEffect, []);
    return timeout;
}
//# sourceMappingURL=useTimeout.js.map