{"version":3,"file":"useAnnounceSearchResultCount.mjs","names":[],"sources":["../../../../../src/components/Search/hooks/useAnnounceSearchResultCount.ts"],"sourcesContent":["import { useRef } from 'react';\nimport type { RefObject } from 'react';\nimport type { SearchSource } from 'stream-chat';\n\nimport { useInteractionAnnouncements } from '../../Accessibility';\nimport { useMutationObserver } from '../../UtilityComponents/hooks/useMutationObserver';\n\nconst OPTION_SELECTOR = '[role=\"option\"]';\n\n/**\n * Announces how many `[role=\"option\"]` elements are listed in `containerRef`, whatever the result\n * source. Once every active source is fully loaded, the \"all results loaded\" status joins the same\n * announcement so that it cannot supersede the count.\n *\n * The announcement goes through the debounced `search.resultCount` interaction and is made only\n * when the count or the loaded status changes, so an in-place update of a result row is not\n * repeated. Becoming inactive cancels a pending announcement.\n *\n * @param containerRef - Element that contains the result options.\n * @param active - Whether to observe and announce, e.g. only while a search query is present.\n * @param activeSources - Sources whose pagination state decides whether all results are loaded.\n */\nexport const useAnnounceSearchResultCount = (\n  containerRef: RefObject<HTMLElement | null>,\n  active: boolean,\n  activeSources: SearchSource[] = [],\n) => {\n  const { announceInteraction, cancelInteraction } = useInteractionAnnouncements();\n  const lastSignatureRef = useRef<string | null>(null);\n\n  useMutationObserver(\n    containerRef,\n    () => {\n      if (!active) return;\n      const count = containerRef.current?.querySelectorAll(OPTION_SELECTOR).length ?? 0;\n      const allResultsLoaded =\n        activeSources.length > 0 &&\n        activeSources.every((source) => {\n          const { hasNext, isLoading } = source.state.getLatestValue();\n          return !hasNext && !isLoading;\n        });\n\n      // Skip mutations that change neither value, such as a result row re-rendering in place.\n      const signature = `${count}|${allResultsLoaded}`;\n      if (signature === lastSignatureRef.current) return;\n      lastSignatureRef.current = signature;\n\n      announceInteraction('search.resultCount', { allResultsLoaded, count });\n    },\n    {\n      enabled: active,\n      onDisconnect: () => {\n        lastSignatureRef.current = null;\n        cancelInteraction('search.resultCount');\n      },\n    },\n  );\n};\n"],"mappings":";;;;AAOA,IAAM,kBAAkB;;;;;;;;;;;;;;AAexB,IAAa,gCACX,cACA,QACA,gBAAgC,CAAC,MAC9B;CACH,MAAM,EAAE,qBAAqB,sBAAsB,4BAA4B;CAC/E,MAAM,mBAAmB,OAAsB,IAAI;CAEnD,oBACE,oBACM;EACJ,IAAI,CAAC,QAAQ;EACb,MAAM,QAAQ,aAAa,SAAS,iBAAiB,eAAe,CAAC,CAAC,UAAU;EAChF,MAAM,mBACJ,cAAc,SAAS,KACvB,cAAc,OAAO,WAAW;GAC9B,MAAM,EAAE,SAAS,cAAc,OAAO,MAAM,eAAe;GAC3D,OAAO,CAAC,WAAW,CAAC;EACtB,CAAC;EAGH,MAAM,YAAY,GAAG,MAAM,GAAG;EAC9B,IAAI,cAAc,iBAAiB,SAAS;EAC5C,iBAAiB,UAAU;EAE3B,oBAAoB,sBAAsB;GAAE;GAAkB;EAAM,CAAC;CACvE,GACA;EACE,SAAS;EACT,oBAAoB;GAClB,iBAAiB,UAAU;GAC3B,kBAAkB,oBAAoB;EACxC;CACF,CACF;AACF"}