import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { createElement } from 'react';
import { createRoot } from 'react-dom/client';
import { Editor, engineConfig, globalContext } from '@gant-lowcode/lowcode-editor-core';
import { registerDefaults } from '@gant-lowcode/lowcode-editor-skeleton';
import { Workspace as InnerWorkspace, Workbench as WorkSpaceWorkbench } from '@gant-lowcode/lowcode-workspace';
import { isPlainObject } from '@gant-lowcode/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 { coreModelFactory } from './modules/core-model-factory';
import { builtinHotkey } from './inner-plugins/builtin-hotkey';
import { defaultContextMenu } from './inner-plugins/default-context-menu';
import { CommandPlugin } from '@gant-lowcode/lowcode-plugin-command';
import { OutlinePlugin } from '@gant-lowcode/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:
          _context.next = 18;
          return plugins.register(CommandPlugin, {});
        case 18:
          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);
            plugins["delete"](CommandPlugin.pluginName);
          });
        case 19:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return _registryInnerPlugin.apply(this, arguments);
}
var innerWorkspace = new InnerWorkspace(registryInnerPlugin, coreModelFactory);
var _coreModelFactory$cre = coreModelFactory.createCore(innerWorkspace),
  designer = _coreModelFactory$cre.designer,
  editor = _coreModelFactory$cre.editor,
  engineContext = _coreModelFactory$cre.engineContext,
  config = _coreModelFactory$cre.config,
  plugins = _coreModelFactory$cre.plugins;
var _ref = engineContext,
  skeleton = _ref.skeleton,
  project = _ref.project,
  setters = _ref.setters,
  material = _ref.material,
  event = _ref.event,
  logger = _ref.logger,
  hotkey = _ref.hotkey,
  common = _ref.common,
  workspace = _ref.workspace,
  canvas = _ref.canvas,
  commonUI = _ref.commonUI,
  command = _ref.command,
  innerSkeleton = _ref.innerSkeleton,
  innerHotkey = _ref.innerHotkey;
globalContext.register(editor, Editor);
globalContext.register(editor, 'editor');
globalContext.register(innerWorkspace, 'workspace');
export { skeleton, plugins, project, setters, material, config, event, logger, hotkey, common, workspace, canvas, commonUI, command };
// 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;
var root;
// @ts-ignore webpack Define variable
export var version = VERSION_PLACEHOLDER || '1.0.0';
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);
          root = createRoot(engineContainer);
          Workbench = common.skeletonCabin.Workbench;
          if (!(options && options.enableWorkspaceMode)) {
            _context2.next = 20;
            break;
          }
          _context2.next = 10;
          return pluginPromise;
        case 10:
          disposeFun = _context2.sent;
          disposeFun && disposeFun();
          root.render(/*#__PURE__*/createElement(WorkSpaceWorkbench, {
            workspace: innerWorkspace,
            // skeleton: workspace.skeleton,
            className: 'engine-main',
            topAreaItemClassName: 'engine-actionitem'
          }));
          innerWorkspace.enableAutoOpenFirstWindow = engineConfig.get('enableAutoOpenFirstWindow', true);
          innerWorkspace.setActive(true);
          innerWorkspace.initWindow();
          innerHotkey.activate(false);
          _context2.next = 19;
          return innerWorkspace.plugins.init(pluginPreference);
        case 19:
          return _context2.abrupt("return");
        case 20:
          _context2.next = 22;
          return plugins.init(pluginPreference);
        case 22:
          root.render(/*#__PURE__*/createElement(Workbench, {
            skeleton: innerSkeleton,
            className: 'engine-main',
            topAreaItemClassName: 'engine-actionitem'
          }));
        case 23:
        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.
          root && root.unmount();
        case 3:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
  return _destroy.apply(this, arguments);
}