{"version":3,"file":"context.mjs","names":["getI18nConfig"],"sources":["../../src/context/context.ts"],"sourcesContent":["import { Dictionary, Hash, Locale } from 'gt-i18n/internal/types';\nimport { Translation } from 'gt-i18n/types';\nimport { createDiagnosticMessage } from 'generaltranslation/internal';\nimport { createGlobalSingleton } from 'gt-i18n/internal';\nimport { createContext, useContext, type Context } from 'react';\nimport { I18nStore } from '../i18n-store/I18nStore';\nimport { getI18nConfig } from '../setup/i18nConfig';\nimport type {\n  OnMissingTranslation,\n  OnMissingDictionaryEntry,\n  OnMissingDictionaryObj,\n} from '../hooks/utils/missing-translation';\n\nexport type GTContextType = {\n  /**\n   * Source of truth for translations, streamed from server\n   * In SPA mode, these won't be accessible and translations\n   * can be accessed via useSyncExternalStore() directly\n   */\n  translationsSnapshot: Record<Locale, Record<Hash, Translation>>;\n  dictionariesSnapshot: Record<Locale, Dictionary>;\n  /**\n   * I18nStore allows us to sync state updates in ConditionStore and I18nCache\n   * with renders\n   */\n  i18nStore: I18nStore;\n  locale: string;\n  region: string | undefined;\n  enableI18n: boolean;\n  setLocale: (locale: string) => void;\n  setRegion: (region: string | undefined) => void;\n  setEnableI18n: (enabled: boolean) => void;\n  /**\n   * Custom override behavior on missing translations\n   * Used for server triggering tx hmr b/c no access to useEffect\n   */\n  onMissingTranslation?: OnMissingTranslation;\n  onMissingDictionaryEntry?: OnMissingDictionaryEntry;\n  onMissingDictionaryObj?: OnMissingDictionaryObj;\n};\n\nconst gtContextSingleton = createGlobalSingleton<\n  Context<GTContextType | undefined>\n>({\n  namespace: 'reactCore',\n  key: 'gtContext',\n  source: '@generaltranslation/react-core',\n  notInitialized: () =>\n    createDiagnosticMessage({\n      source: '@generaltranslation/react-core',\n      severity: 'Error',\n      whatHappened: 'Cannot read GTContext before it has been initialized',\n      why: 'the internal GTContext singleton is unavailable',\n      fix: 'Add a <GTProvider> at the root of your component tree.',\n    }),\n});\n\nexport function getGTContext(): Context<GTContextType | undefined> {\n  // Lazily create the React context once and share it across instances.\n  if (!gtContextSingleton.isInitialized()) {\n    gtContextSingleton.set(createContext<GTContextType | undefined>(undefined));\n  }\n  return gtContextSingleton.get();\n}\n\nexport function useGTContext(): GTContextType | undefined {\n  const context = useContext(getGTContext());\n  if (context || getI18nConfig().getRenderStrategy() === 'SPA') {\n    return context;\n  }\n  /**\n   * TODO: in a separate PR, we should figure out how to make this more of a forgiving system\n   */\n  throw new Error(createMissingGTProviderError());\n}\n\nfunction createMissingGTProviderError(): string {\n  return createDiagnosticMessage({\n    source: '@generaltranslation/react-core',\n    severity: 'Error',\n    whatHappened: 'GT runtime context could not be read',\n    why: 'GTContext was accessed outside of a <GTProvider>',\n    fix: 'Add a <GTProvider> at the root of your component tree.',\n  });\n}\n"],"mappings":"6OAyCA,MAAM,EAAqB,EAEzB,CACA,UAAW,YACX,IAAK,YACL,OAAQ,iCACR,mBACE,EAAwB,CACtB,OAAQ,iCACR,SAAU,QACV,aAAc,uDACd,IAAK,kDACL,IAAK,yDACN,CAAC,CACL,CAAC,CAEF,SAAgB,GAAmD,CAKjE,OAHK,EAAmB,eAAe,EACrC,EAAmB,IAAI,EAAyC,IAAA,GAAU,CAAC,CAEtE,EAAmB,KAAK,CAGjC,SAAgB,GAA0C,CACxD,IAAM,EAAU,EAAW,GAAc,CAAC,CAC1C,GAAI,GAAWA,GAAe,CAAC,mBAAmB,GAAK,MACrD,OAAO,EAKT,MAAU,MAAM,GAA8B,CAAC,CAGjD,SAAS,GAAuC,CAC9C,OAAO,EAAwB,CAC7B,OAAQ,iCACR,SAAU,QACV,aAAc,uCACd,IAAK,mDACL,IAAK,yDACN,CAAC"}