/**
 * Debounce function to limit API calls and improve performance
 *
 * @param {Function} func - Function to debounce
 * @param {number} wait - Milliseconds to wait before executing
 * @returns {Function} Debounced function
 *
 * @example
 * const debouncedSearch = debounce(searchFunction, 500);
 * input.addEventListener('input', () => debouncedSearch(input.value));
 */
export function debounce(func, wait = 500) {
  let timeout;

  return function executedFunction(...args) {
    const later = () => {
      clearTimeout(timeout);
      func(...args);
    };

    clearTimeout(timeout);
    timeout = setTimeout(later, wait);
  };
}
