import { SuperBlocks } from '@freecodecamp/shared/config/curriculum';
import { scriptLoader } from './script-loaders';

export const mathJaxSrc =
  'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-AMS_HTML';

export const isMathJaxAllowed = (pathname: string) =>
  superBlocksWithMathJax.some(superBlock => pathname.includes(superBlock));

const superBlocksWithMathJax = [
  SuperBlocks.JsAlgoDataStructNew,
  SuperBlocks.JsAlgoDataStruct,
  SuperBlocks.ProjectEuler,
  SuperBlocks.RosettaCode,
  SuperBlocks.SciCompPy,
  SuperBlocks.PythonV9,
  SuperBlocks.IntroductionToPrecalculus
];

const configure = () => {
  if (!global.MathJax) return;
  const MathJax = global.MathJax;
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [
        ['$', '$'],
        ['\\(', '\\)']
      ],
      processEscapes: true,
      processClass: 'mathjax-support'
    }
  });
  document.querySelectorAll('.mathjax-support').forEach(el => {
    MathJax.Hub.Queue(['Typeset', MathJax.Hub, el]);
  });
};

export const initializeMathJax = (mathJaxChallenge = true) => {
  const mathJaxMountPoint = document.querySelector('#mathjax');
  if (global.MathJax) {
    // Configure MathJax when it's loaded and
    // users navigate from another challenge
    configure();
  } else if (!mathJaxMountPoint && mathJaxChallenge) {
    mathJaxScriptLoader();
  }
};

const mathJaxScriptLoader = () => {
  scriptLoader('mathjax', false, mathJaxSrc, configure, '');
};
