"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
var _exportNames = {
  skeleton: true,
  plugins: true,
  project: true,
  setters: true,
  material: true,
  config: true,
  event: true,
  logger: true,
  hotkey: true,
  common: true,
  workspace: true,
  canvas: true,
  isOpenSource: true,
  __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: true,
  version: true,
  init: true,
  destroy: true
};
exports.config = exports.common = exports.canvas = exports.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = void 0;
exports.destroy = destroy;
exports.hotkey = exports.event = void 0;
exports.init = init;
exports.workspace = exports.version = exports.skeleton = exports.setters = exports.project = exports.plugins = exports.material = exports.logger = exports.isOpenSource = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = require("react");
var _reactDom = require("react-dom");
var _lowcodeEditorCore = require("@alilc/lowcode-editor-core");
var _lowcodeTypes = require("@alilc/lowcode-types");
var _lowcodeDesigner = require("@alilc/lowcode-designer");
var _lowcodeEditorSkeleton = require("@alilc/lowcode-editor-skeleton");
var _lowcodeWorkspace = require("@alilc/lowcode-workspace");
var _lowcodeShell = require("@alilc/lowcode-shell");
var _lowcodeUtils = require("@alilc/lowcode-utils");
require("./modules/live-editing");
var classes = _interopRequireWildcard(require("./modules/classes"));
var _symbols = _interopRequireDefault(require("./modules/symbols"));
var _componentMetaParser = require("./inner-plugins/component-meta-parser");
var _setterRegistry = require("./inner-plugins/setter-registry");
var _defaultPanelRegistry = require("./inner-plugins/default-panel-registry");
var _shellModelFactory = require("./modules/shell-model-factory");
var _builtinHotkey = require("./inner-plugins/builtin-hotkey");
var _lowcodePluginOutlinePane = require("@alilc/lowcode-plugin-outline-pane");
var _skeletonTypes = require("./modules/skeleton-types");
Object.keys(_skeletonTypes).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  if (key in exports && exports[key] === _skeletonTypes[key]) return;
  exports[key] = _skeletonTypes[key];
});
var _designerTypes = require("./modules/designer-types");
Object.keys(_designerTypes).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  if (key in exports && exports[key] === _designerTypes[key]) return;
  exports[key] = _designerTypes[key];
});
var _lowcodeTypes2 = require("./modules/lowcode-types");
Object.keys(_lowcodeTypes2).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  if (key in exports && exports[key] === _lowcodeTypes2[key]) return;
  exports[key] = _lowcodeTypes2[key];
});
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
/* eslint-disable max-len */
/* eslint-disable no-param-reassign */
function registryInnerPlugin(_x, _x2, _x3) {
  return _registryInnerPlugin.apply(this, arguments);
}
function _registryInnerPlugin() {
  _registryInnerPlugin = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(designer, editor, plugins) {
    var componentMetaParserPlugin, defaultPanelRegistryPlugin;
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          // 注册一批内置插件
          componentMetaParserPlugin = (0, _componentMetaParser.componentMetaParser)(designer);
          defaultPanelRegistryPlugin = (0, _defaultPanelRegistry.defaultPanelRegistry)(editor);
          _context.next = 4;
          return plugins.register(_lowcodePluginOutlinePane.OutlinePlugin, {}, {
            autoInit: true
          });
        case 4:
          _context.next = 6;
          return plugins.register(componentMetaParserPlugin);
        case 6:
          _context.next = 8;
          return plugins.register(_setterRegistry.setterRegistry, {});
        case 8:
          _context.next = 10;
          return plugins.register(defaultPanelRegistryPlugin);
        case 10:
          _context.next = 12;
          return plugins.register(_builtinHotkey.builtinHotkey);
        case 12:
          _context.next = 14;
          return plugins.register(_lowcodeEditorSkeleton.registerDefaults, {}, {
            autoInit: true
          });
        case 14:
          return _context.abrupt("return", function () {
            plugins["delete"](_lowcodePluginOutlinePane.OutlinePlugin.pluginName);
            plugins["delete"](componentMetaParserPlugin.pluginName);
            plugins["delete"](_setterRegistry.setterRegistry.pluginName);
            plugins["delete"](defaultPanelRegistryPlugin.pluginName);
            plugins["delete"](_builtinHotkey.builtinHotkey.pluginName);
            plugins["delete"](_lowcodeEditorSkeleton.registerDefaults.pluginName);
          });
        case 15:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return _registryInnerPlugin.apply(this, arguments);
}
var innerWorkspace = new _lowcodeWorkspace.Workspace(registryInnerPlugin, _shellModelFactory.shellModelFactory);
var workspace = exports.workspace = new _lowcodeShell.Workspace(innerWorkspace);
var editor = new _lowcodeEditorCore.Editor();
_lowcodeEditorCore.globalContext.register(editor, _lowcodeEditorCore.Editor);
_lowcodeEditorCore.globalContext.register(editor, 'editor');
_lowcodeEditorCore.globalContext.register(innerWorkspace, 'workspace');
var innerSkeleton = new _lowcodeEditorSkeleton.Skeleton(editor);
editor.set('skeleton', innerSkeleton);
var designer = new _lowcodeDesigner.Designer({
  editor: editor,
  shellModelFactory: _shellModelFactory.shellModelFactory
});
editor.set('designer', designer);
var innerProject = designer.project;
var innerHotkey = new _lowcodeEditorCore.Hotkey();
var hotkey = exports.hotkey = new _lowcodeShell.Hotkey(innerHotkey);
var project = exports.project = new _lowcodeShell.Project(innerProject);
var skeleton = exports.skeleton = new _lowcodeShell.Skeleton(innerSkeleton, 'any', false);
var innerSetters = new _lowcodeEditorCore.Setters();
var setters = exports.setters = new _lowcodeShell.Setters(innerSetters);
var material = exports.material = new _lowcodeShell.Material(editor);
editor.set('project', project);
editor.set('setters', setters);
editor.set('material', material);
editor.set('innerHotkey', innerHotkey);
var config = exports.config = new _lowcodeShell.Config(_lowcodeEditorCore.engineConfig);
var event = exports.event = new _lowcodeShell.Event(_lowcodeEditorCore.commonEvent, {
  prefix: 'common'
});
var logger = exports.logger = new _lowcodeShell.Logger({
  level: 'warn',
  bizName: 'common'
});
var common = exports.common = new _lowcodeShell.Common(editor, innerSkeleton);
var canvas = exports.canvas = new _lowcodeShell.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 _lowcodeShell.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 _lowcodeShell.Event(_lowcodeEditorCore.commonEvent, {
      prefix: eventPrefix
    });
    context.config = config;
    context.common = common;
    context.canvas = canvas;
    context.plugins = plugins;
    context.logger = new _lowcodeShell.Logger({
      level: 'warn',
      bizName: "plugin:" + pluginName
    });
    context.workspace = workspace;
    context.registerLevel = _lowcodeTypes.IPublicEnumPluginRegisterLevel.Default;
    context.isPluginRegisteredInWorkspace = false;
  }
};
var innerPlugins = new _lowcodeDesigner.LowCodePluginManager(pluginContextApiAssembler);
exports.plugins = plugins = new _lowcodeShell.Plugins(innerPlugins).toProxy();
editor.set('innerPlugins', innerPlugins);
editor.set('plugins', plugins);
// declare this is open-source version
var isOpenSource = exports.isOpenSource = true;
var __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = exports.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = {
  symbols: _symbols["default"],
  classes: classes
};
_lowcodeEditorCore.engineConfig.set('isOpenSource', isOpenSource);

