"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseClaude = parseClaude;
const shared_js_1 = require("./shared.js");
function parseClaude(line) {
    const obj = (0, shared_js_1.tryJson)(line);
    if (!obj || typeof obj !== 'object')
        return [];
    const t = obj.type;
    // Final result line — emit message_done with the assembled text +
    // usage. Claude Code reports `total_cost_usd` (list-price equivalent
    // even on a Pro/Max plan) and a `usage` block with input/output token
    // counts. Without lifting these, every Claude reviewer landed in
    // _stats.json with usage:undefined and the home page's plan-equivalent
    // spend silently lost the largest contributor (Claude is most users'
    // primary voice). The user pays $0 for these on subscription, but the
    // shadow column is the whole point of "plan equiv" — show what the
    // sub is saving them.
    if (t === 'result') {
        const subtype = obj.subtype;
        const isError = obj.is_error;
        if (subtype === 'success' && !isError) {
            const usageBlock = obj.usage;
            const costUsd = typeof obj.total_cost_usd === 'number' ? obj.total_cost_usd : undefined;
            const inputTokens = usageBlock && typeof usageBlock.input_tokens === 'number'
                ? usageBlock.input_tokens
                : undefined;
            const outputTokens = usageBlock && typeof usageBlock.output_tokens === 'number'
                ? usageBlock.output_tokens
                : undefined;
            const cachedInputTokens = usageBlock && typeof usageBlock.cache_read_input_tokens === 'number'
                ? usageBlock.cache_read_input_tokens
                : undefined;
            const usage = costUsd !== undefined ||
                inputTokens !== undefined ||
                outputTokens !== undefined
                ? {
                    ...(inputTokens !== undefined ? { inputTokens } : {}),
                    ...(outputTokens !== undefined ? { outputTokens } : {}),
                    ...(cachedInputTokens !== undefined ? { cachedInputTokens } : {}),
                    ...(costUsd !== undefined ? { costUsd } : {}),
                }
                : undefined;
            return [
                {
                    type: 'message_done',
                    finalText: String(obj.result ?? ''),
                    ...(usage ? { usage } : {}),
                },
            ];
        }
        return [
            {
                type: 'error',
                kind: 'claude_result_error',
                message: String(obj.result ?? obj.api_error_status ?? 'Claude reported error'),
            },
        ];
    }
    if (t === 'stream_event') {
        const event = obj.event ?? {};
        const eventType = event.type;
        if (eventType === 'content_block_delta') {
            const delta = event.delta ?? {};
            if (delta.type === 'text_delta' && typeof delta.text === 'string') {
                return [{ type: 'text_delta', text: delta.text }];
            }
            // Tool input deltas (input_json_delta) aren't surfaced — the
            // tool_call_start carries the initial input snapshot which is
            // enough for UI ("called Read on /path/x.ts").
            return [];
        }
        if (eventType === 'content_block_start') {
            const block = event.content_block ?? {};
            if (block.type === 'tool_use') {
                return [
                    {
                        type: 'tool_call_start',
                        tool: typeof block.name === 'string' ? block.name : 'unknown',
                        input: block.input,
                    },
                ];
            }
            return [];
        }
        // We don't emit tool_call_end from Claude's stream — Claude emits a
        // tool_result message later that we'd need to track separately.
        // Skipping for now; UI shows tool_call_start in the trace, which is
        // enough for live progress.
        return [];
    }
    // System (init, hook events, status), assistant (assembled message),
    // rate_limit_event — silently ignored. Future: surface
    // rate_limit_event into the cli-health module so cockpit can show
    // "Claude resets at <time>" without waiting for a quota_exhausted.
    return [];
}
//# sourceMappingURL=claude.js.map