import { __read, __spreadArray } from "tslib";
import { useCallback, useEffect, useRef } from "react";
var useDebounce = function (func, wait) {
    if (wait === void 0) { wait = 0; }
    var timeoutRef = useRef(null);
    useEffect(function () { return function () {
        timeoutRef.current && clearTimeout(timeoutRef.current);
    }; }, [wait]);
    return useCallback(function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        if (timeoutRef.current) {
            clearTimeout(timeoutRef.current);
        }
        timeoutRef.current = setTimeout(function () {
            timeoutRef.current = null;
            if (typeof func === "function") {
                func.apply(void 0, __spreadArray([], __read(args), false));
            }
        }, wait);
    }, [wait, func]);
};
export default useDebounce;
