class SimpleEventBus {
  handlers = /* @__PURE__ */ new Map();
  eventHistory = [];
  maxHistorySize = 1e3;
  subscriptionCounter = 0;
  emit(event) {
    const fullEvent = {
      ...event,
      id: this.generateEventId(),
      timestamp: Date.now()
    };
    this.eventHistory.push(fullEvent);
    if (this.eventHistory.length > this.maxHistorySize) {
      this.eventHistory.shift();
    }
    const handlers = this.handlers.get(event.type);
    if (handlers) {
      const handlersCopy = Array.from(handlers);
      for (const handler of handlersCopy) {
        try {
          const result = handler(fullEvent);
          if (result instanceof Promise) {
            result.catch((error) => {
              console.error(`Event handler error for ${event.type}:`, error);
            });
          }
        } catch (error) {
          console.error(`Event handler error for ${event.type}:`, error);
        }
      }
    }
  }
  on(eventType, handler) {
    if (!this.handlers.has(eventType)) {
      this.handlers.set(eventType, /* @__PURE__ */ new Set());
    }
    this.handlers.get(eventType).add(handler);
    const subscriptionId = `sub-${++this.subscriptionCounter}`;
    return {
      id: subscriptionId,
      unsubscribe: () => this.off(eventType, handler)
    };
  }
  once(eventType, handler) {
    const wrapper = (event) => {
      handler(event);
      this.off(eventType, wrapper);
    };
    return this.on(eventType, wrapper);
  }
  off(eventType, handler) {
    this.handlers.get(eventType)?.delete(handler);
  }
  offAll(eventType) {
    if (eventType) {
      this.handlers.delete(eventType);
    } else {
      this.handlers.clear();
    }
  }
  async waitFor(eventType, timeout = 3e4) {
    return new Promise((resolve, reject) => {
      const timer = setTimeout(() => {
        reject(new Error(`Timeout waiting for event: ${eventType}`));
      }, timeout);
      const subscription = this.once(eventType, (event) => {
        clearTimeout(timer);
        resolve(event);
      });
    });
  }
  generateEventId() {
    return `evt-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
  }
  getHistory(eventType, limit = 100) {
    let events = this.eventHistory;
    if (eventType) {
      events = events.filter((e) => e.type === eventType);
    }
    return events.slice(-limit);
  }
}
const globalEventBus = new SimpleEventBus();
const EventTypes = {
  // Ralph Loop events
  RALPH_ITERATION_START: "ralph.iteration.start",
  RALPH_ITERATION_COMPLETE: "ralph.iteration.complete",
  RALPH_ITERATION_FAILED: "ralph.iteration.failed",
  RALPH_LOOP_COMPLETE: "ralph.loop.complete",
  // Swarm events
  SWARM_AGENT_STARTED: "swarm.agent.started",
  SWARM_AGENT_STOPPED: "swarm.agent.stopped",
  SWARM_TASK_ASSIGNED: "swarm.task.assigned",
  SWARM_TASK_COMPLETE: "swarm.task.complete",
  SWARM_CONFLICT_DETECTED: "swarm.conflict.detected",
  // Context events
  CONTEXT_SAVED: "context.saved",
  CONTEXT_LOADED: "context.loaded",
  CONTEXT_SYNCED: "context.synced",
  // Git events
  GIT_COMMIT_CREATED: "git.commit.created",
  GIT_BRANCH_CREATED: "git.branch.created",
  GIT_MERGE_COMPLETE: "git.merge.complete"
};
export {
  EventTypes,
  SimpleEventBus,
  globalEventBus
};
//# sourceMappingURL=event-bus.js.map
