import { execa } from 'execa';
import { DEFAULT_REVIEW_INSTRUCTIONS } from '../lib/workflow.js';
const TIER_MODELS = {
    fast: 'claude-haiku-4-5-20251001',
    balanced: 'claude-sonnet-4-6',
    thorough: 'claude-opus-4-7',
};
const EFFORT_MAP = {
    low: 'low',
    medium: 'medium',
    high: 'high',
    max: 'max',
};
export async function runClaudeReview(repoDir, baseBranch, prTitle, quality, vendor, perReviewBudget, stepInstructions, onLog) {
    const model = TIER_MODELS[quality.tier] ?? 'claude-sonnet-4-6';
    const effort = EFFORT_MAP[vendor.effort] ?? 'medium';
    const focusLine = quality.focus.length > 0
        ? `Focus areas: ${quality.focus.join(', ')}.`
        : '';
    const customLine = quality.custom_prompt ?? '';
    const behaviorInstructions = stepInstructions ?? DEFAULT_REVIEW_INSTRUCTIONS;
    const prompt = [
        `You are reviewing a pull request titled: "${prTitle}".`,
        `The branch \`${baseBranch}\` is the base. Review only the changes introduced in this PR.`,
        focusLine,
        customLine,
        behaviorInstructions,
    ].filter(Boolean).join('\n');
    const args = [
        '--print',
        '--output-format', 'json',
        '--model', model,
        '--effort', effort,
        '--max-budget-usd', String(perReviewBudget),
        '--allowedTools', 'Bash(git diff),Bash(git log)',
    ];
    onLog?.(`  running: claude --print --model ${model} --effort ${effort}`);
    try {
        const { stdout } = await execa('claude', args, {
            cwd: repoDir,
            timeout: 180_000,
            input: prompt,
            env: { ...process.env },
        });
        const raw = stdout.trim();
        try {
            const parsed = JSON.parse(raw);
            const review = typeof parsed.result === 'string' ? parsed.result.trim() : raw;
            const inTok = parsed.usage?.input_tokens;
            const outTok = parsed.usage?.output_tokens;
            const tokensUsed = typeof inTok === 'number' && typeof outTok === 'number'
                ? inTok + outTok
                : undefined;
            return { review, tokensUsed };
        }
        catch {
            return { review: raw };
        }
    }
    catch (err) {
        const execa = err;
        const rawStderr = execa.stderr?.trim() ?? '';
        const summary = (rawStderr.split('\n').filter(Boolean).at(-1)) ?? execa.message ?? 'unknown error';
        const thrown = Object.assign(new Error(`claude: ${summary}`), {
            exitCode: execa.exitCode,
            timedOut: execa.timedOut,
            stderr: rawStderr,
        });
        throw thrown;
    }
}
export async function checkClaudeAuth() {
    try {
        const { stdout } = await execa('claude', ['--version'], { timeout: 10_000 });
        return { ok: true, detail: stdout.trim() };
    }
    catch (err) {
        const error = err;
        return { ok: false, detail: error.stderr ?? error.message ?? 'not found' };
    }
}
//# sourceMappingURL=claude.js.map