/**
 * In-Memory AgentMessage Repository
 *
 * Test-friendly adapter for {@link IAgentMessageRepository}. Backed by a
 * single Map keyed by message id. Every list/find query is filtered by
 * appId at the entry point — there is no cross-app leakage by construction.
 */
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;
};
import { injectable } from 'tsyringe';
function toMillis(value) {
    if (value instanceof Date)
        return value.getTime();
    if (typeof value === 'number')
        return value;
    if (typeof value === 'string')
        return new Date(value).getTime();
    return 0;
}
let InMemoryAgentMessageRepository = class InMemoryAgentMessageRepository {
    messages = new Map();
    async create(message) {
        if (this.messages.has(message.id)) {
            throw new Error(`AgentMessage with id "${message.id}" already exists`);
        }
        this.messages.set(message.id, { ...message });
    }
    async findById(appId, id) {
        const row = this.messages.get(id);
        if (!row || row.appId !== appId)
            return null;
        return { ...row };
    }
    async findByCorrelationId(appId, correlationId) {
        for (const row of this.messages.values()) {
            if (row.appId === appId && row.correlationId === correlationId) {
                return { ...row };
            }
        }
        return null;
    }
    async listByScope(appId, featureId, filters = {}) {
        const sinceMillis = filters.since ? filters.since.getTime() : undefined;
        const result = [];
        for (const row of this.messages.values()) {
            if (row.appId !== appId)
                continue;
            if (featureId !== undefined && row.featureId !== featureId)
                continue;
            if (filters.undeliveredOnly && row.deliveredAt)
                continue;
            if (sinceMillis !== undefined && toMillis(row.createdAt) < sinceMillis) {
                continue;
            }
            result.push({ ...row });
        }
        result.sort((a, b) => toMillis(a.createdAt) - toMillis(b.createdAt));
        return filters.limit !== undefined ? result.slice(0, filters.limit) : result;
    }
    async markDelivered(appId, id, deliveredAt) {
        const row = this.messages.get(id);
        if (!row || row.appId !== appId)
            return;
        if (row.deliveredAt)
            return;
        this.messages.set(id, { ...row, deliveredAt });
    }
};
InMemoryAgentMessageRepository = __decorate([
    injectable()
], InMemoryAgentMessageRepository);
export { InMemoryAgentMessageRepository };
