{"version":3,"file":"index.cjs","sources":[""],"sourcesContent":["export type TDebouncedFn<T extends (...args: any[]) => any> = ((\n  ...args: Parameters<T>\n) => ReturnType<T> | undefined) & {\n  cancel: () => void;\n  flush: () => ReturnType<T> | undefined;\n  pending: () => boolean;\n};\n\nexport type TGetDebouncedFnArgs = Parameters<typeof getDebouncedFn>;\n\r\nexport type TGetDebouncedFnReturn = ReturnType<typeof getDebouncedFn>;\r\n\r\n/**\r\n * Returns a debounced version of a function that delays invoking `cb`\r\n * until after `wait` ms have elapsed since the last call.\r\n * Optionally invokes on the leading edge when `isImmediate` is true.\r\n *\r\n * @template {(...args: any[]) => any} T\r\n * @param {T} cb Function to debounce\r\n * @param {number} [wait=250] Delay in milliseconds\r\n * @param {boolean} [isImmediate=false] If `true`, invoke on the leading edge\r\n * @returns {TDebouncedFn<T>} Debounced function with cancel, flush, and pending controls\n * @throws {TypeError} getDebouncedFn: cb must be a function\r\n * @throws {TypeError} getDebouncedFn: wait must be a non-negative finite number\r\n * @throws {TypeError} getDebouncedFn: isImmediate must be a boolean\r\n *\r\n * @example\r\n * const fn = getDebouncedFn((x: number) => console.log(x), 1000);\n * fn(1);\n * @example\n * // Debounce autocomplete requests and cancel the pending call on unmount\n * const search = getDebouncedFn((query: string) => loadSuggestions(query), 300);\n * input.addEventListener(\"input\", () => search(input.value));\n * search.cancel();\n */\nexport const getDebouncedFn = <T extends (...args: any[]) => any>(\n  cb: T,\n  wait: number = 250,\n  isImmediate: boolean = false\n): TDebouncedFn<T> => {\n  if (typeof cb !== \"function\") {\r\n    throw new TypeError(\"getDebouncedFn: cb must be a function\");\r\n  }\r\n  if (typeof wait !== \"number\" || !Number.isFinite(wait) || wait < 0) {\r\n    throw new TypeError(\"getDebouncedFn: wait must be a non-negative finite number\");\r\n  }\r\n  if (typeof isImmediate !== \"boolean\") {\r\n    throw new TypeError(\"getDebouncedFn: isImmediate must be a boolean\");\r\n  }\r\n\n  let timeout: ReturnType<typeof setTimeout> | null = null;\n  let lastArgs: Parameters<T> | null = null;\n  let lastContext: unknown;\n  let result: ReturnType<T> | undefined;\n  const invoke = (): ReturnType<T> | undefined => {\n    if (!lastArgs) {\n      return result;\n    }\n    const args = lastArgs;\n    const context = lastContext;\n    lastArgs = null;\n    lastContext = undefined;\n    result = cb.apply(context, args) as ReturnType<T>;\n    return result;\n  };\n  const debounced = function executedFunction(\n    this: unknown,\n    ...args: Parameters<T>\n  ): ReturnType<T> | undefined {\n    lastArgs = args;\n    lastContext = this;\n    const later = (): void => {\n      timeout = null;\n      if (!isImmediate) {\n        invoke();\n      } else {\n        lastArgs = null;\n        lastContext = undefined;\n      }\n    };\n    const callNow = isImmediate && !timeout;\r\n    if (timeout) {\r\n      clearTimeout(timeout);\r\n    }\r\n    timeout = setTimeout(later, wait);\n    if (callNow) {\n      return invoke();\n    }\n    return result;\n  } as TDebouncedFn<T>;\n\n  debounced.cancel = (): void => {\n    if (timeout) {\n      clearTimeout(timeout);\n    }\n    timeout = null;\n    lastArgs = null;\n    lastContext = undefined;\n  };\n  debounced.flush = (): ReturnType<T> | undefined => {\n    if (timeout) {\n      clearTimeout(timeout);\n      timeout = null;\n      return invoke();\n    }\n    return result;\n  };\n  debounced.pending = (): boolean => timeout !== null;\n\n  return debounced;\n};\n"],"names":["getDebouncedFn","cb","wait","isImmediate","TypeError","Number","isFinite","timeout","lastArgs","lastContext","result","invoke","args","context","undefined","apply","debounced","executedFunction","this","later","callNow","clearTimeout","setTimeout","cancel","flush","pending"],"mappings":"yDAmCO,MAAMA,eAAiB,CAC5BC,GACAC,KAAe,IACfC,YAAuB,SAEvB,UAAWF,KAAO,WAChB,MAAM,IAAIG,UAAU,yCAEtB,UAAWF,OAAS,WAAaG,OAAOC,SAASJ,OAASA,KAAO,EAC/D,MAAM,IAAIE,UAAU,6DAEtB,UAAWD,cAAgB,UACzB,MAAM,IAAIC,UAAU,iDAGtB,IAAIG,QAAgD,KACpD,IAAIC,SAAiC,KACrC,IAAIC,YACJ,IAAIC,OACJ,MAAMC,OAAS,KACb,IAAKH,SACH,OAAOE,OAET,MAAME,KAAOJ,SACb,MAAMK,QAAUJ,YAChBD,SAAW,KACXC,iBAAcK,EACdJ,OAAST,GAAGc,MAAMF,QAASD,MAC3B,OAAOF,QAET,MAAMM,UAAY,SAASC,oBAEtBL,MAEHJ,SAAWI,KACXH,YAAcS,KACd,MAAMC,MAAQ,KACZZ,QAAU,KACV,IAAKJ,YACHQ,aACK,CACLH,SAAW,KACXC,iBAAcK,CAChB,GAEF,MAAMM,QAAUjB,cAAgBI,QAChC,GAAIA,QACFc,aAAad,SAEfA,QAAUe,WAAWH,MAAOjB,MAC5B,GAAIkB,QACF,OAAOT,SAET,OAAOD,MACT,EAEAM,UAAUO,OAAS,KACjB,GAAIhB,QACFc,aAAad,SAEfA,QAAU,KACVC,SAAW,KACXC,iBAAcK,GAEhBE,UAAUQ,MAAQ,KAChB,GAAIjB,QAAS,CACXc,aAAad,SACbA,QAAU,KACV,OAAOI,QACT,CACA,OAAOD,QAETM,UAAUS,QAAU,IAAelB,UAAY,KAE/C,OAAOS"}