export function init(that) {
    function exec() {
        loadMathjax('mml-chtml', null, true);
    }
    if (that) {
        that.$nextTick(()=>{
            exec();
            console.log('MathJax init nextTick');
        });
    } else {
        exec();
        console.log('MathJax init');
    }

}

export function loadMathjax(shortSrc, callBack, force) {
    if (force || !window.MathJax) {
        window.MathJax = {
            tex: {
                inlineMath: [['$', '$'], ['\\(', '\\)']]
            },
            svg: {
                fontCache: 'global'
            },
            options: {
                skipHtmlTags: [            //  HTML tags that won't be searched for math
                    'script', 'noscript', 'style', 'textarea', 'pre',
                    'code', 'annotation', 'annotation-xml', 'div'
                ],
            },
            startup: {
                ready: () => {
                    // console.log('MathJax is loaded, but not yet initialized');
                    MathJax.startup.defaultReady();
                    // console.log('MathJax is initialized, and the initial typeset is queued');
                    // console.log(MathJax);
                    if (callBack) {
                        callBack();
                    }
                }
            }
        };

        var script = document.createElement('script');
        script.src = '/external/mathjax/' + shortSrc + '.js';
        script.async = true;
        document.head.appendChild(script);

    } else {
        if (callBack) {
            callBack();
        }
    }


}

export function latex2svg(latex, callback) {
    function MathJaxStartupDefaultReady() {
        // console.log(latex, MathJax);
        let ret = window.MathJax.tex2svg(latex);
        callback(ret);
    }
    this.loadMathjax('tex-svg-full', MathJaxStartupDefaultReady);
}



