import { mapTranslations } from "../i18n/index.js";
/**
 * Factory to create MyApp.getInitialProps.
 * Follows the same pattern as `@mui/internal-core-docs/Document`'s `createGetInitialProps`.
 *
 * @param options.translationsContext - A webpack require.context pointing at translation JSON files.
 *   e.g. `require.context('docs/translations', false, /\.\/translations.*\.json$/)`
 */

export function createGetInitialProps(options) {
  async function getInitialPropsApp({
    ctx,
    Component
  }) {
    let pageProps = {};
    const translations = mapTranslations(options.translationsContext);
    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx);
    }
    return {
      pageProps: {
        userLanguage: ctx.query.userLanguage || 'en',
        translations,
        ...pageProps
      }
    };
  }
  return getInitialPropsApp;
}