import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import uniqid from "uniqid";

var isOptionsObject = function isOptionsObject(item) {
  return item && !Array.isArray(item) && !item.type && !item.name;
};

var normalizeArgs = function normalizeArgs(args) {
  var options = {}; // Check if last item in the plugins array is actually an options object.

  if (isOptionsObject(args[args.length - 1])) {
    var _args$splice = args.splice(-1, 1);

    var _args$splice2 = _slicedToArray(_args$splice, 1);

    options = _args$splice2[0];
  }

  return [args, options];
};

var assign = function assign(plugins, options, target, nameSaveArray) {
  for (var i = 0; i < plugins.length; i++) {
    var plugin = plugins[i];

    if (Array.isArray(plugin)) {
      assign(plugin, options, target, nameSaveArray);
      continue;
    }

    var name = plugin._name || plugin.name;

    if (!name) {
      plugin.name = name = uniqid(plugin.type + "-");
    }

    if (Array.isArray(nameSaveArray)) {
      nameSaveArray.push(plugin.name);
    } // If skip existing was set to true, and a plugin with the same name was already registered, skip registration.


    if (!options.skipExisting || !target[name]) {
      target[name] = plugin;
      plugin.init && plugin.init();
    }
  }
};

export var PluginsContainer = /*#__PURE__*/function () {
  // 记录业务侧自定义插件名称，方便后面随时卸载
  function PluginsContainer() {
    _classCallCheck(this, PluginsContainer);

    _defineProperty(this, "plugins", {});

    _defineProperty(this, "currentType", '');

    _defineProperty(this, "customPluginNames", []);

    this.register.apply(this, arguments);
  }

  _createClass(PluginsContainer, [{
    key: "byName",
    value: function byName(name) {
      return this.plugins[name];
    }
  }, {
    key: "byType",
    value: function byType(type) {
      return Object.values(this.plugins).filter(function (pl) {
        return pl.type === type;
      });
    }
  }, {
    key: "all",
    value: function all() {
      return Object.values(this.plugins);
    }
  }, {
    key: "register",
    value: function register() {
      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      var _normalizeArgs = normalizeArgs(args),
          _normalizeArgs2 = _slicedToArray(_normalizeArgs, 2),
          plugins = _normalizeArgs2[0],
          options = _normalizeArgs2[1];

      assign(plugins, options, this.plugins);
    }
  }, {
    key: "registerCustom",
    value: function registerCustom() {
      for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
        args[_key2] = arguments[_key2];
      }

      var _normalizeArgs3 = normalizeArgs(args),
          _normalizeArgs4 = _slicedToArray(_normalizeArgs3, 2),
          plugins = _normalizeArgs4[0],
          options = _normalizeArgs4[1];

      assign(plugins, options, this.plugins, this.customPluginNames);
    }
  }, {
    key: "unregister",
    value: function unregister(name) {
      delete this.plugins[name];
    }
  }]);

  return PluginsContainer;
}();
//# sourceMappingURL=PluginsContainer.js.map