/**
 * Compose the user `AgentMessage` from the visible query plus per-turn
 * enrichments. Block 0 is always the user query; later blocks are LLM-only
 * context in fixed order.
 *
 * Pure boundary: no I/O, no global reads, and no dependency on runtime message
 * state. The caller supplies the context-warning eligibility boolean.
 */
import { buildUploadsBlock } from './uploads.js';
import { buildContextUpdateWarningBlock, buildFocusedRangeBlock, buildRangeRestrictionsBlock, buildWorkbookSummaryBlock } from './user-turn-sections.js';
/**
 * Compose the `UserAgentMessage`.
 *
 * Block ordering (matches backend):
 *   0. query (always)
 *   1. workbook_summary (when present)
 *   2. range_restrictions OR focused_range (precedence: restrictions when non-empty)
 *   3. uploads (when present)
 *   4. context_update_warning (when contextWarningEligible)
 *
 * Images (when provided) are appended to block 0's position group — they're not
 * enrichments and stay on the user-visible side. They sit AFTER the query block
 * but the snapshot projection still reads `content[0].text` only, so users see
 * their typed text and the LLM sees text + images + enrichments in order.
 */
export function buildUserMessage(input) {
    const content = [{ type: 'text', text: input.query }];
    const timestamp = input.timestamp ?? Date.now();
    if (input.images && input.images.length > 0) {
        content.push(...input.images);
    }
    const ws = buildWorkbookSummaryBlock(input.workbookSummary);
    if (ws)
        content.push(ws);
    // Range restrictions take precedence over focused range.
    if (input.rangeRestrictions && input.rangeRestrictions.length > 0) {
        const rr = buildRangeRestrictionsBlock(input.rangeRestrictions);
        if (rr)
            content.push(rr);
    }
    else if (input.focusedRange) {
        const fr = buildFocusedRangeBlock(input.focusedRange);
        if (fr)
            content.push(fr);
    }
    const up = buildUploadsBlock(input.fileUploads);
    if (up)
        content.push(up);
    if (input.contextWarningEligible) {
        content.push(buildContextUpdateWarningBlock());
    }
    const message = {
        ...(input.userMessageId !== undefined ? { id: input.userMessageId } : {}),
        role: 'user',
        content,
        timestamp,
        uiMetadata: {
            createdAt: timestamp,
            ...(input.interactionMode !== undefined ? { interactionMode: input.interactionMode } : {}),
            ...(input.fileUploads !== undefined ? { fileUploads: input.fileUploads } : {}),
            ...(input.focusedRange !== undefined ? { focusedRange: input.focusedRange } : {}),
            ...(input.rangeRestrictions !== undefined
                ? { rangeRestrictions: input.rangeRestrictions }
                : {})
        }
    };
    if (input.contextReset === true) {
        message.contextResetBoundary = true;
    }
    return message;
}
//# sourceMappingURL=build-user-message.js.map