{"version":3,"file":"appWithTranslation.mjs","names":["createClient"],"sources":["../../src/pagesRouter/appWithTranslation.tsx"],"sourcesContent":["import React, { useMemo, useRef } from 'react'\nimport hoistNonReactStatics from 'hoist-non-react-statics'\nimport { I18nextProvider } from 'react-i18next'\nimport type { AppProps as NextJsAppProps } from 'next/app'\n\nimport { createConfig } from './config/createConfig'\nimport createClient from './createClient/browser'\n\nimport { SSRConfig, UserConfig } from './types'\n\nimport { i18n as I18NextClient, Resource } from 'i18next'\nimport { useIsomorphicLayoutEffect } from './utils'\nexport {\n  Trans,\n  useTranslation,\n  withTranslation,\n} from 'react-i18next'\n\nexport let globalI18n: I18NextClient | null = null\n\nconst addResourcesToI18next = (instance: I18NextClient, resources: Resource) => {\n  if (resources && instance.isInitialized) {\n    for (const locale of Object.keys(resources)) {\n      for (const ns of Object.keys(resources[locale])) {\n        if (!instance?.store?.data || !instance.store.data[locale] || !instance.store.data[locale][ns]) {\n          instance.addResourceBundle(\n            locale,\n            ns,\n            resources[locale][ns],\n            true,\n            true\n          )\n        }\n      }\n    }\n  }\n}\n\nexport const appWithTranslation = <Props extends NextJsAppProps>(\n  WrappedComponent: React.ComponentType<Props>,\n  configOverride: UserConfig | null = null\n) => {\n  const AppWithTranslation = (\n    props: Props & { pageProps: Props['pageProps'] & SSRConfig }\n  ) => {\n    const { _nextI18Next } = props.pageProps || {} // pageProps may be undefined on strange setups, i.e. https://github.com/i18next/next-i18next/issues/2109\n    let locale: string | undefined =\n      _nextI18Next?.initialLocale ?? props?.router?.locale\n    const ns = _nextI18Next?.ns\n\n    const instanceRef = useRef<I18NextClient | null>(null)\n\n    /**\n     * Memoize i18n instance and reuse it rather than creating new instance.\n     * When the locale or resources are changed after instance was created,\n     * we will update the instance by calling addResourceBundle method on it.\n     */\n    const i18n: I18NextClient | null = useMemo(() => {\n      if (!_nextI18Next && !configOverride) return null\n\n      const userConfig = configOverride ?? _nextI18Next?.userConfig\n\n      if (!userConfig) {\n        throw new Error(\n          'appWithTranslation was called without a next-i18next config'\n        )\n      }\n\n      if (!userConfig?.i18n) {\n        throw new Error(\n          'appWithTranslation was called without config.i18n'\n        )\n      }\n\n      if (!userConfig?.i18n?.defaultLocale) {\n        throw new Error(\n          'config.i18n does not include a defaultLocale property'\n        )\n      }\n\n      const { initialI18nStore } = _nextI18Next || {}\n      const resources = configOverride?.resources ?? initialI18nStore\n\n      if (!locale) locale = userConfig.i18n.defaultLocale\n\n      let instance = instanceRef.current\n      if (instance) {\n        addResourcesToI18next(instance, resources)\n      } else {\n        instance = createClient({\n          ...createConfig({\n            ...userConfig,\n            lng: locale,\n          }),\n          lng: locale,\n          ...(ns && { ns }),\n          resources,\n        }).i18n\n\n        addResourcesToI18next(instance, resources)\n\n        globalI18n = instance\n        instanceRef.current = instance\n      }\n\n      return instance\n    }, [_nextI18Next, locale, ns])\n\n    /**\n     * Since calling changeLanguage method on existing i18n instance cause state update in react,\n     * we need to call the method in `useLayoutEffect` to prevent state update in render phase.\n     */\n    useIsomorphicLayoutEffect(() => {\n      if (!i18n || !locale) return\n      i18n.changeLanguage(locale)\n    }, [i18n, locale])\n\n    return i18n !== null\n      ? (\n        <I18nextProvider i18n={i18n}>\n          <WrappedComponent {...props} />\n        </I18nextProvider>\n        )\n      : (\n        <WrappedComponent key={locale} {...props} />\n        )\n  }\n\n  return hoistNonReactStatics(AppWithTranslation, WrappedComponent)\n}\n"],"mappings":";;;;;;;AAkBA,IAAW,aAAmC;AAE9C,MAAM,yBAAyB,UAAyB,cAAwB;AAC9E,KAAI,aAAa,SAAS;OACnB,MAAM,UAAU,OAAO,KAAK,UAAU,CACzC,MAAK,MAAM,MAAM,OAAO,KAAK,UAAU,QAAQ,CAC7C,KAAI,CAAC,UAAU,OAAO,QAAQ,CAAC,SAAS,MAAM,KAAK,WAAW,CAAC,SAAS,MAAM,KAAK,QAAQ,IACzF,UAAS,kBACP,QACA,IACA,UAAU,QAAQ,KAClB,MACA,KACD;;;AAOX,MAAa,sBACX,kBACA,iBAAoC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