"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseCodex = parseCodex;
exports.parseCodexExit = parseCodexExit;
// Anchored to the literal `ERROR:` prefix codex emits. The loose
// alternation /upgrade to plus/i / /try again at/i without an anchor
// would false-match prompts that legitimately echo those phrases (codex
// `exec` echoes the user prompt back into stderr, so a code review brief
// mentioning "try again at midnight" was a real hazard). Round-1
// review-only dogfood (PR #9) flagged this.
const CODEX_QUOTA_LINE = /ERROR:[^\n]*(usage limit|upgrade to plus|try again at)/i;
function looksLikeCodexQuota(text) {
    return CODEX_QUOTA_LINE.test(text);
}
function parseCodex(_line) {
    return [];
}
function parseCodexExit(fullStdout, fullStderr = '', code = 0) {
    const stdoutTrimmed = fullStdout.trim();
    if (code === 0 && stdoutTrimmed.length > 0) {
        return [{ type: 'message_done', finalText: fullStdout }];
    }
    if (looksLikeCodexQuota(fullStderr) || looksLikeCodexQuota(fullStdout)) {
        // Pull the literal ERROR line for a usable message; fall back to a
        // truncated tail so we never lose the signal.
        const errorLine = [fullStderr, fullStdout]
            .flatMap((s) => s.split('\n'))
            .find((l) => /ERROR:.*usage limit/i.test(l))
            ?.trim() ?? 'codex usage limit reached';
        return [
            {
                type: 'error',
                kind: 'quota_exhausted',
                message: errorLine,
            },
        ];
    }
    if (code !== null && code !== 0) {
        const tail = (fullStderr.trim() || fullStdout.trim()).slice(-300);
        return [
            {
                type: 'error',
                kind: 'cli_error',
                message: tail.length > 0 ? tail : `codex exited ${code} with no output`,
            },
        ];
    }
    // code===0 + empty stdout — preserve old "emit nothing" behavior.
    return [];
}
//# sourceMappingURL=codex.js.map