import lodash from "lodash";
import { isPresent } from "ts-is-present";
import { EditorPlugin } from "./editor-plugin.js";
import { mergeConfig } from "./merge-config.js";
class PluginCollisionError extends Error {
}
class EditorPlugins {
  constructor(services) {
    this.plugins = [];
    this.services = services;
  }
  register(plugin) {
    const { isMobile } = this.services?.context || {};
    if (isMobile && plugin?.disableOnMobile) {
      return;
    }
    const candidate = EditorPlugin.of(plugin, { ...this.services, plugins: this });
    const duplicate = this.hasDuplicate(candidate);
    if (duplicate) {
      throw new PluginCollisionError(
        `the plugin ${candidate.getType()} conflicts with ${duplicate.getType()}`
      );
    }
    candidate.register();
    this.plugins.push(candidate);
  }
  unregister(plugin) {
    plugin.unregister();
    return this.filter((p) => !p.equals(plugin));
  }
  destroy() {
    this.plugins.map((p) => p.unregister());
    this.plugins = [];
  }
  filter(predicate) {
    this.plugins = this.plugins.filter(predicate);
    return this.plugins;
  }
  asArray() {
    return this.plugins;
  }
  getConfig(type) {
    return this.plugins.filter((plugin) => plugin.getType() === type)[0]?.getConfig() || {};
  }
  getTextButtons() {
    return this.plugins.flatMap((plugin) => plugin.getTextButtons() || []);
  }
  getQuickActions() {
    return this.plugins.flatMap((plugin) => plugin.getQuickActions());
  }
  getAddButtons() {
    return this.plugins.flatMap((plugin) => plugin.getAddButtons() || []);
  }
  getFloatingToolbarButtons() {
    return this.plugins.flatMap((plugin) => plugin.getFloatingToolbarButton()).filter(isPresent).sort(({ priority: a }, { priority: b }) => b - a);
  }
  getExports() {
    return this.plugins.map((plugin) => plugin.getExports()).filter(isPresent).reduce(
      (integrations, pluginIntegrations) => {
        return lodash.merge(integrations, pluginIntegrations);
      },
      {}
    );
  }
  getVisibleToolbar(selection, editor) {
    const toolbar = this.plugins.flatMap((plugin) => plugin.getToolbars()).find((toolbar2) => !!toolbar2?.isVisible(selection, editor));
    return toolbar;
  }
  getTiptapExtensions() {
    return lodash.compact(this.plugins.flatMap((plugin) => plugin.getTiptapExtensions?.() || []));
  }
  getPluginProviders() {
    return this.plugins.map((plugin) => plugin.getProvider()).filter(isPresent);
  }
  getPluginGlobalComponents() {
    return this.plugins.map((plugin) => plugin.getGlobalComponent()).filter(isPresent);
  }
  isFooterToolbarDisabled(selection) {
    const nodeType = selection?.node.type.name;
    if (nodeType) {
      const selectedPlugin = this.plugins.find(
        (plugin) => plugin?.getTiptapExtensions()?.[0]?.name === nodeType
      );
      return !!selectedPlugin?.getTiptapExtensions()?.[0]?.groups.includes("footer-disabled");
    }
    return false;
  }
  hasDuplicate(plugin) {
    return this.plugins.find((p) => p.equals(plugin));
  }
  setPublicCommands(publicCommands) {
    this.publicCommands = publicCommands;
  }
  getPublicCommands() {
    return this.publicCommands;
  }
}
EditorPlugins.mergeConfig = mergeConfig;
export {
  EditorPlugins,
  PluginCollisionError
};
//# sourceMappingURL=editor-plugins.js.map
