module.exports = {
    /* For now: default to cdn. */
    load(srcPath = 'https://as.alipayobjects.com/g/cicada/monaco-editor-mirror/0.6.1/min', callback) {
        if (window.monaco) {
            callback();
            return;
        }
        const config = {
            baseUrl:srcPath,
            'vs/nls' : {
                availableLanguages: {
                    '*': 'zh-cn'
                }
            }
            // paths: {
            //     vs: srcPath + '/vs'
            // }
        };
        const loaderUrl = `${srcPath}/vs/loader.js`;
        const onGotAmdLoader = () => {

            // if (window.LOADER_PENDING) {
            //     window.require.config(config);
            // }
            const loader = global.require(loaderUrl);
            loader.require.config(config);

            // Load monaco
            loader.require([`vs/editor/editor.main`], () => {
                callback();
            });
            self.module = undefined;
            // workaround monaco-typescript not understanding the environment
            self.process.browser = true;

            // Call the delayed callbacks when AMD loader has been loaded
            if (window.LOADER_PENDING) {
                window.LOADER_PENDING = false;
                const loaderCallbacks = window.LOADER_CALLBACKS;
                if (loaderCallbacks && loaderCallbacks.length) {
                    let currentCallback = loaderCallbacks.shift();
                    while (currentCallback) {
                        currentCallback.fn.call(currentCallback.window);
                        currentCallback = loaderCallbacks.shift();
                    }
                }
            }
        };

        // Load AMD loader if necessary
        if (window.LOADER_PENDING) {
            // We need to avoid loading multiple loader.js when there are multiple editors loading concurrently
            //  delay to call callbacks except the first one
            window.LOADER_CALLBACKS = window.LOADER_CALLBACKS || [];
            window.LOADER_CALLBACKS.push({
                window: this,
                fn: onGotAmdLoader
            });
        } else {
            if (typeof window.require === 'undefined') {
                const loaderScript = window.document.createElement('script');
                loaderScript.type = 'text/javascript';
                loaderScript.src = loaderUrl;
                loaderScript.addEventListener('load', onGotAmdLoader);
                window.document.body.appendChild(loaderScript);
                window.LOADER_PENDING = true;
            } else {
                onGotAmdLoader();
            }
        }
    }
}
