{"version":3,"file":"initGT.server.mjs","names":[],"sources":["../../src/setup/initGT.server.ts"],"sourcesContent":["// @ts-expect-error: resolved by Next aliases or gt-next package exports.\nimport * as getLocaleModule from 'gt-next/internal/_getLocale';\n// @ts-expect-error: resolved by Next aliases or gt-next package exports.\nimport * as getRegionModule from 'gt-next/internal/_getRegion';\nimport { getParams } from './shared';\nimport type { NextSetupI18nConfigParams } from './shared';\nimport type { NextI18nCacheParams } from '../i18n-cache/NextI18nCache';\nimport { initializeGT as coreInitializeGT } from './initGT';\nimport {\n  AsyncConditionStore,\n  type AsyncConditionStoreParams,\n} from '../condition-store/AsyncConditionStore';\nimport { setAsyncConditionStore } from '../condition-store/AsyncConditionStore';\nimport {\n  customGetLocaleUnresolvedWarning,\n  customGetRegionUnresolvedWarning,\n} from '../errors/createErrors';\n\n/**\n * Initialize GT for Next.js\n *\n * Something to note is that even though we initialize the\n * AsyncConditionStore on the server SSR logic is actually\n * bound to the ReadonlyConditionStore from ServerGTProvider.\n *\n * While technically, this could risk a divergence in locale state,\n * the ReadonlyConditionStore is tied to the getLocale() function\n * in the RSC GTProvider, so ReadonlyConditionStore always reads from\n * the AsyncConditionStore.\n */\nexport function initializeGTServer(\n  {\n    i18nConfigParams,\n    nextI18nCacheParams,\n  }: {\n    i18nConfigParams: NextSetupI18nConfigParams;\n    nextI18nCacheParams: NextI18nCacheParams;\n  } = getParams()\n): void {\n  coreInitializeGT({\n    i18nConfigParams,\n    nextI18nCacheParams,\n  });\n\n  const asyncConditionStoreParams = getAsyncConditionStoreParams();\n\n  // Note that this gets used by RSC, but SSR (aka 'use client' bondary on server)\n  // uses context to access this condition store\n  const conditionStore = new AsyncConditionStore(asyncConditionStoreParams);\n  setAsyncConditionStore(conditionStore);\n}\n\nfunction getAsyncConditionStoreParams(): AsyncConditionStoreParams {\n  // TODO: we are parsing this twice, address in separate PR\n  const privateConfig = JSON.parse(\n    process.env._GENERALTRANSLATION_I18N_CONFIG_PARAMS || '{}'\n  );\n  return {\n    headerName: privateConfig.headersAndCookies?.localeHeaderName,\n    cookieName: privateConfig.headersAndCookies?.localeCookieName,\n    ignorePreferredLanguages: privateConfig.ignoreBrowserLocales,\n    getLocale: resolveGetLocale(),\n    getRegion: resolveGetRegion(),\n  };\n}\n\nfunction resolveGetLocale(): (() => Promise<string>) | undefined {\n  const isCustomGetLocaleEnabled =\n    process.env._GENERALTRANSLATION_CUSTOM_GET_LOCALE_ENABLED === 'true';\n  if (!isCustomGetLocaleEnabled) return undefined;\n  const module: unknown = getLocaleModule;\n\n  if (typeof module === 'function') {\n    return module as () => Promise<string>;\n  } else if (typeof module === 'object' && module !== null) {\n    if ('default' in module && typeof module.default === 'function') {\n      return module.default as () => Promise<string>;\n    } else if (\n      'getLocale' in module &&\n      typeof module.getLocale === 'function'\n    ) {\n      return module.getLocale as () => Promise<string>;\n    }\n  }\n  console.warn(customGetLocaleUnresolvedWarning);\n}\n\nfunction resolveGetRegion(): (() => Promise<string | undefined>) | undefined {\n  const isCustomGetRegionEnabled =\n    process.env._GENERALTRANSLATION_CUSTOM_GET_REGION_ENABLED === 'true';\n  if (!isCustomGetRegionEnabled) return undefined;\n  const module: unknown = getRegionModule;\n\n  if (typeof module === 'function') {\n    return module as () => Promise<string | undefined>;\n  } else if (typeof module === 'object' && module !== null) {\n    if ('default' in module && typeof module.default === 'function') {\n      return module.default as () => Promise<string | undefined>;\n    } else if (\n      'getRegion' in module &&\n      typeof module.getRegion === 'function'\n    ) {\n      return module.getRegion as () => Promise<string | undefined>;\n    }\n  }\n  console.warn(customGetRegionUnresolvedWarning);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA8BA,SAAgB,mBACd,EACE,kBACA,wBAIE,WAAW,EACT;AACN,cAAiB;EACf;EACA;EACD,CAAC;AAOF,wBAAuB,IADI,oBAJO,8BAIsC,CACnC,CAAC;;AAGxC,SAAS,+BAA0D;CAEjE,MAAM,gBAAgB,KAAK,MACzB,QAAQ,IAAI,0CAA0C,KACvD;AACD,QAAO;EACL,YAAY,cAAc,mBAAmB;EAC7C,YAAY,cAAc,mBAAmB;EAC7C,0BAA0B,cAAc;EACxC,WAAW,kBAAkB;EAC7B,WAAW,kBAAkB;EAC9B;;AAGH,SAAS,mBAAwD;AAG/D,KAAI,EADF,QAAQ,IAAI,kDAAkD,QACjC,QAAO,KAAA;CACtC,MAAM,SAAkB;AAExB,KAAI,OAAO,WAAW,WACpB,QAAO;UACE,OAAO,WAAW,YAAY,WAAW;MAC9C,aAAa,UAAU,OAAO,OAAO,YAAY,WACnD,QAAO,OAAO;WAEd,eAAe,UACf,OAAO,OAAO,cAAc,WAE5B,QAAO,OAAO;;AAGlB,SAAQ,KAAK,iCAAiC;;AAGhD,SAAS,mBAAoE;AAG3E,KAAI,EADF,QAAQ,IAAI,kDAAkD,QACjC,QAAO,KAAA;CACtC,MAAM,SAAkB;AAExB,KAAI,OAAO,WAAW,WACpB,QAAO;UACE,OAAO,WAAW,YAAY,WAAW;MAC9C,aAAa,UAAU,OAAO,OAAO,YAAY,WACnD,QAAO,OAAO;WAEd,eAAe,UACf,OAAO,OAAO,cAAc,WAE5B,QAAO,OAAO;;AAGlB,SAAQ,KAAK,iCAAiC"}