import { parseStructuredOutput } from '../../shared/utils/index.js';
import { buildRateLimitedResponseFields, containsRateLimitError, containsRateLimitMarker } from '../rate-limit/detection.js';
function buildNoContentMessage(stdout, stderr) {
    const hint = stderr.trim() || stdout.trim().slice(0, 500) || 'no parseable stream-json output';
    return `Claude CLI returned no assistant text. ${hint}`;
}
function hasCompatibilityDisplayText(parsed) {
    return !parsed.hasResult && parsed.displayText.length > 0;
}
function emitResultEvent(onStream, payload) {
    if (!onStream) {
        return;
    }
    onStream({
        type: 'result',
        data: payload,
    });
}
function findRateLimitMarkerText(parsed, stdout) {
    const candidates = [parsed.content, parsed.displayText, stdout];
    return candidates.find((candidate) => containsRateLimitMarker(candidate));
}
export function buildClaudeHeadlessResponse(input) {
    const { agentName, parsed, stdout, stderr, sessionId, outputSchema, onStream } = input;
    const content = parsed.content;
    const structuredOutput = parsed.structuredOutput ?? parseStructuredOutput(content, !!outputSchema);
    const resolvedSessionId = sessionId ?? '';
    const compatibilitySuccess = hasCompatibilityDisplayText(parsed);
    const rateLimitMarkerText = findRateLimitMarkerText(parsed, stdout);
    if (rateLimitMarkerText) {
        emitResultEvent(onStream, {
            result: '',
            success: false,
            error: rateLimitMarkerText,
            sessionId: resolvedSessionId,
        });
        return {
            persona: agentName,
            timestamp: new Date(),
            sessionId,
            ...buildRateLimitedResponseFields('claude', 'stream_marker', rateLimitMarkerText),
        };
    }
    if (!parsed.hasResult && !compatibilitySuccess) {
        const message = buildNoContentMessage(stdout, stderr);
        emitResultEvent(onStream, {
            result: '',
            success: false,
            error: message,
            sessionId: resolvedSessionId,
        });
        return {
            persona: agentName,
            status: 'error',
            content: message,
            timestamp: new Date(),
            sessionId,
            error: message,
        };
    }
    if (parsed.hasResult && !parsed.success) {
        const message = parsed.error ?? buildNoContentMessage(stdout, stderr);
        if (containsRateLimitError(message)) {
            emitResultEvent(onStream, {
                result: '',
                success: false,
                error: message,
                sessionId: resolvedSessionId,
            });
            return {
                persona: agentName,
                timestamp: new Date(),
                sessionId,
                ...buildRateLimitedResponseFields('claude', 'error_text', message || content),
            };
        }
        emitResultEvent(onStream, {
            result: content,
            success: false,
            error: message,
            sessionId: resolvedSessionId,
        });
        return {
            persona: agentName,
            status: 'error',
            content: content || message,
            timestamp: new Date(),
            sessionId,
            error: message,
        };
    }
    emitResultEvent(onStream, {
        result: content,
        success: true,
        sessionId: resolvedSessionId,
    });
    return {
        persona: agentName,
        status: 'done',
        content,
        timestamp: new Date(),
        sessionId,
        structuredOutput,
    };
}
//# sourceMappingURL=result-response.js.map