import { createRender } from '@mui/internal-markdown';
const notEnglishJsonRegExp = /-([a-z]{2})\.json$/;
/**
 * @param req - A webpack `require.context` for the API translation JSON files.
 * @param languagesIgnorePages - A function that returns true if the page should not be translated.
 *   Typically `LANGUAGES_IGNORE_PAGES` from the docs config.
 */
export default function mapApiPageTranslations(req, languagesIgnorePages) {
  const headingHashes = {};
  const translations = {};

  // Process the English markdown before the other locales.
  // English ToC anchor links are used in all languages
  let filenames = [];
  req.keys().forEach(filename => {
    if (filename.match(notEnglishJsonRegExp)) {
      filenames.push(filename);
    } else {
      filenames = [filename].concat(filenames);
    }
  });
  filenames.forEach(filename => {
    const matchNotEnglishMarkdown = filename.match(notEnglishJsonRegExp);
    const userLanguage = matchNotEnglishMarkdown !== null ? matchNotEnglishMarkdown[1] : 'en';
    const translation = req(filename) || null;
    if (translation !== null && translation.componentDescription) {
      const componentDescriptionToc = [];
      const render = createRender({
        headingHashes,
        toc: componentDescriptionToc,
        userLanguage,
        location: filenames,
        options: {
          ignoreLanguagePages: languagesIgnorePages || (() => false),
          env: {
            SOURCE_CODE_REPO: ''
          }
        }
      });
      translation.componentDescription = render(translation.componentDescription);
      translation.componentDescriptionToc = componentDescriptionToc;
    }
    translations[userLanguage] = translation;
  });
  return translations;
}