/**
 * In-memory operation log event bus.
 *
 * Single-process pub/sub backed by node:events. The SQLite operation-log
 * repository publishes here after a successful INSERT; the SSE route
 * (StreamAgentEventsUseCase) subscribes and re-emits as notifications.
 *
 * A throwing subscriber is logged and swallowed so it never prevents
 * delivery to the other subscribers — publish() is best-effort fanout.
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { EventEmitter } from 'node:events';
import { injectable } from 'tsyringe';
const EVENT_NAME = 'operation-log-appended';
let InMemoryOperationLogEventBus = class InMemoryOperationLogEventBus {
    emitter = new EventEmitter();
    constructor() {
        // Prevent MaxListenersExceededWarning when many SSE clients subscribe.
        this.emitter.setMaxListeners(0);
    }
    publish(event) {
        this.emitter.emit(EVENT_NAME, event);
    }
    subscribe(listener) {
        const safeListener = (event) => {
            try {
                listener(event);
            }
            catch (error) {
                // Low-level bus — no logger injected here. A misbehaving
                // subscriber must not poison the fanout; log to stderr and move
                // on. The SSE stream and every other subscriber keep working.
                // eslint-disable-next-line no-console
                console.error('[OperationLogBus] subscriber threw — continuing fanout', error);
            }
        };
        this.emitter.on(EVENT_NAME, safeListener);
        return () => this.emitter.off(EVENT_NAME, safeListener);
    }
};
InMemoryOperationLogEventBus = __decorate([
    injectable(),
    __metadata("design:paramtypes", [])
], InMemoryOperationLogEventBus);
export { InMemoryOperationLogEventBus };
