import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from 'ink';
import { theme } from '../../semantic-colors.js';
import Spinner from 'ink-spinner';
import { TOOL_STATUS } from '../../constants.js';
import { STATUS_INDICATOR_WIDTH } from './ToolShared.js';
const formatToolArgs = (args) => {
    if (!args)
        return '';
    try {
        const parsed = JSON.parse(args);
        if (typeof parsed !== 'object' || parsed === null) {
            return args;
        }
        if ('description' in parsed &&
            typeof parsed.description === 'string' &&
            parsed.description) {
            return parsed.description;
        }
        if ('command' in parsed && typeof parsed.command === 'string')
            return parsed.command;
        if ('file_path' in parsed && typeof parsed.file_path === 'string')
            return parsed.file_path;
        if ('dir_path' in parsed && typeof parsed.dir_path === 'string')
            return parsed.dir_path;
        if ('query' in parsed && typeof parsed.query === 'string')
            return parsed.query;
        if ('url' in parsed && typeof parsed.url === 'string')
            return parsed.url;
        if ('target' in parsed && typeof parsed.target === 'string')
            return parsed.target;
        return args;
    }
    catch {
        return args;
    }
};
export const SubagentProgressDisplay = ({ progress }) => {
    let headerText;
    let headerColor = theme.text.secondary;
    if (progress.state === 'cancelled') {
        headerText = `Subagent ${progress.agentName} was cancelled.`;
        headerColor = theme.status.warning;
    }
    else if (progress.state === 'error') {
        headerText = `Subagent ${progress.agentName} failed.`;
        headerColor = theme.status.error;
    }
    else if (progress.state === 'completed') {
        headerText = `Subagent ${progress.agentName} completed.`;
        headerColor = theme.status.success;
    }
    return (_jsxs(Box, { flexDirection: "column", paddingY: 0, children: [headerText && (_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: headerColor, italic: true, children: headerText }) })), _jsx(Box, { flexDirection: "column", marginLeft: 0, gap: 0, children: progress.recentActivity.map((item) => {
                    if (item.type === 'thought') {
                        const isCancellation = item.content === 'Request cancelled.';
                        const icon = isCancellation ? 'ℹ ' : '💭';
                        const color = isCancellation
                            ? theme.status.warning
                            : theme.text.secondary;
                        return (_jsxs(Box, { flexDirection: "row", children: [_jsx(Box, { minWidth: STATUS_INDICATOR_WIDTH, children: _jsx(Text, { color: color, children: icon }) }), _jsx(Box, { flexGrow: 1, children: _jsx(Text, { color: color, children: item.content }) })] }, item.id));
                    }
                    else if (item.type === 'tool_call') {
                        const statusSymbol = item.status === 'running' ? (_jsx(Spinner, { type: "dots" })) : item.status === 'completed' ? (_jsx(Text, { color: theme.status.success, children: TOOL_STATUS.SUCCESS })) : item.status === 'cancelled' ? (_jsx(Text, { color: theme.status.warning, bold: true, children: TOOL_STATUS.CANCELED })) : (_jsx(Text, { color: theme.status.error, children: TOOL_STATUS.ERROR }));
                        const formattedArgs = item.description || formatToolArgs(item.args);
                        const displayArgs = formattedArgs.length > 60
                            ? formattedArgs.slice(0, 60) + '...'
                            : formattedArgs;
                        return (_jsxs(Box, { flexDirection: "row", children: [_jsx(Box, { minWidth: STATUS_INDICATOR_WIDTH, children: statusSymbol }), _jsxs(Box, { flexDirection: "row", flexGrow: 1, flexWrap: "wrap", children: [_jsx(Text, { bold: true, color: theme.text.primary, strikethrough: item.status === 'cancelled', children: item.displayName || item.content }), displayArgs && (_jsx(Box, { marginLeft: 1, children: _jsx(Text, { color: theme.text.secondary, wrap: "truncate", strikethrough: item.status === 'cancelled', children: displayArgs }) }))] })] }, item.id));
                    }
                    return null;
                }) })] }));
};
//# sourceMappingURL=SubagentProgressDisplay.js.map