/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { ToolCallStatus, } from '../types.js';
function safeToolResultToString(result) {
    if (typeof result === 'string') {
        return result;
    }
    try {
        return JSON.stringify(result, null, 2);
    }
    catch {
        return String(result);
    }
}
/**
 * Converts provider-agnostic IContent[] (from session recording) into
 * UI HistoryItem[] for display.  Only block types renderable in the CLI
 * are converted — MediaBlock is intentionally omitted because the CLI UI
 * does not render inline images or file attachments.
 */
export function iContentToHistoryItems(contents) {
    const items = [];
    // Negative IDs avoid collisions with live IDs (always positive).
    let idCounter = -1;
    const responseMap = new Map();
    for (const content of contents) {
        if (content.speaker !== 'tool')
            continue;
        for (const block of content.blocks) {
            if (block.type === 'tool_response') {
                responseMap.set(block.callId, {
                    result: block.result,
                    error: block.error,
                });
            }
        }
    }
    for (const content of contents) {
        if (content.speaker === 'human') {
            const text = content.blocks
                .filter((b) => b.type === 'text')
                .map((b) => b.text)
                .join('\n');
            if (text) {
                items.push({ id: idCounter--, type: 'user', text });
            }
            continue;
        }
        if (content.speaker === 'ai') {
            const textParts = [];
            const thinkingBlocks = [];
            const toolCallBlocks = [];
            for (const block of content.blocks) {
                switch (block.type) {
                    case 'text':
                        textParts.push(block.text);
                        break;
                    case 'code':
                        textParts.push(`\`\`\`${block.language ?? ''}\n${block.code}\n\`\`\``);
                        break;
                    case 'thinking':
                        thinkingBlocks.push(block);
                        break;
                    case 'tool_call':
                        toolCallBlocks.push(block);
                        break;
                    default:
                        break;
                }
            }
            const combinedText = textParts.join('\n');
            if (combinedText) {
                items.push({
                    id: idCounter--,
                    type: 'gemini',
                    text: combinedText,
                    model: content.metadata?.model,
                    ...(thinkingBlocks.length > 0 ? { thinkingBlocks } : {}),
                });
            }
            if (toolCallBlocks.length > 0) {
                const tools = toolCallBlocks.map((tc) => {
                    const response = responseMap.get(tc.id);
                    return {
                        callId: tc.id,
                        name: tc.name,
                        description: tc.description ?? tc.name,
                        resultDisplay: response
                            ? safeToolResultToString(response.result)
                            : undefined,
                        status: response
                            ? response.error
                                ? ToolCallStatus.Error
                                : ToolCallStatus.Success
                            : ToolCallStatus.Pending,
                        confirmationDetails: undefined,
                    };
                });
                items.push({ id: idCounter--, type: 'tool_group', tools });
            }
        }
    }
    return items;
}
//# sourceMappingURL=iContentToHistoryItems.js.map