import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
/* eslint-disable max-len */
/* eslint-disable no-param-reassign */
import { createElement } from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import { globalContext, Editor, commonEvent, engineConfig, Setters as InnerSetters, Hotkey as InnerHotkey } from '@alilc/lowcode-editor-core';
import { IPublicEnumPluginRegisterLevel } from '@alilc/lowcode-types';
import { Designer, LowCodePluginManager } from '@alilc/lowcode-designer';
import { Skeleton as InnerSkeleton, registerDefaults } from '@alilc/lowcode-editor-skeleton';
import { Workspace as InnerWorkspace, Workbench as WorkSpaceWorkbench } from '@alilc/lowcode-workspace';
import { Hotkey, Project, Skeleton, Setters, Material, Event, Plugins, Common, Logger, Canvas, Workspace, Config, CommonUI } from '@alilc/lowcode-shell';
import { isPlainObject } from '@alilc/lowcode-utils';
import './modules/live-editing';
import * as classes from './modules/classes';
import symbols from './modules/symbols';
import { componentMetaParser } from './inner-plugins/component-meta-parser';
import { setterRegistry } from './inner-plugins/setter-registry';
import { defaultPanelRegistry } from './inner-plugins/default-panel-registry';
import { shellModelFactory } from './modules/shell-model-factory';
import { builtinHotkey } from './inner-plugins/builtin-hotkey';
import { defaultContextMenu } from './inner-plugins/default-context-menu';
import { OutlinePlugin } from '@alilc/lowcode-plugin-outline-pane';
export * from './modules/skeleton-types';
export * from './modules/designer-types';
export * from './modules/lowcode-types';
function registryInnerPlugin(_x, _x2, _x3) {
  return _registryInnerPlugin.apply(this, arguments);
}
function _registryInnerPlugin() {
  _registryInnerPlugin = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(designer, editor, plugins) {
    var componentMetaParserPlugin, defaultPanelRegistryPlugin;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          // 注册一批内置插件
          componentMetaParserPlugin = componentMetaParser(designer);
          defaultPanelRegistryPlugin = defaultPanelRegistry(editor);
          _context.next = 4;
          return plugins.register(OutlinePlugin, {}, {
            autoInit: true
          });
        case 4:
          _context.next = 6;
          return plugins.register(componentMetaParserPlugin);
        case 6:
          _context.next = 8;
          return plugins.register(setterRegistry, {});
        case 8:
          _context.next = 10;
          return plugins.register(defaultPanelRegistryPlugin);
        case 10:
          _context.next = 12;
          return plugins.register(builtinHotkey);
        case 12:
          _context.next = 14;
          return plugins.register(registerDefaults, {}, {
            autoInit: true
          });
        case 14:
          _context.next = 16;
          return plugins.register(defaultContextMenu);
        case 16:
          return _context.abrupt("return", function () {
            plugins["delete"](OutlinePlugin.pluginName);
            plugins["delete"](componentMetaParserPlugin.pluginName);
            plugins["delete"](setterRegistry.pluginName);
            plugins["delete"](defaultPanelRegistryPlugin.pluginName);
            plugins["delete"](builtinHotkey.pluginName);
            plugins["delete"](registerDefaults.pluginName);
            plugins["delete"](defaultContextMenu.pluginName);
          });
        case 17:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return _registryInnerPlugin.apply(this, arguments);
}
var innerWorkspace = new InnerWorkspace(registryInnerPlugin, shellModelFactory);
var workspace = new Workspace(innerWorkspace);
var editor = new Editor();
globalContext.register(editor, Editor);
globalContext.register(editor, 'editor');
globalContext.register(innerWorkspace, 'workspace');
var innerSkeleton = new InnerSkeleton(editor);
editor.set('skeleton', innerSkeleton);
var designer = new Designer({
  editor: editor,
  shellModelFactory: shellModelFactory
});
editor.set('designer', designer);
var innerProject = designer.project;
var innerHotkey = new InnerHotkey();
var hotkey = new Hotkey(innerHotkey);
var project = new Project(innerProject);
var skeleton = new Skeleton(innerSkeleton, 'any', false);
var innerSetters = new InnerSetters();
var setters = new Setters(innerSetters);
var material = new Material(editor);
var commonUI = new CommonUI(editor);
editor.set('project', project);
editor.set('setters', setters);
editor.set('material', material);
editor.set('innerHotkey', innerHotkey);
var config = new Config(engineConfig);
var event = new Event(commonEvent, {
  prefix: 'common'
});
var logger = new Logger({
  level: 'warn',
  bizName: 'common'
});
var common = new Common(editor, innerSkeleton);
var canvas = new Canvas(editor);
var plugins;
var pluginContextApiAssembler = {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  assembleApis: function assembleApis(context, pluginName, meta) {
    context.hotkey = hotkey;
    context.project = project;
    context.skeleton = new Skeleton(innerSkeleton, pluginName, false);
    context.setters = setters;
    context.material = material;
    var eventPrefix = (meta === null || meta === void 0 ? void 0 : meta.eventPrefix) || 'common';
    context.event = new Event(commonEvent, {
      prefix: eventPrefix
    });
    context.config = config;
    context.common = common;
    context.canvas = canvas;
    context.plugins = plugins;
    context.logger = new Logger({
      level: 'warn',
      bizName: "plugin:" + pluginName
    });
    context.workspace = workspace;
    context.commonUI = commonUI;
    context.registerLevel = IPublicEnumPluginRegisterLevel.Default;
    context.isPluginRegisteredInWorkspace = false;
    editor.set('pluginContext', context);
  }
};
var innerPlugins = new LowCodePluginManager(pluginContextApiAssembler);
plugins = new Plugins(innerPlugins).toProxy();
editor.set('innerPlugins', innerPlugins);
editor.set('plugins', plugins);
export { skeleton, plugins, project, setters, material, config, event, logger, hotkey, common, workspace, canvas, commonUI };
// declare this is open-source version
export var isOpenSource = true;
export var __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = {
  symbols: symbols,
  classes: classes
};
engineConfig.set('isOpenSource', isOpenSource);

// container which will host LowCodeEngine DOM
var engineContainer;
// @ts-ignore webpack Define variable
export var version = VERSION_PLACEHOLDER;
engineConfig.set('ENGINE_VERSION', version);
var pluginPromise = registryInnerPlugin(designer, editor, plugins);
export function init(_x4, _x5, _x6) {
  return _init.apply(this, arguments);
}
function _init() {
  _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(container, options, pluginPreference) {
    var engineOptions, Workbench, disposeFun;
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 2;
          return destroy();
        case 2:
          engineOptions = null;
          if (isPlainObject(container)) {
            engineOptions = container;
            engineContainer = document.createElement('div');
            engineContainer.id = 'engine';
            document.body.appendChild(engineContainer);
          } else {
            engineOptions = options;
            engineContainer = container;
            if (!container) {
              engineContainer = document.createElement('div');
              engineContainer.id = 'engine';
              document.body.appendChild(engineContainer);
            }
          }
          engineConfig.setEngineOptions(engineOptions);
          Workbench = common.skeletonCabin.Workbench;
          if (!(options && options.enableWorkspaceMode)) {
            _context2.next = 19;
            break;
          }
          _context2.next = 9;
          return pluginPromise;
        case 9:
          disposeFun = _context2.sent;
          disposeFun && disposeFun();
          render( /*#__PURE__*/createElement(WorkSpaceWorkbench, {
            workspace: innerWorkspace,
            // skeleton: workspace.skeleton,
            className: 'engine-main',
            topAreaItemClassName: 'engine-actionitem'
          }), engineContainer);
          innerWorkspace.enableAutoOpenFirstWindow = engineConfig.get('enableAutoOpenFirstWindow', true);
          innerWorkspace.setActive(true);
          innerWorkspace.initWindow();
          innerHotkey.activate(false);
          _context2.next = 18;
          return innerWorkspace.plugins.init(pluginPreference);
        case 18:
          return _context2.abrupt("return");
        case 19:
          _context2.next = 21;
          return plugins.init(pluginPreference);
        case 21:
          render( /*#__PURE__*/createElement(Workbench, {
            skeleton: innerSkeleton,
            className: 'engine-main',
            topAreaItemClassName: 'engine-actionitem'
          }), engineContainer);
        case 22:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
  return _init.apply(this, arguments);
}
export function destroy() {
  return _destroy.apply(this, arguments);
}
function _destroy() {
  _destroy = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
    var documents;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          // remove all documents
          documents = project.documents;
          if (Array.isArray(documents) && documents.length > 0) {
            documents.forEach(function (doc) {
              return project.removeDocument(doc);
            });
          }

          // TODO: delete plugins except for core plugins

          // unmount DOM container, this will trigger React componentWillUnmount lifeCycle,
          // so necessary cleanups will be done.
          engineContainer && unmountComponentAtNode(engineContainer);
        case 3:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
  return _destroy.apply(this, arguments);
}