import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from 'ink';
import { useEffect, useState } from 'react';
import ToolMessage from '../components/tool-message.js';
import { TRUNCATION_OUTPUT_LIMIT } from '../constants.js';
import { useTheme } from '../hooks/useTheme.js';
import { bashExecutor } from '../services/bash-executor.js';
import { calculateTokens } from '../utils/token-calculator.js';
export default function BashProgress({ executionId, command, completedState, isLive = false, }) {
    const { colors } = useTheme();
    // If completedState is provided, use it directly (static mode)
    const [state, setState] = useState(completedState ?? {
        executionId,
        command,
        outputPreview: '',
        fullOutput: '',
        stderr: '',
        isComplete: false,
        exitCode: null,
        error: null,
    });
    // Subscribe to bash executor events (only if not in static mode)
    useEffect(() => {
        // Skip event subscription if we have a completed state
        if (completedState)
            return;
        const handleUpdate = (update) => {
            if (update.executionId === executionId) {
                setState(update);
            }
        };
        bashExecutor.on('start', handleUpdate);
        bashExecutor.on('progress', handleUpdate);
        bashExecutor.on('complete', handleUpdate);
        // Get initial state if execution already started
        const initialState = bashExecutor.getState(executionId);
        if (initialState) {
            setState(initialState);
        }
        return () => {
            bashExecutor.off('start', handleUpdate);
            bashExecutor.off('progress', handleUpdate);
            bashExecutor.off('complete', handleUpdate);
        };
    }, [executionId, completedState]);
    // Escape-to-cancel for a running bash command is owned by the single
    // section-level handler in InteractiveApp. execute_bash is the only entry
    // point for bash and it always sets isToolExecuting + an abort controller,
    // so the global handler reliably tears this command down via handleCancel.
    // Determine dot color
    let dotColor = colors.secondary;
    if (state.isComplete) {
        dotColor =
            state.exitCode === 0 && !state.error ? colors.success : colors.error;
    }
    // Calculate output stats for completed state (use truncated size to match what LLM receives)
    const totalOutput = state.fullOutput + state.stderr;
    const truncatedOutput = totalOutput.length > TRUNCATION_OUTPUT_LIMIT
        ? totalOutput.substring(0, TRUNCATION_OUTPUT_LIMIT)
        : totalOutput;
    const estimatedTokens = calculateTokens(truncatedOutput);
    const messageContent = (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: colors.tool, children: "\u2692 execute_bash" }), _jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: colors.secondary, children: "Command:" }), _jsx(Text, { wrap: "wrap", color: colors.primary, children: command })] }), state.isComplete && (_jsxs(Box, { children: [_jsx(Text, { color: colors.secondary, children: "Status: " }), _jsx(Text, { color: dotColor, children: "\u25CF" })] })), !state.isComplete && state.outputPreview && (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: colors.secondary, children: "Output: " }), _jsx(Text, { color: colors.text, children: state.outputPreview })] })), state.isComplete && (_jsxs(Box, { children: [_jsx(Text, { color: colors.secondary, children: "Tokens: " }), _jsxs(Text, { color: colors.text, children: ["~", estimatedTokens] })] }))] }));
    return (_jsx(ToolMessage, { message: messageContent, hideBox: true, isLive: isLive }));
}
//# sourceMappingURL=bash-progress.js.map