'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo } from 'react';
import { Badge } from '../ui/badge.js';
import { Alert, AlertDescription } from '../ui/alert.js';
import { useAgentMessages } from '../../hooks/agent-events-provider.js';
import { AgentMessageKind } from '../../../../../packages/core/src/domain/generated/output.js';
const KIND_VARIANT = {
    [AgentMessageKind.status]: 'secondary',
    [AgentMessageKind.request]: 'default',
    [AgentMessageKind.reply]: 'default',
    [AgentMessageKind.blocked]: 'destructive',
    [AgentMessageKind.info]: 'secondary',
};
export function AgentActivityFeed({ scopeType, scopeId, featureId, initialMessages = [], overrideMessages, errorMessage = null, limit = 100, }) {
    const live = useAgentMessages({ scopeType, scopeId, featureId });
    const liveMessages = overrideMessages ?? live.messages;
    const merged = useMemo(() => {
        const byId = new Map();
        for (const m of initialMessages)
            byId.set(m.messageId, m);
        for (const m of liveMessages)
            byId.set(m.messageId, m);
        const all = [...byId.values()];
        all.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
        return all.slice(-limit);
    }, [initialMessages, liveMessages, limit]);
    if (errorMessage) {
        return (_jsx(Alert, { variant: "destructive", "data-testid": "agent-activity-error", children: _jsx(AlertDescription, { children: errorMessage }) }));
    }
    if (merged.length === 0) {
        return (_jsx("div", { "data-testid": "agent-activity-empty", className: "text-muted-foreground rounded border border-dashed p-4 text-center text-sm", children: "No agent activity yet for this feature." }));
    }
    return (_jsx("ol", { className: "flex flex-col gap-2", "data-testid": "agent-activity-feed", "aria-label": "Agent activity feed", children: merged.map((message) => (_jsx(AgentMessageRow, { message: message }, message.messageId))) }));
}
function AgentMessageRow({ message }) {
    const variant = KIND_VARIANT[message.messageKind] ?? 'secondary';
    const preview = previewPayload(message.payload);
    return (_jsxs("li", { className: "flex flex-col gap-1 rounded border p-3", "data-testid": `agent-message-row-${message.messageId}`, children: [_jsxs("header", { className: "flex items-center justify-between gap-2", children: [_jsxs("div", { className: "flex items-center gap-2", children: [_jsx(Badge, { variant: variant, className: "capitalize", children: message.messageKind }), _jsx("span", { className: "text-muted-foreground text-xs", "data-testid": `agent-message-from-${message.messageId}`, children: message.fromActor }), _jsx("span", { className: "text-muted-foreground text-xs", children: "\u2192" }), _jsx("span", { className: "text-muted-foreground text-xs", children: message.toKind === 'broadcast' ? 'broadcast' : message.toTarget })] }), _jsx("time", { className: "text-muted-foreground text-xs", children: formatTime(message.createdAt) })] }), _jsx("p", { className: "font-mono text-xs whitespace-pre-wrap", "data-testid": `agent-message-payload-${message.messageId}`, children: preview })] }));
}
function formatTime(iso) {
    try {
        return new Date(iso).toLocaleTimeString();
    }
    catch {
        return iso;
    }
}
function previewPayload(payload) {
    if (!payload)
        return '';
    try {
        const parsed = JSON.parse(payload);
        return JSON.stringify(parsed, null, 2);
    }
    catch {
        return payload;
    }
}
