{"version":3,"file":"useTrackedDictionaryResolver.cjs","names":["useDictionariesSnapshot","useI18nStore","useHandleMissingDictionaryEntry","useSubscribeToTrackedLookups","getDictionaryListenerKey"],"sources":["../../../src/hooks/external-store/useTrackedDictionaryResolver.ts"],"sourcesContent":["import { useCallback, useRef } from 'react';\nimport {\n  useDictionariesSnapshot,\n  useI18nStore,\n} from '../../i18n-store/useI18nStore';\nimport type {\n  DictionaryEntrySnapshot,\n  DictionaryLookup,\n} from '../../i18n-store/storeTypes';\nimport { getDictionaryListenerKey, getI18nConfig } from 'gt-i18n/internal';\nimport { useHandleMissingDictionaryEntry } from '../utils/missing-translation';\nimport { useSubscribeToTrackedLookups } from './useSubscribeToTrackedLookups';\n\nexport type TrackedDictionaryEntryResolver = (\n  lookup: DictionaryLookup\n) => DictionaryEntrySnapshot;\n\n// TODO: rename to useTrackedDictionaryEntryResolver\nexport function useTrackedDictionaryResolver(): TrackedDictionaryEntryResolver {\n  const dictionariesSnapshot = useDictionariesSnapshot();\n  const i18nStore = useI18nStore();\n  const devHotReloadEnabled =\n    process.env.NODE_ENV !== 'production' &&\n    getI18nConfig().isDevHotReloadEnabled();\n  const onMissingDictionaryEntry = useHandleMissingDictionaryEntry();\n\n  const trackedKeysRef = useRef<Set<string> | null>(null);\n  if (trackedKeysRef.current == null) {\n    trackedKeysRef.current = new Set();\n  }\n\n  // subscribe to dictionary entry updates\n  const trackedLookupsVersion = useSubscribeToTrackedLookups(\n    trackedKeysRef,\n    i18nStore.subscribeToDictionaryEntryEvents,\n    getDictionaryListenerKey\n  );\n\n  // Resolution callback\n  return useCallback(\n    (lookup: DictionaryLookup) => {\n      // Track the lookup for dev hot reload\n      const lookupKey = getDictionaryListenerKey(lookup);\n      if (devHotReloadEnabled) {\n        trackedKeysRef.current!.add(lookupKey);\n      }\n\n      // Resolve the dictionary entry from the store\n      const dictionaryEntry = i18nStore.getDictionaryEntrySnapshot(\n        lookup,\n        dictionariesSnapshot\n      );\n\n      // Hot reload\n      if (dictionaryEntry == null && devHotReloadEnabled) {\n        onMissingDictionaryEntry(lookup);\n      }\n\n      return dictionaryEntry;\n    },\n    // trackedLookupsVersion: change identity when a tracked entry arrives\n    [\n      i18nStore,\n      dictionariesSnapshot,\n      devHotReloadEnabled,\n      onMissingDictionaryEntry,\n      trackedLookupsVersion,\n    ]\n  );\n}\n"],"mappings":"wPAkBA,SAAgB,GAA+D,CAC7E,IAAM,EAAuBA,EAAAA,yBAAyB,CAChD,EAAYC,EAAAA,cAAc,CAC1B,EACJ,QAAQ,IAAI,WAAa,eAAA,EAAA,EAAA,gBACV,CAAC,uBAAuB,CACnC,EAA2BC,EAAAA,iCAAiC,CAE5D,GAAA,EAAA,EAAA,QAA4C,KAAK,CAavD,MAZA,CACE,EAAe,UAAU,IAAI,KAW/B,EAAA,EAAA,aACG,GAA6B,CAE5B,IAAM,GAAA,EAAA,EAAA,0BAAqC,EAAO,CAC9C,GACF,EAAe,QAAS,IAAI,EAAU,CAIxC,IAAM,EAAkB,EAAU,2BAChC,EACA,EACD,CAOD,OAJI,GAAmB,MAAQ,GAC7B,EAAyB,EAAO,CAG3B,GAGT,CACE,EACA,EACA,EACA,EAjC0BC,EAAAA,6BAC5B,EACA,EAAU,iCACVC,EAAAA,yBA+BuB,CACtB,CACF"}