// utils/mathjax.ts
declare global {
    interface Window {
      MathJax: any;
    }
  }
  
  export const loadMathJax = (): Promise<void> => {
    return new Promise((resolve, reject) => {
      if (window.MathJax) {
        resolve();
        return;
      }
  
      const script = document.createElement('script');
      script.type = 'text/javascript';
      script.src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML';
      script.async = true;
  
      script.onload = () => {
        window.MathJax.Hub.Config({
          tex2jax: {
            inlineMath: [['$', '$'], ['\\(', '\\)']],
            processEscapes: true,
          },
        });
        window.MathJax.Hub.Queue(['Typeset', window.MathJax.Hub]);
        resolve();
      };
  
      script.onerror = () => {
        reject(new Error('Failed to load MathJax'));
      };
  
      document.head.appendChild(script);
    });
  };
  