import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { editor, plugins, project, material } from "@alilc/lowcode-engine";
import { getPageSchema } from '../utils';
import commonLogoPlugin from "../plugins/logo";
import '../plugins/register-plugins';
import '../setters/register-setters';
import './global.scss';
import './cover.scss';
var isNewEngineVersion = !!material;

function initPlugins(init, customPlugins, options) {
  var _ref = options || {},
      logo = _ref.logo;

  plugins.register(commonLogoPlugin, logo);

  if (customPlugins && Array.isArray(customPlugins) && customPlugins.length) {
    customPlugins.map(function (plugin) {
      plugins.register(plugin);
    });
  }

  if (init) {
    init.pluginName = 'editorInit';
    return plugins.register(init);
  } // 默认 init 插件


  plugins.register(function () {
    return {
      name: "editor-init",
      init: function init() {
        return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
          var assets, schema;
          return _regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  _context.next = 2;
                  return editor.utils.get('https://unpkg.alibaba-inc.com/@ali/ali-lowcode-materials/dist/assets.json');

                case 2:
                  assets = _context.sent;

                  if (isNewEngineVersion) {
                    material.setAssets(assets);
                  } else {
                    editor.setAssets(assets);
                  }

                  _context.t0 = getPageSchema();

                  if (_context.t0) {
                    _context.next = 9;
                    break;
                  }

                  _context.next = 8;
                  return editor.utils.get("./schema.json");

                case 8:
                  _context.t0 = _context.sent;

                case 9:
                  schema = _context.t0;

                  if (isNewEngineVersion) {
                    project.openDocument(schema);
                  } else {
                    project.open(schema);
                  }

                case 11:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee);
        }))();
      }
    };
  });
}

export default initPlugins;