const debounce = (func, delay) => {
    let timeoutId = null;
    let lastArgs = null;
    let lastThis;
    const debounced = function (...args) {
        // Clear the previous timeout if it exists
        if (timeoutId)
            clearTimeout(timeoutId);
        lastArgs = args;
        lastThis = this;
        // Set a new timeout
        timeoutId = setTimeout(() => {
            if (lastArgs) {
                func.apply(lastThis, lastArgs);
                lastArgs = null;
                lastThis = null;
            }
            timeoutId = null;
        }, delay);
    };
    // Method to cancel the debounced function
    debounced.cancel = () => {
        if (timeoutId)
            clearTimeout(timeoutId);
        timeoutId = null;
        lastArgs = null;
        lastThis = null;
    };
    // Method to flush the debounced function without waiting for delay
    debounced.flush = () => {
        if (timeoutId) {
            clearTimeout(timeoutId);
            if (lastArgs) {
                func.apply(lastThis, lastArgs);
                lastArgs = null;
                lastThis = null;
            }
            timeoutId = null;
        }
    };
    return debounced;
};

export { debounce };
//# sourceMappingURL=debounce.js.map
