/**
 * Message Mapper
 *
 * Maps SDK message types to the existing SpawnedSessionEvent format
 * for backward compatibility with the orchestrator's event handling.
 *
 * SDK Message Type Mappings:
 * | SDK Message Type      | Current Event Type |
 * |-----------------------|-------------------|
 * | SDKAssistantMessage   | assistant         |
 * | SDKUserMessage        | user              |
 * | tool_use content      | tool_use          |
 * | tool_result content   | tool_result       |
 * | SDKSystemMessage      | system            |
 * | SDKResultMessage      | result            |
 *
 * @module
 */
import { createTimestamp } from '@stoneforge/core';
// ============================================================================
// Mapper Functions
// ============================================================================
/**
 * Maps an SDK message to a SpawnedSessionEvent.
 *
 * @param sdkMessage - The SDK message to map
 * @returns The mapped SpawnedSessionEvent
 */
export function mapSDKMessageToEvent(sdkMessage) {
    const receivedAt = createTimestamp();
    switch (sdkMessage.type) {
        case 'assistant':
            return mapAssistantMessage(sdkMessage, receivedAt);
        case 'user':
            return mapUserMessage(sdkMessage, receivedAt);
        case 'system':
            return mapSystemMessage(sdkMessage, receivedAt);
        case 'result':
            return mapResultMessage(sdkMessage, receivedAt);
        case 'error':
            return mapErrorMessage(sdkMessage, receivedAt);
        default:
            // Unknown message type - pass through as raw
            return {
                type: (sdkMessage.type || 'system'),
                receivedAt,
                raw: sdkMessage,
            };
    }
}
/**
 * Maps an SDK assistant message to SpawnedSessionEvent.
 * Assistant messages may contain text or tool_use blocks.
 */
function mapAssistantMessage(message, receivedAt) {
    // If content is a string, it's simple text
    if (typeof message.content === 'string') {
        return {
            type: 'assistant',
            receivedAt,
            raw: {
                type: 'assistant',
                message: message.content,
            },
            message: message.content,
        };
    }
    // Content is an array of content blocks
    const blocks = message.content;
    // Find text content
    const textBlocks = blocks.filter((b) => b.type === 'text');
    const textContent = textBlocks.map((b) => b.text || '').join('\n');
    // Find tool_use blocks
    const toolUseBlocks = blocks.filter((b) => b.type === 'tool_use');
    if (toolUseBlocks.length > 0) {
        // Return tool_use event for the first tool use block
        // (In practice, Claude typically uses one tool at a time)
        const toolBlock = toolUseBlocks[0];
        return {
            type: 'tool_use',
            receivedAt,
            raw: {
                type: 'tool_use',
                tool: toolBlock.name,
                tool_use_id: toolBlock.id,
                tool_input: toolBlock.input,
                message: textContent || undefined,
            },
            message: textContent || undefined,
            tool: {
                name: toolBlock.name,
                id: toolBlock.id,
                input: toolBlock.input,
            },
        };
    }
    // No tool use, return as assistant message
    return {
        type: 'assistant',
        receivedAt,
        raw: {
            type: 'assistant',
            message: textContent,
        },
        message: textContent,
    };
}
/**
 * Maps an SDK user message to SpawnedSessionEvent.
 */
function mapUserMessage(message, receivedAt) {
    return {
        type: 'user',
        receivedAt,
        raw: {
            type: 'user',
            message: message.content,
        },
        message: message.content,
    };
}
/**
 * Maps an SDK system message to SpawnedSessionEvent.
 */
function mapSystemMessage(message, receivedAt) {
    return {
        type: 'system',
        subtype: message.subtype,
        receivedAt,
        raw: {
            type: 'system',
            subtype: message.subtype,
            session_id: message.session_id,
            message: message.message,
        },
        message: message.message,
    };
}
/**
 * Maps an SDK result message to SpawnedSessionEvent.
 */
function mapResultMessage(message, receivedAt) {
    return {
        type: 'result',
        receivedAt,
        raw: {
            type: 'result',
            result: message.result,
        },
        message: message.result,
    };
}
/**
 * Maps an SDK error message to SpawnedSessionEvent.
 */
function mapErrorMessage(message, receivedAt) {
    return {
        type: 'error',
        receivedAt,
        raw: {
            type: 'error',
            error: message.error,
            message: message.message,
        },
        message: message.message || message.error,
    };
}
// ============================================================================
// Content Block Mappers
// ============================================================================
/**
 * Maps a tool_result content block to SpawnedSessionEvent.
 */
export function mapToolResultToEvent(toolUseId, content, isError = false, receivedAt = createTimestamp()) {
    return {
        type: 'tool_result',
        receivedAt,
        raw: {
            type: 'tool_result',
            tool_use_id: toolUseId,
            content,
            is_error: isError,
        },
        message: content,
        tool: {
            id: toolUseId,
        },
    };
}
// ============================================================================
// Batch Processing
// ============================================================================
/**
 * Maps an array of SDK messages to SpawnedSessionEvents.
 *
 * @param messages - Array of SDK messages
 * @returns Array of mapped events
 */
export function mapSDKMessagesToEvents(messages) {
    return messages.map(mapSDKMessageToEvent);
}
//# sourceMappingURL=message-mapper.js.map