"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var events_exports = {};
__export(events_exports, {
  RicosEvents: () => RicosEvents
});
module.exports = __toCommonJS(events_exports);
var import_ricos_event_logger = require("./ricos-event-logger.js");
var import_ricos_event = require("./ricos-event.js");
class DuplicateEventRegistrationError extends Error {
}
class RicosEvents {
  constructor(isDebugMode = false) {
    this.events = [];
    this.logger = (0, import_ricos_event_logger.getEventLogger)(isDebugMode);
  }
  register(topic) {
    const event = new import_ricos_event.RicosEvent(topic, this.logger);
    if (this.events.some((e) => e.getTopic().equals(event.getTopic()))) {
      throw new DuplicateEventRegistrationError(`Event ${topic} already registered`);
    }
    this.events.push(event);
    this.logger.log(`[${topic}]: event registered`);
    return new RicosEventPublisher(event);
  }
  getAllTopics() {
    return this.events.map((e) => e.getTopic().toString());
  }
  subscribe(topic, handler, id) {
    const subscriptions = this.events.filter((e) => e.getTopic().matches(topic)).map((e) => e.subscribe(handler, id));
    if (subscriptions.length === 0) {
      this.logger.log(`No event found for topic ${topic} while subscribing ${id}`);
    }
    return {
      topic,
      cancel: () => subscriptions.forEach((s) => s.cancel())
    };
  }
  clear() {
    this.events.forEach((e) => e.dispose());
    this.events = [];
  }
}
class RicosEventPublisher {
  constructor(event) {
    this.event = event;
    this.event = event;
  }
  publish(data) {
    return this.event.publish(data);
  }
  publishSync(data) {
    return this.event.publishSync(data);
  }
  publishOnce(data) {
    return this.event.publishOnce(data);
  }
  get topic() {
    return this.event.getTopic().toString();
  }
}
//# sourceMappingURL=events.js.map
