import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { notFound } from 'next/navigation';
import { resolve } from '../../lib/server-container.js';
import { AgentQuestionStatus } from '../../../../../packages/core/src/domain/generated/output.js';
import { getFeatureFlags } from '../../lib/feature-flags.js';
import { AgentQuestionsInbox } from '../../components/agent-questions/agent-questions-inbox.js';
/** DI-backed page; never pre-render. */
export const dynamic = 'force-dynamic';
export default async function AgentQuestionsRoute({ searchParams }) {
    const flags = getFeatureFlags();
    if (!flags.collaboration) {
        notFound();
    }
    const search = await searchParams;
    const requestedApp = search.app?.trim() ? search.app : undefined;
    const featureFilter = search.feature?.trim() ? search.feature : undefined;
    const statusFilter = parseStatus(search.status);
    const apps = await resolve('ListApplicationsUseCase').execute();
    const appIds = requestedApp ? [requestedApp] : apps.map((a) => a.id);
    const useCase = resolve('ListAgentQuestionsUseCase');
    const lists = await Promise.all(appIds.map((appId) => useCase
        .execute({
        appId,
        featureId: featureFilter,
        status: statusFilter,
    })
        .catch(() => [])));
    const questions = lists.flat();
    questions.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
    return (_jsxs("div", { className: "flex h-full flex-col gap-6 p-6", children: [_jsxs("header", { children: [_jsx("h1", { className: "text-2xl font-semibold", children: "Agent questions" }), _jsx("p", { className: "text-muted-foreground text-sm", children: "Unified inbox for questions raised by agents during interactive and background runs." })] }), _jsx(AgentQuestionsInbox, { initialQuestions: questions, initialStatusFilter: statusFilter ?? AgentQuestionStatus.pending })] }));
}
function parseStatus(value) {
    if (!value)
        return undefined;
    const allowed = [
        AgentQuestionStatus.pending,
        AgentQuestionStatus.answered,
        AgentQuestionStatus.cancelled,
        AgentQuestionStatus.expired,
    ];
    return allowed.find((s) => s === value);
}
