{"ast":null,"code":"var __jsx = React.createElement;\n\nfunction _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }\n\nfunction _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }\n\nfunction _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { I18nextProvider, withSSR } from 'react-i18next';\nimport { isServer, lngFromReq, lngPathCorrector, lngsToLoad } from '../utils';\nimport { NextStaticProvider } from '../components';\nimport React from 'react';\nimport hoistNonReactStatics from 'hoist-non-react-statics';\nimport { withRouter } from 'next/router';\nexport const appWithTranslation = function (WrappedComponent) {\n  const WrappedComponentWithSSR = withSSR()(WrappedComponent);\n  const {\n    config,\n    i18n\n  } = this;\n  const consoleMessage = this.consoleMessage.bind(this);\n\n  const clientLoadNamespaces = (lng, namespaces) => Promise.all(namespaces.filter(ns => !i18n.hasResourceBundle(lng, ns)).map(ns => i18n.reloadResources(lng, ns)));\n\n  class AppWithTranslation extends React.Component {\n    constructor(props) {\n      super(props);\n\n      if (!isServer()) {\n        const changeLanguageCallback = (prevLng, newLng) => {\n          const {\n            router\n          } = props;\n          const {\n            pathname,\n            asPath,\n            query\n          } = router;\n          const routeInfo = {\n            pathname,\n            query\n          };\n\n          if (i18n.initializedLanguageOnce && typeof newLng === 'string' && prevLng !== newLng) {\n            const {\n              as,\n              href\n            } = lngPathCorrector(config, {\n              as: asPath,\n              href: routeInfo\n            }, newLng);\n            router.replace(href, as, {\n              shallow: config.shallowRender\n            });\n          }\n        };\n\n        const changeLanguage = i18n.changeLanguage.bind(i18n);\n\n        i18n.changeLanguage = async (newLng, callback = () => null) => {\n          const prevLng = i18n.language;\n\n          if (typeof newLng === 'string' && i18n.initializedLanguageOnce === true) {\n            const usedNamespaces = Object.entries(i18n.reportNamespaces.usedNamespaces).filter(x => x[1] === true).map(x => x[0]);\n            await clientLoadNamespaces(newLng, usedNamespaces);\n          }\n\n          return changeLanguage(newLng, () => {\n            changeLanguageCallback(prevLng, newLng);\n            callback(null, i18n.t);\n          });\n        };\n      }\n    }\n\n    static async getInitialProps(ctx) {\n      let wrappedComponentProps = {\n        pageProps: {}\n      };\n\n      if (WrappedComponent.getInitialProps) {\n        wrappedComponentProps = await WrappedComponent.getInitialProps(ctx);\n      }\n\n      if (typeof wrappedComponentProps.pageProps === 'undefined') {\n        consoleMessage('error', 'If you have a getInitialProps method in your custom _app.js file, you must explicitly return pageProps. For more information, see: https://github.com/zeit/next.js#custom-app');\n      }\n      /*\n          Initiate vars to return\n      */\n\n\n      const req = ctx.ctx.req;\n      let initialI18nStore = {};\n      let initialLanguage = null;\n      let i18nServerInstance = null;\n\n      if (req && !req.i18n) {\n        const {\n          router\n        } = ctx;\n        const result = router.asPath.match(/^\\/(.*?)\\//);\n        const lng = result ? result[1] : process.env.DEFAULT_LANGUAGE;\n        req.i18n = i18n.cloneInstance({\n          initImmediate: false,\n          lng\n        });\n        const res = ctx.ctx.res;\n\n        const setContextLocale = lng => {\n          // SEE: i18n-express-middleware\n          req.language = req.locale = req.lng = lng;\n\n          if (res) {\n            res.locals = res.locals || {};\n            res.locals.language = lng;\n            res.locals.languageDir = i18n.dir(lng);\n          }\n        };\n\n        setContextLocale(lng);\n        i18n.on('languageChanged', setContextLocale);\n      }\n      /*\n          Step 1: Determine initial language\n      */\n\n\n      if (req && req.i18n) {\n        initialLanguage = lngFromReq(req);\n        /*\n            Perform a lang change in case we're not on the right lang\n        */\n\n        await req.i18n.changeLanguage(initialLanguage);\n      } else if (Array.isArray(i18n.languages) && i18n.languages.length > 0) {\n        initialLanguage = i18n.language;\n      }\n      /*\n          Step 2: Determine namespace dependencies\n      */\n\n\n      let namespacesRequired = config.ns;\n\n      if (Array.isArray(wrappedComponentProps.pageProps.namespacesRequired)) {\n        ({\n          namespacesRequired\n        } = wrappedComponentProps.pageProps);\n      } else {\n        consoleMessage('warn', `You have not declared a namespacesRequired array on your page-level component: ${ctx.Component.displayName || ctx.Component.name || 'Component'}. This will cause all namespaces to be sent down to the client, possibly negatively impacting the performance of your app. For more info, see: https://github.com/isaachinman/next-i18next#4-declaring-namespace-dependencies`);\n      }\n      /*\n          We must always send down the defaultNS, otherwise\n          the client will trigger a request for it and issue\n          the \"Did not expect server HTML to contain a <h1> in <div>\"\n          error\n      */\n\n\n      if (typeof config.defaultNS === 'string' && !namespacesRequired.includes(config.defaultNS)) {\n        namespacesRequired.push(config.defaultNS);\n      }\n      /*\n          Step 3: Perform data fetching, depending on environment\n      */\n\n\n      if (req && req.i18n) {\n        /*\n            Detect the languages to load based upon the fallbackLng configuration\n        */\n        const {\n          fallbackLng\n        } = config;\n        const languagesToLoad = lngsToLoad(initialLanguage, fallbackLng, config.otherLanguages);\n        /*\n            Initialize the store with the languagesToLoad and\n            necessary namespaces needed to render this specific tree\n        */\n\n        languagesToLoad.forEach(lng => {\n          initialI18nStore[lng] = {};\n          namespacesRequired.forEach(ns => {\n            initialI18nStore[lng][ns] = (req.i18n.services.resourceStore.data[lng] || {})[ns] || {};\n          });\n        });\n      } else if (Array.isArray(i18n.languages) && i18n.languages.length > 0) {\n        /*\n            Load newly-required translations if changing route client side\n        */\n        await clientLoadNamespaces(i18n.languages[0], namespacesRequired);\n        initialI18nStore = i18n.store.data;\n      }\n      /*\n          Step 4: Overwrite i18n.toJSON method to be able to serialize the instance\n      */\n\n\n      if (req && req.i18n) {\n        req.i18n.toJSON = () => null;\n\n        i18nServerInstance = req.i18n;\n      }\n      /*\n          `pageProps` will get serialized automatically by NextJs\n      */\n\n\n      return _objectSpread({\n        initialI18nStore,\n        initialLanguage,\n        i18nServerInstance\n      }, wrappedComponentProps);\n    }\n\n    render() {\n      const {\n        initialLanguage,\n        initialI18nStore,\n        i18nServerInstance\n      } = this.props;\n      return __jsx(I18nextProvider, {\n        i18n: i18nServerInstance || i18n\n      }, __jsx(NextStaticProvider, null, __jsx(WrappedComponentWithSSR, _extends({\n        initialLanguage: initialLanguage,\n        initialI18nStore: initialI18nStore\n      }, this.props))));\n    }\n\n  }\n\n  return hoistNonReactStatics(withRouter(AppWithTranslation), WrappedComponent, {\n    getInitialProps: true\n  });\n};","map":null,"metadata":{},"sourceType":"module"}