'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo } from 'react';
import { FileText, Terminal, MessageSquare, CheckCircle2, Coins, Server, FileCode, Play, ArrowRight, AlertCircle, } from 'lucide-react';
import { parseLogContent, parseToolCall, parseResultMessage, parseTokensMessage, } from '../../../lib/parse-log-line.js';
export function EventLogViewer({ content }) {
    const lines = useMemo(() => parseLogContent(content), [content]);
    if (lines.length === 0)
        return null;
    return (_jsx("div", { className: "flex flex-col", children: lines.map((line, i) => (
        // eslint-disable-next-line react/no-array-index-key -- append-only list, raw content not unique
        _jsx(LogLineRow, { line: line }, i))) }));
}
/* ---------------------------------------------------------------------------
 * Line router
 * ------------------------------------------------------------------------- */
function LogLineRow({ line }) {
    switch (line.tag) {
        case 'tool':
            return _jsx(ToolCallRow, { line: line });
        case 'tool-result':
            return _jsx(ToolResultRow, { line: line });
        case 'text':
        case 'delta':
            return _jsx(TextRow, { line: line });
        case 'result':
            return _jsx(ResultRow, { line: line });
        case 'tokens':
        case 'turn':
            return _jsx(TokensRow, { line: line });
        case 'cmd':
            return _jsx(CommandRow, { line: line });
        case 'file':
            return _jsx(FileChangeRow, { line: line });
        case 'thread':
        case 'event':
            return _jsx(InfoRow, { line: line });
        case 'error':
            return _jsx(ErrorRow, { line: line });
        case 'worker':
            return _jsx(WorkerRow, { line: line });
        case 'info':
            return _jsx(InfoRow, { line: line });
        case 'raw':
        default:
            return _jsx(RawRow, { line: line });
    }
}
/* ---------------------------------------------------------------------------
 * Shared helpers
 * ------------------------------------------------------------------------- */
function Timestamp({ value }) {
    if (!value)
        return null;
    // Show only MM:SS — compact, hours rarely change in a session
    const full = value.replace(/^.*T/, '').replace('Z', '');
    const parts = full.split(':');
    const compact = parts.length >= 3 ? `${parts[1]}:${parts[2].split('.')[0]}` : full;
    return (_jsx("span", { className: "text-muted-foreground/60 w-11 shrink-0 font-mono text-[10px]", children: compact }));
}
function PhaseBadge({ phase }) {
    if (!phase)
        return _jsx("span", { className: "w-20 shrink-0" });
    return (_jsx("span", { className: "bg-muted text-muted-foreground w-20 shrink-0 truncate rounded px-1.5 py-0.5 font-mono text-[10px] font-medium", title: phase, children: phase }));
}
/* ---------------------------------------------------------------------------
 * Event-specific rows
 * ------------------------------------------------------------------------- */
