"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,
  commonUI: true,
  command: true,
  isOpenSource: true,
  __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: true,
  version: true,
  init: true,
  destroy: true
};
exports.config = exports.commonUI = exports.common = exports.command = 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 _client = require("react-dom/client");
var _lowcodeEditorCore = require("@gant-lowcode/lowcode-editor-core");
var _lowcodeEditorSkeleton = require("@gant-lowcode/lowcode-editor-skeleton");
var _lowcodeWorkspace = require("@gant-lowcode/lowcode-workspace");
var _lowcodeUtils = require("@gant-lowcode/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 _coreModelFactory = require("./modules/core-model-factory");
var _builtinHotkey = require("./inner-plugins/builtin-hotkey");
var _defaultContextMenu = require("./inner-plugins/default-context-menu");
var _lowcodePluginCommand = require("@gant-lowcode/lowcode-plugin-command");
var _lowcodePluginOutlinePane = require("@gant-lowcode/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 _lowcodeTypes = require("./modules/lowcode-types");
Object.keys(_lowcodeTypes).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  if (key in exports && exports[key] === _lowcodeTypes[key]) return;
  exports[key] = _lowcodeTypes[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 && {}.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; }
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:
          _context.next = 16;
          return plugins.register(_defaultContextMenu.defaultContextMenu);
        case 16:
          _context.next = 18;
          return plugins.register(_lowcodePluginCommand.CommandPlugin, {});
        case 18:
          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);
            plugins["delete"](_defaultContextMenu.defaultContextMenu.pluginName);
            plugins["delete"](_lowcodePluginCommand.CommandPlugin.pluginName);
          });
        case 19:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return _registryInnerPlugin.apply(this, arguments);
}
var innerWorkspace = new _lowcodeWorkspace.Workspace(registryInnerPlugin, _coreModelFactory.coreModelFactory);
var _coreModelFactory$cre = _coreModelFactory.coreModelFactory.createCore(innerWorkspace),
  designer = _coreModelFactory$cre.designer,
  editor = _coreModelFactory$cre.editor,
  engineContext = _coreModelFactory$cre.engineContext,
  config = exports.config = _coreModelFactory$cre.config,
  plugins = exports.plugins = _coreModelFactory$cre.plugins;
var _ref = engineContext,
  skeleton = exports.skeleton = _ref.skeleton,
  project = exports.project = _ref.project,
  setters = exports.setters = _ref.setters,
  material = exports.material = _ref.material,
  event = exports.event = _ref.event,
  logger = exports.logger = _ref.logger,
  hotkey = exports.hotkey = _ref.hotkey,
  common = exports.common = _ref.common,
  workspace = exports.workspace = _ref.workspace,
  canvas = exports.canvas = _ref.canvas,
  commonUI = exports.commonUI = _ref.commonUI,
  command = exports.command = _ref.command,
  innerSkeleton = _ref.innerSkeleton,
  innerHotkey = _ref.innerHotkey;
_lowcodeEditorCore.globalContext.register(editor, _lowcodeEditorCore.Editor);
_lowcodeEditorCore.globalContext.register(editor, 'editor');
_lowcodeEditorCore.globalContext.register(innerWorkspace, 'workspace');
// 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;
var root;
// @ts-ignore webpack Define variable
var version = exports.version = VERSION_PLACEHOLDER || '1.0.0';
_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);
          root = (0, _client.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__*/(0, _react.createElement)(_lowcodeWorkspace.Workbench, {
            workspace: innerWorkspace,
            // skeleton: workspace.skeleton,
            className: 'engine-main',
            topAreaItemClassName: 'engine-actionitem'
          }));
          innerWorkspace.enableAutoOpenFirstWindow = _lowcodeEditorCore.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__*/(0, _react.createElement)(Workbench, {
            skeleton: innerSkeleton,
            className: 'engine-main',
            topAreaItemClassName: 'engine-actionitem'
          }));
        case 23:
        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.
          root && root.unmount();
        case 3:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
  return _destroy.apply(this, arguments);
}