import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
const execFileAsync = promisify(execFile);
export const PR_STATUS_TTL_MS = 45_000;
export const PR_STATUS_FIELDS = 'number,title,state,isDraft,reviewDecision,mergeable,statusCheckRollup';
const cache = new Map();
function needsHuman(value) {
    if (value.state !== 'OPEN' || value.isDraft)
        return false;
    const checks = value.statusCheckRollup ?? [];
    const checksSettled = checks.every((check) => {
        const row = check;
        return row.conclusion === 'SUCCESS' || row.conclusion === 'NEUTRAL' || row.status === 'COMPLETED';
    });
    return value.reviewDecision !== 'APPROVED' || (value.mergeable === 'MERGEABLE' && checksSettled);
}
/** CLI-owned bounded-TTL source shared by feed attention and PR-board consumers. */
export async function readPullRequestStatus(session, options = {}) {
    const ref = session.pr?.url ?? session.pr?.number;
    if (!ref || !session.cwd)
        return undefined;
    const key = `${session.cwd}\0${String(ref)}`;
    const now = options.nowMs ?? Date.now();
    const hit = cache.get(key);
    if (hit && hit.expiresAt > now)
        return hit.value;
    try {
        const { stdout } = await execFileAsync('gh', ['pr', 'view', String(ref), '--json', PR_STATUS_FIELDS], {
            cwd: session.cwd,
            timeout: 15_000,
            maxBuffer: 1024 * 1024,
        });
        const raw = JSON.parse(stdout);
        const value = { ...raw, url: session.pr?.url, needsHuman: needsHuman(raw) };
        cache.set(key, { expiresAt: now + (options.ttlMs ?? PR_STATUS_TTL_MS), value });
        return value;
    }
    catch {
        cache.set(key, { expiresAt: now + (options.ttlMs ?? PR_STATUS_TTL_MS) });
        return undefined;
    }
}
export async function projectPullRequestBoard(sessions) {
    const rows = await Promise.all(sessions.map((session) => readPullRequestStatus(session)));
    return rows.filter((row) => row !== undefined);
}
export function resetPullRequestStatusCache() { cache.clear(); }