function ToolCallRow({ line }) {
    const { toolName, args } = parseToolCall(line.message);
    // Try to extract a key detail from args for common tools
    const detail = getToolDetail(toolName, args);
    return (_jsxs("div", { className: "hover:bg-muted/50 flex items-start gap-2 border-b border-transparent px-3 py-1.5 transition-colors", children: [_jsx(Timestamp, { value: line.timestamp }), _jsx(PhaseBadge, { phase: line.phase }), _jsxs("div", { className: "flex min-w-0 flex-1 items-start gap-1.5", children: [_jsx(ToolIcon, { toolName: toolName }), _jsx("span", { className: "text-xs font-semibold text-violet-600 dark:text-violet-400", children: toolName }), detail ? (_jsx("span", { className: "text-muted-foreground min-w-0 font-mono text-xs break-all", children: detail })) : null] })] }));
}
function ToolIcon({ toolName }) {
    const lower = toolName.toLowerCase();
    if (lower === 'bash')
        return _jsx(Terminal, { className: "mt-0.5 h-3 w-3 shrink-0 text-violet-500" });
    if (lower === 'read' || lower === 'glob' || lower === 'grep')
        return _jsx(FileText, { className: "mt-0.5 h-3 w-3 shrink-0 text-violet-500" });
    if (lower === 'write' || lower === 'edit')
        return _jsx(FileCode, { className: "mt-0.5 h-3 w-3 shrink-0 text-violet-500" });
    return _jsx(Terminal, { className: "mt-0.5 h-3 w-3 shrink-0 text-violet-500" });
}
function getToolDetail(toolName, args) {
    try {
        const parsed = JSON.parse(args);
        if (toolName === 'Read' && parsed.file_path)
            return parsed.file_path;
        if (toolName === 'Write' && parsed.file_path)
            return parsed.file_path;
        if (toolName === 'Edit' && parsed.file_path)
            return parsed.file_path;
        if (toolName === 'Glob' && parsed.pattern)
            return parsed.pattern;
        if (toolName === 'Grep' && parsed.pattern)
            return parsed.pattern;
        if (toolName === 'Bash' && parsed.command) {
            return parsed.command;
        }
        if (toolName === 'Task' && parsed.description)
            return parsed.description;
    }
    catch {
        // Not JSON — show truncated raw args
        if (args.length > 0)
            return args;
    }
    return null;
}
function TextRow({ line }) {
    return (_jsxs("div", { className: "hover:bg-muted/50 flex items-start gap-2 px-3 py-1.5 transition-colors", children: [_jsx(Timestamp, { value: line.timestamp }), _jsx(PhaseBadge, { phase: line.phase }), _jsxs("div", { className: "flex min-w-0 flex-1 items-start gap-1.5", children: [_jsx(MessageSquare, { className: "mt-0.5 h-3 w-3 shrink-0 text-blue-500" }), _jsx("span", { className: "text-foreground/80 min-w-0 text-xs leading-relaxed", children: line.message })] })] }));
}
function ResultRow({ line }) {
    const { chars } = parseResultMessage(line.message);
    return (_jsxs("div", { className: "hover:bg-muted/50 flex items-start gap-2 px-3 py-1.5 transition-colors", children: [_jsx(Timestamp, { value: line.timestamp }), _jsx(PhaseBadge, { phase: line.phase }), _jsxs("div", { className: "flex min-w-0 flex-1 items-start gap-1.5", children: [_jsx(CheckCircle2, { className: "mt-0.5 h-3 w-3 shrink-0 text-emerald-500" }), _jsxs("span", { className: "text-xs font-medium text-emerald-600 dark:text-emerald-400", children: ["Result: ", chars.toLocaleString(), " chars"] })] })] }));
}
function TokensRow({ line }) {
    const { inputTokens, outputTokens } = parseTokensMessage(line.message);
    return (_jsxs("div", { className: "hover:bg-muted/50 flex items-start gap-2 px-3 py-1.5 transition-colors", children: [_jsx(Timestamp, { value: line.timestamp }), _jsx(PhaseBadge, { phase: line.phase }), _jsxs("div", { className: "flex min-w-0 flex-1 items-start gap-1.5", children: [_jsx(Coins, { className: "mt-0.5 h-3 w-3 shrink-0 text-amber-500" }), _jsxs("span", { className: "text-muted-foreground text-xs", children: [_jsx("span", { className: "text-amber-600 dark:text-amber-400", children: inputTokens.toLocaleString() }), ' in / ', _jsx("span", { className: "text-amber-600 dark:text-amber-400", children: outputTokens.toLocaleString() }), ' out'] })] })] }));
}
function WorkerRow({ line }) {
    return (_jsxs("div", { className: "hover:bg-muted/50 flex items-start gap-2 bg-zinc-50 px-3 py-1.5 transition-colors dark:bg-zinc-900/50", children: [_jsx(Timestamp, { value: line.timestamp }), _jsx(PhaseBadge, { phase: line.phase }), _jsxs("div", { className: "flex min-w-0 flex-1 items-start gap-1.5", children: [_jsx(Server, { className: "mt-0.5 h-3 w-3 shrink-0 text-zinc-500" }), _jsx("span", { className: "text-muted-foreground text-xs font-medium break-all", children: line.message })] })] }));
}
function ToolResultRow({ line }) {
    return (_jsxs("div", { className: "hover:bg-muted/50 flex items-start gap-2 px-3 py-1 transition-colors", children: [_jsx(Timestamp, { value: line.timestamp }), _jsx(PhaseBadge, { phase: line.phase }), _jsxs("div", { className: "flex min-w-0 flex-1 items-start gap-1.5", children: [_jsx(ArrowRight, { className: "mt-0.5 h-3 w-3 shrink-0 text-violet-400" }), _jsx("span", { className: "text-muted-foreground min-w-0 font-mono text-[11px] break-all", children: line.message })] })] }));
}
function CommandRow({ line }) {
    return (_jsxs("div", { className: "hover:bg-muted/50 flex items-start gap-2 px-3 py-1.5 transition-colors", children: [_jsx(Timestamp, { value: line.timestamp }), _jsx(PhaseBadge, { phase: line.phase }), _jsxs("div", { className: "flex min-w-0 flex-1 items-start gap-1.5", children: [_jsx(Play, { className: "mt-0.5 h-3 w-3 shrink-0 text-cyan-500" }), _jsx("span", { className: "min-w-0 font-mono text-xs break-all text-cyan-700 dark:text-cyan-400", children: line.message })] })] }));
}
function FileChangeRow({ line }) {
    return (_jsxs("div", { className: "hover:bg-muted/50 flex items-start gap-2 px-3 py-1.5 transition-colors", children: [_jsx(Timestamp, { value: line.timestamp }), _jsx(PhaseBadge, { phase: line.phase }), _jsxs("div", { className: "flex min-w-0 flex-1 items-start gap-1.5", children: [_jsx(FileCode, { className: "mt-0.5 h-3 w-3 shrink-0 text-orange-500" }), _jsx("span", { className: "min-w-0 text-xs break-all text-orange-700 dark:text-orange-400", children: line.message })] })] }));
}
function ErrorRow({ line }) {
    return (_jsxs("div", { className: "hover:bg-muted/50 flex items-start gap-2 bg-red-50 px-3 py-1.5 transition-colors dark:bg-red-950/30", children: [_jsx(Timestamp, { value: line.timestamp }), _jsx(PhaseBadge, { phase: line.phase }), _jsxs("div", { className: "flex min-w-0 flex-1 items-start gap-1.5", children: [_jsx(AlertCircle, { className: "mt-0.5 h-3 w-3 shrink-0 text-red-500" }), _jsx("span", { className: "min-w-0 text-xs font-medium break-all text-red-700 dark:text-red-400", children: line.message })] })] }));
}
function InfoRow({ line }) {
    return (_jsxs("div", { className: "hover:bg-muted/50 flex items-start gap-2 px-3 py-1.5 transition-colors", children: [_jsx(Timestamp, { value: line.timestamp }), _jsx(PhaseBadge, { phase: line.phase }), _jsx("span", { className: "text-muted-foreground min-w-0 text-xs break-all", children: line.message })] }));
}
function RawRow({ line }) {
    return (_jsx("div", { className: "px-3 py-0.5", children: _jsx("span", { className: "text-muted-foreground/70 font-mono text-[11px] break-all whitespace-pre-wrap", children: line.raw }) }));
}
