/**
 *
 * Throttle function so it is only called once every n milliseconds
 *
 * @example
 * Utils.throttle(myFunction() {}, 100);
 *
 * @param {Function} func - Function to be throttled
 * @param {number} wait - Time in miliseconds
 *
 * @returns {Function} - Throttled function
 */
export function throttle<T extends unknown[]>(
  func: (...args: T) => void,
  wait: number
): (...args: T) => void {
  let timeout: ReturnType<typeof setTimeout> | null = null

  return (...args: T) => {
    if (timeout) return // Check if a timeout is already active

    timeout = setTimeout(() => {
      timeout = null
      func(...args)
    }, wait)
  }
}
