{"version":3,"file":"useTrackedDictionaryObjResolver.mjs","names":[],"sources":["../../../src/hooks/external-store/useTrackedDictionaryObjResolver.ts"],"sourcesContent":["import { getDictionaryListenerKey, getI18nConfig } from 'gt-i18n/internal';\nimport type {\n  DictionaryLookup,\n  DictionaryObjectSnapshot,\n} from '../../i18n-store/storeTypes';\nimport {\n  useDictionariesSnapshot,\n  useI18nStore,\n} from '../../i18n-store/useI18nStore';\nimport { useCallback, useRef } from 'react';\nimport { useHandleMissingDictionaryObject } from '../utils/missing-translation';\nimport { useSubscribeToTrackedLookups } from './useSubscribeToTrackedLookups';\n\nexport type TrackedDictionaryObjResolver = (\n  lookup: DictionaryLookup\n) => DictionaryObjectSnapshot;\n\n// TODO: rename to useTrackedDictionaryObjectResolver\nexport function useTrackedDictionaryObjResolver(): TrackedDictionaryObjResolver {\n  const dictionariesSnapshot = useDictionariesSnapshot();\n  const i18nStore = useI18nStore();\n  const devHotReloadEnabled =\n    process.env.NODE_ENV !== 'production' &&\n    getI18nConfig().isDevHotReloadEnabled();\n  const onMissingDictionaryObj = useHandleMissingDictionaryObject();\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 object updates\n  const trackedLookupsVersion = useSubscribeToTrackedLookups(\n    trackedKeysRef,\n    i18nStore.subscribeToDictionaryObjectEvents,\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 dictionaryObject = i18nStore.getDictionaryObjectSnapshot(\n        lookup,\n        dictionariesSnapshot\n      );\n\n      // Hot reload\n      if (dictionaryObject == null && devHotReloadEnabled) {\n        onMissingDictionaryObj(lookup);\n      }\n\n      return dictionaryObject;\n    },\n    // trackedLookupsVersion: change identity when a tracked entry arrives\n    [\n      i18nStore,\n      dictionariesSnapshot,\n      devHotReloadEnabled,\n      onMissingDictionaryObj,\n      trackedLookupsVersion,\n    ]\n  );\n}\n"],"mappings":"mYAkBA,SAAgB,GAAgE,CAC9E,IAAM,EAAuB,GAAyB,CAChD,EAAY,GAAc,CAC1B,EACJ,QAAQ,IAAI,WAAa,cACzB,GAAe,CAAC,uBAAuB,CACnC,EAAyB,GAAkC,CAE3D,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,EAAmB,EAAU,4BACjC,EACA,EACD,CAOD,OAJI,GAAoB,MAAQ,GAC9B,EAAuB,EAAO,CAGzB,GAGT,CACE,EACA,EACA,EACA,EAjC0B,EAC5B,EACA,EAAU,kCACV,EA+BuB,CACtB,CACF"}