export class PluginsRegister {
  constructor(poly) {
    this.poly = poly;
    this._plugins_by_name = new Map();
    this._plugin_name_by_node_context_by_type = new Map();
    this._plugin_name_by_operation_context_by_type = new Map();
  }
  register(plugin) {
    this._current_plugin = plugin;
    this._plugins_by_name.set(plugin.name(), plugin);
    plugin.init(this.poly);
    this._current_plugin = void 0;
  }
  pluginByName(pluginName) {
    return this._plugins_by_name.get(pluginName);
  }
  registerNode(node) {
    if (!this._current_plugin) {
      return;
    }
    const context = node.nodeContext();
    const type = node.type();
    let map_for_context = this._plugin_name_by_node_context_by_type.get(context);
    if (!map_for_context) {
      map_for_context = new Map();
      this._plugin_name_by_node_context_by_type.set(context, map_for_context);
    }
    map_for_context.set(type, this._current_plugin.name());
  }
  registerOperation(operation) {
    if (!this._current_plugin) {
      return;
    }
    const context = operation.context();
    const type = operation.type();
    let map_for_context = this._plugin_name_by_operation_context_by_type.get(context);
    if (!map_for_context) {
      map_for_context = new Map();
      this._plugin_name_by_operation_context_by_type.set(context, map_for_context);
    }
    map_for_context.set(type, this._current_plugin.name());
  }
  toJson() {
    const data = {
      plugins: {},
      nodes: {},
      operations: {}
    };
    this._plugins_by_name.forEach((plugin, name) => {
      data.plugins[name] = plugin.toJSON();
    });
    this._plugin_name_by_node_context_by_type.forEach((map_for_context, context) => {
      data.nodes[context] = {};
      map_for_context.forEach((plugin_name, type) => {
        data.nodes[context][type] = plugin_name;
      });
    });
    this._plugin_name_by_operation_context_by_type.forEach((map_for_context, context) => {
      data.operations[context] = {};
      map_for_context.forEach((plugin_name, type) => {
        data.operations[context][type] = plugin_name;
      });
    });
    return data;
  }
}
