import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { memo } from 'react';
import { Box, Text } from 'ink';
import { ClientSharedSessionTurnPresentationService } from '../../client-shared/services/session-turn-presentation/index.js';
import { AssistantMarkdown } from './AssistantMarkdown.js';
import { ConversationTurnActivityBlock } from './ConversationTurnActivityBlock.js';
const MAX_VISIBLE_TIMELINE_ITEMS = 14;
export const ConversationPanel = memo(function ConversationPanel({ activityExpanded = false, runtimeContext, session, }) {
    const timelineItems = ClientSharedSessionTurnPresentationService
        .projectConversationTimeline(session)
        .slice(-MAX_VISIBLE_TIMELINE_ITEMS);
    const showWelcome = Boolean(session && runtimeContext?.welcomeGuide && !session.messages.some((message) => message.role === 'user'));
    return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Text, { bold: true, children: "Conversation" }), _jsx(WelcomeGuideSlot, { show: showWelcome, runtimeContext: runtimeContext }), _jsx(EmptyConversationSlot, { show: timelineItems.length === 0 && !showWelcome }), timelineItems.map((item, index) => (_jsx(ConversationTimelineItemView, { activityExpanded: activityExpanded, item: item, last: index === timelineItems.length - 1 }, item.id)))] }));
});
function ConversationTimelineItemView({ activityExpanded, item, last, }) {
    if (item.type === 'turn_activity_group') {
        return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Text, { dimColor: true, children: "\u250C Activity" }), _jsx(Box, { paddingLeft: 2, flexDirection: "column", children: _jsx(ConversationTurnActivityBlock, { expanded: activityExpanded, item: item }) }), _jsx(TimelineFooter, { last: last })] }));
    }
    return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Text, { dimColor: true, children: resolveMessageLabel(item.message) }), _jsx(Box, { paddingLeft: 2, flexDirection: "column", children: _jsx(MessageBody, { message: item.message }) }), _jsx(TimelineFooter, { last: last })] }));
}
function TimelineFooter({ last }) {
    return _jsx(Text, { dimColor: true, children: last ? '└' : '└────────────────────────────────────────────────────────' });
}
function WelcomeGuideSlot({ runtimeContext, show, }) {
    if (!show || !runtimeContext?.welcomeGuide) {
        return null;
    }
    return _jsx(WelcomeGuide, { runtimeContext: runtimeContext });
}
function EmptyConversationSlot({ show }) {
    return show ? _jsx(Text, { dimColor: true, children: "No messages yet." }) : null;
}
function resolveMessageLabel(message) {
    if (message.role !== 'user') {
        return '┌ Heddle';
    }
    return '┌ You';
}
function MessageBody({ message }) {
    if (message.directShellResult) {
        return _jsx(DirectShellResult, { result: message.directShellResult });
    }
    if (message.role === 'assistant') {
        return _jsx(AssistantMarkdown, { children: message.text });
    }
    return message.text.split(/\r?\n/).map((line, lineIndex) => (_jsx(Text, { color: "cyan", children: line || ' ' }, `${message.id}-${lineIndex}`)));
}
function DirectShellResult({ result }) {
    return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Text, { children: [_jsx(Text, { color: result.outcome === 'done' ? 'green' : 'red', children: result.outcome }), _jsx(Text, { dimColor: true, children: " shell " }), _jsx(Text, { children: result.command })] }), result.policy?.reason ? _jsx(Text, { dimColor: true, children: result.policy.reason }) : null, result.stdout ? _jsx(OutputBlock, { label: "stdout", value: result.stdout }) : null, result.stderr ? _jsx(OutputBlock, { label: "stderr", value: result.stderr }) : null, result.error ? _jsx(OutputBlock, { label: "error", value: result.error }) : null] }));
}
function OutputBlock({ label, value }) {
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Text, { dimColor: true, children: label }), value.split(/\r?\n/).map((line, index) => (_jsx(Text, { children: line || ' ' }, `${label}-${index}`)))] }));
}
function WelcomeGuide({ runtimeContext }) {
    return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Text, { dimColor: true, children: "\u250C Heddle" }), _jsxs(Box, { paddingLeft: 2, flexDirection: "column", children: [_jsx(Text, { children: "Heddle conversational mode." }), _jsx(Text, { children: " " }), _jsx(Text, { children: "Ask a question about this workspace." }), _jsx(WelcomeTranscriptHint, { runtimeContext: runtimeContext }), _jsx(WelcomeCredentialWarning, { runtimeContext: runtimeContext })] }), _jsx(Text, { dimColor: true, children: "\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500" })] }));
}
function WelcomeTranscriptHint({ runtimeContext }) {
    return runtimeContext.welcomeGuide.carriesTranscriptAcrossTurns ? (_jsx(Text, { children: "Each turn runs the current agent loop and carries the transcript into the next turn." })) : null;
}
function WelcomeCredentialWarning({ runtimeContext }) {
    return runtimeContext.welcomeGuide.hasProviderCredential ? null : (_jsx(Text, { color: "yellow", children: "No provider credential detected. Use /auth login openai or set a provider API key." }));
}
//# sourceMappingURL=ConversationPanel.js.map