// container which will host LowCodeEngine DOM
var engineContainer;
// @ts-ignore webpack Define variable
var version = exports.version = VERSION_PLACEHOLDER;
_lowcodeEditorCore.engineConfig.set('ENGINE_VERSION', version);
var pluginPromise = registryInnerPlugin(designer, editor, plugins);
function init(_x4, _x5, _x6) {
  return _init.apply(this, arguments);
}
function _init() {
  _init = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(container, options, pluginPreference) {
    var engineOptions, Workbench, disposeFun;
    return _regenerator["default"].wrap(function _callee2$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 2;
          return destroy();
        case 2:
          engineOptions = null;
          if ((0, _lowcodeUtils.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);
            }
          }
          _lowcodeEditorCore.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();
          (0, _reactDom.render)( /*#__PURE__*/(0, _react.createElement)(_lowcodeWorkspace.Workbench, {
            workspace: innerWorkspace,
            // skeleton: workspace.skeleton,
            className: 'engine-main',
            topAreaItemClassName: 'engine-actionitem'
          }), engineContainer);
          innerWorkspace.enableAutoOpenFirstWindow = _lowcodeEditorCore.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:
          (0, _reactDom.render)( /*#__PURE__*/(0, _react.createElement)(Workbench, {
            skeleton: innerSkeleton,
            className: 'engine-main',
            topAreaItemClassName: 'engine-actionitem'
          }), engineContainer);
        case 22:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
  return _init.apply(this, arguments);
}
function destroy() {
  return _destroy.apply(this, arguments);
}
function _destroy() {
  _destroy = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
    var documents;
    return _regenerator["default"].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 && (0, _reactDom.unmountComponentAtNode)(engineContainer);
        case 3:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
  return _destroy.apply(this, arguments);
}