/**
 * In-Memory SupervisorDecision Repository
 *
 * Test-friendly adapter for {@link ISupervisorDecisionRepository}.
 * Append-only — no update or delete.
 */
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 InMemorySupervisorDecisionRepository = class InMemorySupervisorDecisionRepository {
    decisions = new Map();
    async create(decision) {
        if (this.decisions.has(decision.id)) {
            throw new Error(`SupervisorDecision with id "${decision.id}" already exists`);
        }
        this.decisions.set(decision.id, { ...decision });
    }
    async findById(id) {
        const row = this.decisions.get(id);
        return row ? { ...row } : null;
    }
    async listBySourceEvent(sourceEventKind, sourceEventId) {
        const result = [];
        for (const row of this.decisions.values()) {
            if (row.sourceEventKind !== sourceEventKind)
                continue;
            if (row.sourceEventId !== sourceEventId)
                continue;
            result.push({ ...row });
        }
        result.sort((a, b) => toMillis(a.createdAt) - toMillis(b.createdAt));
        return result;
    }
    async listBySupervisorRun(supervisorRunId) {
        const result = [];
        for (const row of this.decisions.values()) {
            if (row.supervisorRunId !== supervisorRunId)
                continue;
            result.push({ ...row });
        }
        result.sort((a, b) => toMillis(a.createdAt) - toMillis(b.createdAt));
        return result;
    }
    async listByScope(scopeType, scopeId, featureId, filters = {}) {
        const sinceMillis = filters.since ? filters.since.getTime() : undefined;
        const result = [];
        for (const row of this.decisions.values()) {
            if (row.scopeType !== scopeType)
                continue;
            if ((row.scopeId ?? undefined) !== (scopeId ?? undefined))
                continue;
            if (featureId !== undefined && row.featureId !== featureId)
                continue;
            if (sinceMillis !== undefined && toMillis(row.createdAt) < sinceMillis)
                continue;
            result.push({ ...row });
        }
        result.sort((a, b) => toMillis(b.createdAt) - toMillis(a.createdAt));
        return filters.limit !== undefined ? result.slice(0, filters.limit) : result;
    }
    async listRecent(limit) {
        return [...this.decisions.values()]
            .map((row) => ({ ...row }))
            .sort((a, b) => toMillis(b.createdAt) - toMillis(a.createdAt))
            .slice(0, Math.max(0, limit));
    }
};
InMemorySupervisorDecisionRepository = __decorate([
    injectable()
], InMemorySupervisorDecisionRepository);
export { InMemorySupervisorDecisionRepository };
