{"version":3,"file":"useDebouncedAnnounce.mjs","names":[],"sources":["../../../../../src/components/Accessibility/scheduling/useDebouncedAnnounce.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\nimport type { AriaLiveAnnounce, AriaLivePriority } from '../useAriaLiveAnnouncer';\n\nexport type DebouncedAnnounceResult = {\n  /**\n   * Schedules an announcement under `key`. Each call on a key resets that key's timer, so only the\n   * last call within its `delayMs` fires. Keys debounce independently, each with its own `delayMs`.\n   */\n  announce: (\n    key: string,\n    message: string,\n    delayMs: number,\n    priority?: AriaLivePriority,\n  ) => void;\n  /** Cancels the pending announcement of `key`, or all when omitted (also runs on unmount). */\n  cancel: (key?: string) => void;\n};\n\n/**\n * Per-key debounce over `announce`: each `key` has an independent timer and each call its own\n * delay, so streams with different windows do not clobber one another. Use a constant key for a\n * single stream. Pending timers are cleared on unmount.\n *\n * The caller pushes each fresh value; rapid calls on a key collapse to the last one, so a changing\n * value is announced once per pause. For one fixed message per state change, use\n * {@link useSettledAnnouncement}.\n */\nexport const useDebouncedAnnounce = (\n  announce: AriaLiveAnnounce,\n): DebouncedAnnounceResult => {\n  const timersRef = useRef<Map<string, ReturnType<typeof setTimeout>>>(new Map());\n\n  // Latest `announce` without changing the identity of the debounced callback.\n  const announceRef = useRef(announce);\n  announceRef.current = announce;\n\n  const cancel = useCallback((key?: string) => {\n    const timers = timersRef.current;\n    if (key === undefined) {\n      timers.forEach((timerId) => clearTimeout(timerId));\n      timers.clear();\n      return;\n    }\n    const timerId = timers.get(key);\n    if (timerId !== undefined) {\n      clearTimeout(timerId);\n      timers.delete(key);\n    }\n  }, []);\n\n  useEffect(() => () => cancel(), [cancel]);\n\n  const debouncedAnnounce = useCallback(\n    (key: string, message: string, delayMs: number, priority?: AriaLivePriority) => {\n      const timers = timersRef.current;\n      const existing = timers.get(key);\n      if (existing !== undefined) clearTimeout(existing);\n      const timerId = setTimeout(() => {\n        timers.delete(key);\n        announceRef.current(message, { priority });\n      }, delayMs);\n      timers.set(key, timerId);\n    },\n    [],\n  );\n\n  return { announce: debouncedAnnounce, cancel };\n};\n"],"mappings":";;;;;;;;;;;AA4BA,IAAa,wBACX,aAC4B;CAC5B,MAAM,YAAY,uBAAmD,IAAI,IAAI,CAAC;CAG9E,MAAM,cAAc,OAAO,QAAQ;CACnC,YAAY,UAAU;CAEtB,MAAM,SAAS,aAAa,QAAiB;EAC3C,MAAM,SAAS,UAAU;EACzB,IAAI,QAAQ,KAAA,GAAW;GACrB,OAAO,SAAS,YAAY,aAAa,OAAO,CAAC;GACjD,OAAO,MAAM;GACb;EACF;EACA,MAAM,UAAU,OAAO,IAAI,GAAG;EAC9B,IAAI,YAAY,KAAA,GAAW;GACzB,aAAa,OAAO;GACpB,OAAO,OAAO,GAAG;EACnB;CACF,GAAG,CAAC,CAAC;CAEL,sBAAsB,OAAO,GAAG,CAAC,MAAM,CAAC;CAgBxC,OAAO;EAAE,UAdiB,aACvB,KAAa,SAAiB,SAAiB,aAAgC;GAC9E,MAAM,SAAS,UAAU;GACzB,MAAM,WAAW,OAAO,IAAI,GAAG;GAC/B,IAAI,aAAa,KAAA,GAAW,aAAa,QAAQ;GACjD,MAAM,UAAU,iBAAiB;IAC/B,OAAO,OAAO,GAAG;IACjB,YAAY,QAAQ,SAAS,EAAE,SAAS,CAAC;GAC3C,GAAG,OAAO;GACV,OAAO,IAAI,KAAK,OAAO;EACzB,GACA,CAAC,CAGgB;EAAmB;CAAO;AAC/C"}