"use strict";
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InProcessEventBus = void 0;
const di_1 = require("@n8n/di");
const node_events_1 = require("node:events");
const MAX_EVENTS_PER_THREAD = 500;
const MAX_BYTES_PER_THREAD = 2 * 1024 * 1024;
let InProcessEventBus = class InProcessEventBus {
    constructor() {
        this.emitter = new node_events_1.EventEmitter();
        this.store = new Map();
        this.sizeBytes = new Map();
        this.nextId = new Map();
        this.emitter.setMaxListeners(0);
    }
    publish(threadId, event) {
        const events = this.getOrCreateStore(threadId);
        const id = (this.nextId.get(threadId) ?? 0) + 1;
        this.nextId.set(threadId, id);
        const stored = { id, event };
        const eventSize = JSON.stringify(event).length;
        events.push(stored);
        this.sizeBytes.set(threadId, (this.sizeBytes.get(threadId) ?? 0) + eventSize);
        this.evictIfNeeded(threadId, events);
        this.emitter.emit(threadId, stored);
    }
    subscribe(threadId, handler) {
        this.emitter.on(threadId, handler);
        return () => this.emitter.off(threadId, handler);
    }
    getEventsAfter(threadId, afterId) {
        const events = this.store.get(threadId);
        if (!events)
            return [];
        return events.filter((e) => e.id > afterId);
    }
    getEventsForRun(threadId, runId) {
        const events = this.store.get(threadId);
        if (!events)
            return [];
        return events.filter((e) => e.event.runId === runId).map((e) => e.event);
    }
    getEventsForRuns(threadId, runIds) {
        const events = this.store.get(threadId);
        if (!events || runIds.length === 0)
            return [];
        const runIdSet = new Set(runIds);
        return events.filter((e) => runIdSet.has(e.event.runId)).map((e) => e.event);
    }
    getNextEventId(threadId) {
        return (this.nextId.get(threadId) ?? 0) + 1;
    }
    clearThread(threadId) {
        this.store.delete(threadId);
        this.sizeBytes.delete(threadId);
        this.nextId.delete(threadId);
        this.emitter.removeAllListeners(threadId);
    }
    clear() {
        this.store.clear();
        this.sizeBytes.clear();
        this.nextId.clear();
        this.emitter.removeAllListeners();
    }
    evictIfNeeded(threadId, events) {
        let totalSize = this.sizeBytes.get(threadId) ?? 0;
        while (events.length > MAX_EVENTS_PER_THREAD || totalSize > MAX_BYTES_PER_THREAD) {
            const evicted = events.shift();
            if (!evicted)
                break;
            totalSize -= JSON.stringify(evicted.event).length;
        }
        this.sizeBytes.set(threadId, Math.max(0, totalSize));
    }
    getOrCreateStore(threadId) {
        let events = this.store.get(threadId);
        if (!events) {
            events = [];
            this.store.set(threadId, events);
        }
        return events;
    }
};
exports.InProcessEventBus = InProcessEventBus;
exports.InProcessEventBus = InProcessEventBus = __decorate([
    (0, di_1.Service)(),
    __metadata("design:paramtypes", [])
], InProcessEventBus);
//# sourceMappingURL=in-process-event-bus.js.map