import { insertStyle } from '../dom/dom.mjs';
import { loadJS } from '../loader/loader.mjs';
import { V_CONSOLE_URL, V_CONSOLE_STYLE_CONTENT } from './config.mjs';
import { initLoadDelayPlugin } from './plugin/delay.mjs';
import { initSwitchEnvPlugin } from './plugin/env.mjs';
import { initFeedbackPlugin } from './plugin/feedback.mjs';
import { initMsdkPlugin } from './plugin/msdk.mjs';
import { initVersionPlugin } from './plugin/version.mjs';
import { initVersionSimplePlugin } from './plugin/version-simple.mjs';
import '../loader/little-loader.mjs';
import '../clipboard/clipboard-web.mjs';
import '@babel/runtime/helpers/toConsumableArray';
import '../time/time.mjs';

/**
 * 加载 vConsole
 * @param {Object} [options = {}] vConsole 选项
 * @param {Array<string>} [plugins = []] 插件列表
 * @returns {Promise<Object>} vConsole 实例
 *
 * @example
 * ```ts
 * loadVConsole()
 * ```
 */
function loadVConsole() {
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var plugins = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  return new Promise(function (resolve) {
    if (typeof window.VConsole === 'undefined') {
      loadJS(V_CONSOLE_URL).then(function () {
        resolve(initVConsole(options, plugins));
      });
    } else {
      resolve(initVConsole(options, plugins));
    }
  });
}
function initVConsole(options, plugins) {
  var vConsole = new VConsole(Object.assign({}, options || {}));
  vConsole.addPlugin(initVersionPlugin());
  vConsole.addPlugin(initFeedbackPlugin());
  vConsole.addPlugin(initSwitchEnvPlugin());
  vConsole.addPlugin(initLoadDelayPlugin());
  vConsole.addPlugin(initVersionSimplePlugin());
  vConsole.addPlugin(initMsdkPlugin());
  plugins.forEach(function (plugin) {
    vConsole.addPlugin(plugin());
  });
  insertStyle({
    id: 'vConsolePluginStyle',
    content: V_CONSOLE_STYLE_CONTENT
  });
  if (window && !window.vConsole) {
    window.vConsole = vConsole;
  }
  return vConsole;
}

export { loadVConsole };
