{"version":3,"file":"useTrackedDictionaryResolver.mjs","names":[],"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":"kYAkBA,SAAgB,GAA+D,CAC7E,IAAM,EAAuB,GAAyB,CAChD,EAAY,GAAc,CAC1B,EACJ,QAAQ,IAAI,WAAa,cACzB,GAAe,CAAC,uBAAuB,CACnC,EAA2B,GAAiC,CAE5D,EAAiB,EAA2B,KAAK,CAavD,MAZA,CACE,EAAe,UAAU,IAAI,IAWxB,EACJ,GAA6B,CAE5B,IAAM,EAAY,EAAyB,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,EAjC0B,EAC5B,EACA,EAAU,iCACV,EA+BuB,CACtB,CACF"}