{"version":3,"file":"useDebouncedState.cjs","sources":["../../hooks/useDebouncedState.tsx"],"sourcesContent":["import {\n  SetStateAction,\n  useCallback,\n  useEffect,\n  useRef,\n  useState\n} from 'react';\n\nexport interface UseDebouncedStateOptions {\n  /**\n   * If `true`, the state will be updated immediately on the first call (leading edge).\n   * Subsequent calls within the delay period will be debounced.\n   * @default false\n   */\n  leading?: boolean;\n}\n\nexport type UseDebouncedStateReturnValue<T> = [\n  T,\n  (newValue: SetStateAction<T>) => void\n];\n\n/**\n * A hook that debounces the state update.\n * @param defaultValue - The default value of the state.\n * @param delay - The delay time in milliseconds.\n * @param options - The options for the hook.\n * @returns A tuple containing the current value and the debounced set value function.\n *\n * @example\n * const [value, setValue] = useDebouncedState('Hello', 1000);\n\n * @example\n * const [value, setValue] = useDebouncedState('Hello', 1000, { leading: true });\n */\nexport function useDebouncedState<T = unknown>(\n  defaultValue: T,\n  delay: number,\n  options: UseDebouncedStateOptions = { leading: false }\n): UseDebouncedStateReturnValue<T> {\n  const [value, setValue] = useState(defaultValue);\n  const timeoutRef = useRef<number | null>(null);\n  const leadingRef = useRef(true);\n\n  const clearTimeout = useCallback(\n    () => window.clearTimeout(timeoutRef.current!),\n    []\n  );\n  useEffect(() => clearTimeout, [clearTimeout]);\n\n  const debouncedSetValue = useCallback(\n    (newValue: SetStateAction<T>) => {\n      clearTimeout();\n      if (leadingRef.current && options.leading) {\n        setValue(newValue);\n      } else {\n        timeoutRef.current = window.setTimeout(() => {\n          leadingRef.current = true;\n          setValue(newValue);\n        }, delay);\n      }\n      leadingRef.current = false;\n    },\n    [options.leading, clearTimeout, delay]\n  );\n\n  return [value, debouncedSetValue] as const;\n}\n"],"names":["useState","useRef","useCallback","useEffect"],"mappings":";;;;AAsBA;;;;;;;;;;;;AAYG;AACa,SAAA,iBAAiB,CAC/B,YAAe,EACf,KAAa,EACb,OAAA,GAAoC,EAAE,OAAO,EAAE,KAAK,EAAE,EAAA;IAEtD,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAGA,cAAQ,CAAC,YAAY,CAAC,CAAC;AACjD,IAAA,MAAM,UAAU,GAAGC,YAAM,CAAgB,IAAI,CAAC,CAAC;AAC/C,IAAA,MAAM,UAAU,GAAGA,YAAM,CAAC,IAAI,CAAC,CAAC;AAEhC,IAAA,MAAM,YAAY,GAAGC,iBAAW,CAC9B,MAAM,MAAM,CAAC,YAAY,CAAC,UAAU,CAAC,OAAQ,CAAC,EAC9C,EAAE,CACH,CAAC;IACFC,eAAS,CAAC,MAAM,YAAY,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC;AAE9C,IAAA,MAAM,iBAAiB,GAAGD,iBAAW,CACnC,CAAC,QAA2B,KAAI;AAC9B,QAAA,YAAY,EAAE,CAAC;QACf,IAAI,UAAU,CAAC,OAAO,IAAI,OAAO,CAAC,OAAO,EAAE;YACzC,QAAQ,CAAC,QAAQ,CAAC,CAAC;SACpB;aAAM;YACL,UAAU,CAAC,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,MAAK;AAC1C,gBAAA,UAAU,CAAC,OAAO,GAAG,IAAI,CAAC;gBAC1B,QAAQ,CAAC,QAAQ,CAAC,CAAC;aACpB,EAAE,KAAK,CAAC,CAAC;SACX;AACD,QAAA,UAAU,CAAC,OAAO,GAAG,KAAK,CAAC;KAC5B,EACD,CAAC,OAAO,CAAC,OAAO,EAAE,YAAY,EAAE,KAAK,CAAC,CACvC,CAAC;AAEF,IAAA,OAAO,CAAC,KAAK,EAAE,iBAAiB,CAAU,CAAC;AAC7C;;;;"}