{"version":3,"file":"useSettledAnnouncement.mjs","names":[],"sources":["../../../../../src/components/Accessibility/scheduling/useSettledAnnouncement.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\n\nimport type { AriaLiveAnnounce, AriaLivePriority } from '../useAriaLiveAnnouncer';\n\nexport type UseSettledAnnouncementParams = {\n  /**\n   * While `true`, the announcement is (re)scheduled. Going back to `false` resets the announced\n   * state, so the message can fire again the next time `active` becomes `true`.\n   */\n  active: boolean;\n  /** The message to announce; the latest value is used when it fires. */\n  message: string;\n  /**\n   * A value whose identity changes while the surrounding UI is still in motion. Every change\n   * restarts the idle timer. It must be stable between changes: an immutably updated array or\n   * string works, a fresh object literal each render resets the timer forever.\n   */\n  settleKey: unknown;\n  /** Idle window (ms) `settleKey` must stay unchanged before announcing. Defaults to `1500`. */\n  idleMs?: number;\n  /**\n   * Announces at most once per `active` epoch. When `false`, it announces again each time it\n   * settles while still active. Defaults to `true`.\n   */\n  once?: boolean;\n  /** Live-region priority. Defaults to `'polite'`. */\n  priority?: AriaLivePriority;\n};\n\n/**\n * Announces a message once the surrounding UI has gone idle. An immediate live-region update can\n * be superseded by the screen reader's own speech (typing echo, field re-read), so each change of\n * `settleKey` restarts an idle timer and the message fires only after the user has paused.\n *\n * `active` is the condition that makes the announcement relevant. With the default `once`, it fires\n * once per `active` epoch and re-arms when `active` goes false. For a changing value announced\n * repeatedly, use {@link useDebouncedAnnounce}. The pending timer is cleared on unmount.\n */\nexport const useSettledAnnouncement = (\n  announce: AriaLiveAnnounce,\n  {\n    active,\n    idleMs = 1500,\n    message,\n    once = true,\n    priority = 'polite',\n    settleKey,\n  }: UseSettledAnnouncementParams,\n) => {\n  const announcedRef = useRef(false);\n  const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);\n\n  // Latest `announce` and `message` without resetting the idle timer; only `settleKey` does that.\n  const announceRef = useRef(announce);\n  announceRef.current = announce;\n  const messageRef = useRef(message);\n  messageRef.current = message;\n\n  useEffect(() => {\n    const clearPending = () => {\n      if (timeoutRef.current) {\n        clearTimeout(timeoutRef.current);\n        timeoutRef.current = null;\n      }\n    };\n\n    if (!active) {\n      announcedRef.current = false;\n      clearPending();\n      return;\n    }\n\n    if (once && announcedRef.current) return;\n\n    // Every `settleKey` change restarts the timer, so the message lands after activity stops.\n    clearPending();\n    timeoutRef.current = setTimeout(() => {\n      timeoutRef.current = null;\n      announcedRef.current = true;\n      announceRef.current(messageRef.current, { priority });\n    }, idleMs);\n  }, [active, idleMs, once, priority, settleKey]);\n\n  useEffect(\n    () => () => {\n      if (timeoutRef.current) clearTimeout(timeoutRef.current);\n    },\n    [],\n  );\n};\n"],"mappings":";;;;;;;;;;;AAsCA,IAAa,0BACX,UACA,EACE,QACA,SAAS,MACT,SACA,OAAO,MACP,WAAW,UACX,gBAEC;CACH,MAAM,eAAe,OAAO,KAAK;CACjC,MAAM,aAAa,OAA6C,IAAI;CAGpE,MAAM,cAAc,OAAO,QAAQ;CACnC,YAAY,UAAU;CACtB,MAAM,aAAa,OAAO,OAAO;CACjC,WAAW,UAAU;CAErB,gBAAgB;EACd,MAAM,qBAAqB;GACzB,IAAI,WAAW,SAAS;IACtB,aAAa,WAAW,OAAO;IAC/B,WAAW,UAAU;GACvB;EACF;EAEA,IAAI,CAAC,QAAQ;GACX,aAAa,UAAU;GACvB,aAAa;GACb;EACF;EAEA,IAAI,QAAQ,aAAa,SAAS;EAGlC,aAAa;EACb,WAAW,UAAU,iBAAiB;GACpC,WAAW,UAAU;GACrB,aAAa,UAAU;GACvB,YAAY,QAAQ,WAAW,SAAS,EAAE,SAAS,CAAC;EACtD,GAAG,MAAM;CACX,GAAG;EAAC;EAAQ;EAAQ;EAAM;EAAU;CAAS,CAAC;CAE9C,sBACc;EACV,IAAI,WAAW,SAAS,aAAa,WAAW,OAAO;CACzD,GACA,CAAC,CACH;AACF"}