{"version":3,"sources":["../../src/functions/debounce/debounce.ts"],"names":[],"mappings":";AAUO,SAAS,SACd,MACA,OACA,UAMI,CAAC,GACyB;AAC9B,MAAI,YAAkD;AACtD,MAAI,WAAW;AACf,MAAI;AAEJ,WAAS,qBAAqB,MAAmC;AAC/D,sBAAkB;AAElB,QAAI,QAAQ,WAAW,CAAC,UAAU;AAChC,WAAK,GAAG,IAAI;AACZ,iBAAW;AAAA,IACb;AAEA,QAAI,cAAc,MAAM;AACtB,mBAAa,SAAS;AAAA,IACxB;AAEA,gBAAY,WAAW,MAAM;AAC3B,UAAI,CAAC,QAAQ,SAAS;AACpB,aAAK,GAAG,IAAI;AAAA,MACd;AACA,iBAAW;AAAA,IACb,GAAG,KAAK;AAAA,EACV;AAEA,oBAAkB,QAAQ,MAAM;AAC9B,QAAI,cAAc,MAAM;AACtB,mBAAa,SAAS;AAAA,IACxB;AAEA,SAAK,GAAG,eAAe;AAAA,EACzB;AAEA,oBAAkB,QAAQ,MAAM;AAC9B,QAAI,cAAc,MAAM;AACtB,mBAAa,SAAS;AAAA,IACxB;AAAA,EACF;AAEA,SAAO;AACT","sourcesContent":["import { AnyFunction } from '../../types/_internal';\n\n/**\n * Creates a debounced function that delays invoking `func` until after `delay` milliseconds have elapsed since the last time the debounced function was invoked.\n * @param func The function to debounce.\n * @param delay The number of milliseconds to delay.\n * @param options Optional configuration options.\n * @param options.leading Whether to call the function on the leading edge of the timeout.\n * @returns A debounced function that can be called multiple times, but only invokes `func` once per `delay` milliseconds.\n */\nexport function debounce<TFunction extends AnyFunction<void | undefined>>(\n  func: TFunction,\n  delay: number,\n  options: {\n    /**\n     * Whether to call the function on the leading edge of the timeout.\n     * @default false\n     */\n    leading?: boolean;\n  } = {}\n): DebouncedFunction<TFunction> {\n  let timeoutId: ReturnType<typeof setTimeout> | null = null;\n  let isCalled = false;\n  let latestArguments: Parameters<TFunction>;\n\n  function debouncedFunction(...args: Parameters<TFunction>): void {\n    latestArguments = args;\n\n    if (options.leading && !isCalled) {\n      func(...args);\n      isCalled = true;\n    }\n\n    if (timeoutId !== null) {\n      clearTimeout(timeoutId);\n    }\n\n    timeoutId = setTimeout(() => {\n      if (!options.leading) {\n        func(...args);\n      }\n      isCalled = false;\n    }, delay);\n  }\n\n  debouncedFunction.flush = () => {\n    if (timeoutId !== null) {\n      clearTimeout(timeoutId);\n    }\n\n    func(...latestArguments);\n  };\n\n  debouncedFunction.clear = () => {\n    if (timeoutId !== null) {\n      clearTimeout(timeoutId);\n    }\n  };\n\n  return debouncedFunction;\n}\n\ninterface DebouncedFunction<TFunction extends AnyFunction<void | undefined>> {\n  (...args: Parameters<TFunction>): void;\n  clear(): void;\n  flush(): void;\n}\n"]}