/**
 * In-Memory AgentQuestion Repository
 *
 * Test-friendly adapter for {@link IAgentQuestionRepository}. Enforces
 * appId scoping at the entry point.
 */
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';
import { AgentQuestionStatus as AgentQuestionStatusEnum } from '../../../domain/generated/output.js';
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 InMemoryAgentQuestionRepository = class InMemoryAgentQuestionRepository {
    questions = new Map();
    async create(question) {
        if (this.questions.has(question.id)) {
            throw new Error(`AgentQuestion with id "${question.id}" already exists`);
        }
        this.questions.set(question.id, { ...question });
    }
    async findById(appId, id) {
        const row = this.questions.get(id);
        if (!row || row.appId !== appId)
            return null;
        return { ...row };
    }
    async listByScope(appId, featureId, filters = {}) {
        const result = [];
        for (const row of this.questions.values()) {
            if (row.appId !== appId)
                continue;
            if (featureId !== undefined && row.featureId !== featureId)
                continue;
            if (filters.status !== undefined && row.status !== filters.status)
                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 listByAgentRun(appId, agentRunId) {
        const result = [];
        for (const row of this.questions.values()) {
            if (row.appId !== appId)
                continue;
            if (row.agentRunId !== agentRunId)
                continue;
            result.push({ ...row });
        }
        result.sort((a, b) => toMillis(b.createdAt) - toMillis(a.createdAt));
        return result;
    }
    async updateStatus(appId, id, status, fields = {}) {
        const row = this.questions.get(id);
        if (!row || row.appId !== appId)
            return;
        const now = new Date();
        this.questions.set(id, {
            ...row,
            status,
            ...(fields.answer !== undefined ? { answer: fields.answer } : {}),
            ...(fields.answeredBy !== undefined ? { answeredBy: fields.answeredBy } : {}),
            ...(fields.answeredAt !== undefined ? { answeredAt: fields.answeredAt } : {}),
            updatedAt: now,
        });
    }
    async findExpired(cutoff, limit) {
        const cutoffMillis = cutoff.getTime();
        const result = [];
        for (const row of this.questions.values()) {
            if (row.status !== AgentQuestionStatusEnum.pending)
                continue;
            if (!row.expiresAt)
                continue;
            if (toMillis(row.expiresAt) > cutoffMillis)
                continue;
            result.push({ ...row });
        }
        result.sort((a, b) => toMillis(a.expiresAt) - toMillis(b.expiresAt));
        return limit !== undefined ? result.slice(0, limit) : result;
    }
};
InMemoryAgentQuestionRepository = __decorate([
    injectable()
], InMemoryAgentQuestionRepository);
export { InMemoryAgentQuestionRepository };
