"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.EditorPresetBuilder = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _construct2 = _interopRequireDefault(require("@babel/runtime/helpers/construct"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var EditorPresetBuilder = exports.EditorPresetBuilder = /*#__PURE__*/function () {
  function EditorPresetBuilder() {
    (0, _classCallCheck2.default)(this, EditorPresetBuilder);
    (0, _defineProperty2.default)(this, "safeEntry", function (plugin) {
      return Array.isArray(plugin) ? plugin : [plugin, undefined];
    });
    for (var _len = arguments.length, more = new Array(_len), _key = 0; _key < _len; _key++) {
      more[_key] = arguments[_key];
    }
    this.data = [].concat(more) || [];
  }
  (0, _createClass2.default)(EditorPresetBuilder, [{
    key: "add",
    value: function add(nextOrTuple) {
      return (0, _construct2.default)(EditorPresetBuilder, [
      /**
       * re-cast this to NewPlugin as we've done all the type
       * safety, dependency checking, narrowing, during
       * `SafePresetCheck & VerifyPluginDependencies`
       */
      nextOrTuple].concat((0, _toConsumableArray2.default)(this.data)));
    }
  }, {
    key: "maybeAdd",
    value: function maybeAdd(pluginToAdd, shouldAdd) {
      var 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;
      }
      var nextPluginStack = [
      /**
       * re-cast this to NewPlugin as we've done all the type
       * safety, dependency checking, narrowing, during
       * `SafePresetCheck & VerifyPluginDependencies`
       */
      pluginOrBuilder ? pluginToAdd : undefined].concat((0, _toConsumableArray2.default)(this.data));
      var nextEditorPresetBuilder = (0, _construct2.default)(EditorPresetBuilder, (0, _toConsumableArray2.default)(nextPluginStack));
      return nextEditorPresetBuilder;
    }
  }, {
    key: "has",
    value: function has(plugin) {
      return this.data.some(function (pluginPreset) {
        if (Array.isArray(pluginPreset)) {
          return pluginPreset[0] === plugin;
        }
        return pluginPreset === plugin;
      });
    }
  }, {
    key: "build",
    value: function build() {
      var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        pluginInjectionAPI = _ref.pluginInjectionAPI,
        maybeExcludePlugins = _ref.excludePlugins;
      var excludePlugins = new Set(maybeExcludePlugins ? maybeExcludePlugins : []);
      var editorPlugins = this.processEditorPlugins({
        pluginInjectionAPI: pluginInjectionAPI,
        excludePlugins: excludePlugins
      });
      return this.removeExcludedPlugins(editorPlugins, excludePlugins);
    }
  }, {
    key: "verifyDuplicatedPlugins",
    value: function verifyDuplicatedPlugins() {
      var cache = new Set();
      this.data.filter(Boolean).forEach(function (pluginEntry) {
        var _ref2 = Array.isArray(pluginEntry) ? pluginEntry : [pluginEntry, undefined],
          _ref3 = (0, _slicedToArray2.default)(_ref2, 2),
          pluginFn = _ref3[0],
          _ = _ref3[1];
        if (cache.has(pluginFn)) {
          throw new Error("".concat(pluginFn, " is already included!"));
        }
        cache.add(pluginFn);
      });
      return true;
    }
  }, {
    key: "processEditorPlugins",
    value: function processEditorPlugins(_ref4) {
      var _this = this;
      var pluginInjectionAPI = _ref4.pluginInjectionAPI,
        excludePlugins = _ref4.excludePlugins;
      this.verifyDuplicatedPlugins();
      var seen = new Set();
      var pluginsDataCopy = this.data.slice();
      var plugins = pluginsDataCopy.reverse().filter(Boolean).map(function (entry) {
        var _this$safeEntry = _this.safeEntry(entry),
          _this$safeEntry2 = (0, _slicedToArray2.default)(_this$safeEntry, 2),
          fn = _this$safeEntry2[0],
          config = _this$safeEntry2[1];
        if (seen.has(fn)) {
          return null;
        }
        seen.add(fn);
        if (typeof fn !== 'function') {
          return null;
        }
        var plugin = pluginInjectionAPI ? fn({
          config: config,
          api: pluginInjectionAPI.api()
        }) : fn({
          config: 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;
    }
  }, {
    key: "removeExcludedPlugins",
    value: function removeExcludedPlugins(plugins, excludes) {
      if (excludes) {
        return plugins.filter(function (plugin) {
          return !plugin || !excludes.has(plugin.name);
        });
      }
      return plugins;
    }
  }]);
  return EditorPresetBuilder;
}();