import _defineProperty from "@babel/runtime/helpers/defineProperty";
export class EditorPresetBuilder {
  constructor(...more) {
    _defineProperty(this, "safeEntry", plugin => Array.isArray(plugin) ? plugin : [plugin, undefined]);
    this.data = [...more] || [];
  }
  add(nextOrTuple) {
    return new EditorPresetBuilder(
    /**
     * re-cast this to NewPlugin as we've done all the type
     * safety, dependency checking, narrowing, during
     * `SafePresetCheck & VerifyPluginDependencies`
     */
    nextOrTuple, ...this.data);
  }
  maybeAdd(pluginToAdd, shouldAdd) {
    const pluginOrBuilder = typeof shouldAdd === 'function' ?
    // @ts-expect-error Argument of type 'SafePresetCheck<ToAddPlugin, StackPlugins>' is not assignable to parameter of type 'ToAddPlugin'.
    shouldAdd(pluginToAdd, this) : shouldAdd;
    if (pluginOrBuilder instanceof EditorPresetBuilder) {
      return pluginOrBuilder;
    }
    const nextPluginStack = [
    /**
     * re-cast this to NewPlugin as we've done all the type
     * safety, dependency checking, narrowing, during
     * `SafePresetCheck & VerifyPluginDependencies`
     */
    pluginOrBuilder ? pluginToAdd : undefined, ...this.data];
    const nextEditorPresetBuilder = new EditorPresetBuilder(...nextPluginStack);
    return nextEditorPresetBuilder;
  }
  has(plugin) {
    return this.data.some(pluginPreset => {
      if (Array.isArray(pluginPreset)) {
        return pluginPreset[0] === plugin;
      }
      return pluginPreset === plugin;
    });
  }
  build({
    pluginInjectionAPI,
    excludePlugins: maybeExcludePlugins
  } = {}) {
    const excludePlugins = new Set(maybeExcludePlugins ? maybeExcludePlugins : []);
    const editorPlugins = this.processEditorPlugins({
      pluginInjectionAPI,
      excludePlugins
    });
    return this.removeExcludedPlugins(editorPlugins, excludePlugins);
  }
  verifyDuplicatedPlugins() {
    const cache = new Set();
    this.data.filter(Boolean).forEach(pluginEntry => {
      const [pluginFn, _] = Array.isArray(pluginEntry) ? pluginEntry : [pluginEntry, undefined];
      if (cache.has(pluginFn)) {
        throw new Error(`${pluginFn} is already included!`);
      }
      cache.add(pluginFn);
    });
    return true;
  }
  processEditorPlugins({
    pluginInjectionAPI,
    excludePlugins
  }) {
    this.verifyDuplicatedPlugins();
    const seen = new Set();
    const pluginsDataCopy = this.data.slice();
    const plugins = pluginsDataCopy.reverse().filter(Boolean).map(entry => {
      const [fn, config] = this.safeEntry(entry);
      if (seen.has(fn)) {
        return null;
      }
      seen.add(fn);
      if (typeof fn !== 'function') {
        return null;
      }
      const plugin = pluginInjectionAPI ? fn({
        config,
        api: pluginInjectionAPI.api()
      }) : fn({
        config
      });
      if (plugin && excludePlugins !== null && excludePlugins !== void 0 && excludePlugins.has(plugin.name)) {
        return null;
      }
      if (!pluginInjectionAPI) {
        return plugin;
      }
      pluginInjectionAPI.onEditorPluginInitialized(plugin);
      return plugin;
    }).filter(Boolean);
    return plugins;
  }
  removeExcludedPlugins(plugins, excludes) {
    if (excludes) {
      return plugins.filter(plugin => !plugin || !excludes.has(plugin.name));
    }
    return plugins;
  }
}