"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
class EventBus {
  constructor() {
    this.instance = null;
    this.subscribers = {};
  }
  static getInstance() {
    if (this.instance) return this.instance;
    this.instance = new EventBus();
    return this.instance;
  }
  subscribe(type, handler) {
    if (!this.subscribers[type]) {
      this.subscribers[type] = [];
    }
    const handlers = this.subscribers[type];
    handlers.push(handler);
    return () => {
      const index = handlers.indexOf(handler);
      if (index > -1) {
        handlers.splice(index, 1);
      }
    };
  }
  dispatch(type) {
    for (var _len = arguments.length, data = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
      data[_key - 1] = arguments[_key];
    }
    const handlers = this.subscribers[type];
    if (Array.isArray(handlers)) {
      handlers.forEach(handler => handler(...data));
    }
  }
}
var _default = exports.default = EventBus;