import { Editor, commonEvent, engineConfig, Setters as InnerSetters, Hotkey as InnerHotkey, Command as InnerCommand } from '@gant-lowcode/lowcode-editor-core';
import { Designer, LowCodePluginManager } from '@gant-lowcode/lowcode-designer';
import { Skeleton as InnerSkeleton } from '@gant-lowcode/lowcode-editor-skeleton';
import { IPublicEnumPluginRegisterLevel } from '@gant-lowcode/lowcode-types';
import { Hotkey, Plugins, Project, Skeleton, Setters, Material, Event, Common, Logger, Workspace, Window, Canvas, CommonUI, Command, Config } from '@gant-lowcode/lowcode-shell';
import { getLogger } from '@gant-lowcode/lowcode-utils';
import { shellModelFactory } from './shell-model-factory';
var CoreModelFactory = /*#__PURE__*/function () {
  function CoreModelFactory() {}
  var _proto = CoreModelFactory.prototype;
  _proto.createCore = function createCore(innerWorkspace, viewName, workspaceMode, registerLevel, editorWindow) {
    var workspace = new Workspace(innerWorkspace);
    var editor = new Editor(viewName, workspaceMode);
    var engineContext = {};
    var innerSkeleton = new InnerSkeleton(editor, viewName);
    editor.set('skeleton', innerSkeleton);
    var designer = new Designer({
      editor: editor,
      viewName: viewName,
      shellModelFactory: shellModelFactory
    });
    editor.set('designer', designer);
    var innerProject = designer.project;
    var innerHotkey = new InnerHotkey(viewName);
    var hotkey = new Hotkey(innerHotkey, workspaceMode);
    var project = new Project(innerProject, workspaceMode);
    var skeleton = new Skeleton(innerSkeleton, 'any', workspaceMode);
    var innerSetters = new InnerSetters(viewName);
    var setters = new Setters(innerSetters, workspaceMode);
    var innerCommand = new InnerCommand();
    if (!workspaceMode) {
      var command = new Command(innerCommand, engineContext);
      engineContext.command = command;
    }
    var material = new Material(editor, workspaceMode);
    var commonUI = new CommonUI(editor);
    editor.set('setters', setters);
    editor.set('project', project);
    editor.set('material', material);
    editor.set('hotkey', hotkey);
    editor.set('innerHotkey', innerHotkey);
    var config;
    if (!workspaceMode) {
      config = new Config(engineConfig);
    } else {
      config = engineConfig;
    }
    var event = new Event(commonEvent, {
      prefix: 'common'
    });
    var logger = getLogger({
      level: 'warn',
      bizName: 'common'
    });
    var canvas = new Canvas(editor, workspaceMode);
    var common = new Common(editor, innerSkeleton);
    var plugins;
    var pluginContextApiAssembler = {
      assembleApis: function assembleApis(context, pluginName, meta) {
        context.hotkey = hotkey;
        context.project = project;
        context.skeleton = new Skeleton(innerSkeleton, pluginName, true);
        context.setters = setters;
        context.material = material;
        var eventPrefix = (meta === null || meta === void 0 ? void 0 : meta.eventPrefix) || 'common';
        var commandScope = meta === null || meta === void 0 ? void 0 : meta.commandScope;
        context.event = new Event(commonEvent, {
          prefix: eventPrefix
        });
        context.config = config;
        context.common = common;
        context.plugins = plugins;
        context.logger = new Logger({
          level: 'warn',
          bizName: "plugin:" + pluginName
        });
        context.canvas = canvas;
        context.commonUI = commonUI;
        context.workspace = workspace;
        if (editorWindow) {
          context.editorWindow = new Window(editorWindow);
        }
        context.command = new Command(innerCommand, context, {
          commandScope: commandScope
        });
        if (registerLevel) {
          context.registerLevel = registerLevel;
        }
        context.isPluginRegisteredInWorkspace = registerLevel === IPublicEnumPluginRegisterLevel.Workspace;
        editor.set('pluginContext', context);
      }
    };
    var innerPlugins = new LowCodePluginManager(pluginContextApiAssembler, viewName);
    plugins = new Plugins(innerPlugins, true).toProxy();
    editor.set('plugins', plugins);
    editor.set('innerPlugins', innerPlugins);
    engineContext.skeleton = skeleton;
    engineContext.plugins = plugins;
    engineContext.project = project;
    engineContext.setters = setters;
    engineContext.material = material;
    engineContext.event = event;
    engineContext.logger = logger;
    engineContext.hotkey = hotkey;
    engineContext.common = common;
    engineContext.workspace = workspace;
    engineContext.canvas = canvas;
    engineContext.commonUI = commonUI;
    engineContext.innerSetters = innerSetters;
    engineContext.innerSkeleton = innerSkeleton;
    engineContext.innerProject = innerProject;
    engineContext.innerHotkey = innerHotkey;
    engineContext.innerPlugins = innerPlugins;
    return {
      designer: designer,
      engineContext: engineContext,
      editor: editor,
      plugins: plugins,
      config: config
    };
  };
  return CoreModelFactory;
}();
export var coreModelFactory = new CoreModelFactory();