/**
 * Creates a debounced version of the provided function that delays execution
 * until after the specified wait time has elapsed since the last invocation.
 * @param {T} fn - The function to debounce
 * @param {number} ms - Delay in milliseconds (default: 0)
 * @returns {UmbDebouncedFunction<T>} A debounced function with a cancel method
 */
export function debounce(fn, ms = 0) {
    let timeoutId;
    const debounced = function (...args) {
        clearTimeout(timeoutId);
        timeoutId = setTimeout(() => fn.apply(this, args), ms);
    };
    debounced.cancel = () => {
        clearTimeout(timeoutId);
        timeoutId = undefined;
    };
    return debounced;
}
