/**
 * InMemoryAgentMessageBus — test-time adapter for {@link IAgentMessageBus}.
 *
 * Persists through a wrapped {@link InMemoryAgentMessageRepository} and
 * delivers to in-process subscribers synchronously. Cross-process delivery
 * is the SQLite adapter's job — this class is intentionally process-local
 * and is the bus used by every unit/integration test that does not need a
 * real database.
 */
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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { inject, injectable } from 'tsyringe';
import { PeerAddressingForbiddenError } from '../../../domain/errors/peer-addressing-forbidden.error.js';
function matches(filter, message) {
    if (filter.appId !== message.appId)
        return false;
    if (filter.featureId !== undefined && filter.featureId !== message.featureId)
        return false;
    if (filter.agentRunId !== undefined) {
        const runMatches = message.fromAgentRunId === filter.agentRunId ||
            (message.toKind === 'agent' && message.toTarget === filter.agentRunId);
        if (!runMatches)
            return false;
    }
    return true;
}
let InMemoryAgentMessageBus = class InMemoryAgentMessageBus {
    repository;
    subscriptions = new Set();
    constructor(repository) {
        this.repository = repository;
    }
    async publish(message) {
        if (message.toKind === 'peer') {
            throw new PeerAddressingForbiddenError(message.toKind);
        }
        await this.repository.create(message);
        for (const sub of this.subscriptions) {
            if (matches(sub.filter, message)) {
                await sub.handler(message);
            }
        }
    }
    subscribe(filter, handler) {
        const sub = { filter, handler };
        this.subscriptions.add(sub);
        return () => {
            this.subscriptions.delete(sub);
        };
    }
    async listFor(filter, limit) {
        const rows = await this.repository.listByScope(filter.appId, filter.featureId, {
            since: filter.since,
            limit,
        });
        if (filter.agentRunId === undefined)
            return rows;
        return rows.filter((m) => m.fromAgentRunId === filter.agentRunId ||
            (m.toKind === 'agent' && m.toTarget === filter.agentRunId));
    }
};
InMemoryAgentMessageBus = __decorate([
    injectable(),
    __param(0, inject('IAgentMessageRepository')),
    __metadata("design:paramtypes", [Object])
], InMemoryAgentMessageBus);
export { InMemoryAgentMessageBus };
