const DEFAULT_TYPE = 'review';
const DEFAULT_MODEL = 'default';
const DEFAULT_ROUND = 1;
const DEFAULT_SERVICE = 'crosscheck';
const STEP_TYPES = new Set(['review', 'recheck', 'fix', 'conflict-resolve']);
export function buildAnnotation(input) {
    const sha = input.sha ? ` sha=${fieldValue(input.sha)}` : '';
    const nextStep = input.next_step ? ` next_step=${fieldValue(input.next_step)}` : '';
    const trigger = input.trigger ? ` trigger=${fieldValue(input.trigger)}` : '';
    return `<!-- crosscheck: origin=${fieldValue(input.origin)} reviewer=${fieldValue(input.reviewer)} model=${fieldValue(input.model)} type=${fieldValue(input.type)} round=${input.round} verdict=${fieldValue(input.verdict)} service=${fieldValue(input.service)}${sha}${nextStep}${trigger} -->`;
}
export function parseAnnotation(body) {
    const fields = parseAnnotationFields(body);
    if (!fields)
        return null;
    const origin = fields.get('origin');
    const reviewer = fields.get('reviewer');
    if (!origin || !reviewer)
        return null;
    // Defaults keep legacy annotations parseable:
    // type=review, round=1, service=crosscheck, model=default.
    return {
        origin,
        reviewer,
        model: fields.get('model') ?? DEFAULT_MODEL,
        type: parseType(fields.get('type')),
        round: parseRound(fields.get('round')),
        verdict: fields.get('verdict') ?? 'UNKNOWN',
        service: fields.get('service') ?? DEFAULT_SERVICE,
        ...(fields.has('sha') && { sha: fields.get('sha') }),
        ...(fields.has('next_step') && { next_step: fields.get('next_step') }),
        ...(fields.has('trigger') && { trigger: fields.get('trigger') }),
    };
}
export function parseAnnotationFields(body) {
    return parseAnnotationFieldsFenced(body);
}
// Like parseAnnotationFields but skips annotations inside fenced code blocks
// (``` or ~~~). Prevents annotations in examples from being misread as the
// authoritative crosscheck marker for a comment.
//
// Bareword tokens (e.g. `fix_applied`, `conflict_resolved`) that have no `=`
// are stored in the returned map under the key '__marker__' so callers can
// detect them without special-casing the loop.
export function parseAnnotationFieldsFenced(body) {
    let inFence = false;
    let lastMatch = null;
    for (const line of body.split('\n')) {
        const trimmed = line.trim();
        if (trimmed.startsWith('```') || trimmed.startsWith('~~~')) {
            inFence = !inFence;
            continue;
        }
        if (inFence)
            continue;
        const match = trimmed.match(/^<!--\s*crosscheck:\s*([^>]*)-->/);
        if (match)
            lastMatch = match[1];
    }
    if (!lastMatch)
        return null;
    return parseFieldsWithMarker(lastMatch);
}
function parseFieldsWithMarker(attrs) {
    const fields = new Map();
    for (const token of attrs.trim().split(/\s+/).filter(Boolean)) {
        const eq = token.indexOf('=');
        if (eq === -1) {
            // Bareword token: store as marker (last one wins, matching legacy behaviour)
            fields.set('__marker__', token);
        }
        else {
            fields.set(token.slice(0, eq), token.slice(eq + 1));
        }
    }
    return fields;
}
export function buildCommitTrailers(input) {
    return [
        `Crosscheck-Reviewer: ${fieldValue(input.reviewer)}`,
        `Crosscheck-Model: ${fieldValue(input.model)}`,
        `Crosscheck-Step: ${fieldValue(input.step)}`,
        `Crosscheck-Service: ${fieldValue(input.service)}`,
    ].join('\n');
}
function parseFields(attrs) {
    const fields = new Map();
    for (const match of attrs.matchAll(/\b([a-z_]+)=([^\s]+)/g)) {
        fields.set(match[1], match[2]);
    }
    return fields;
}
function parseType(value) {
    if (value === undefined)
        return DEFAULT_TYPE;
    return STEP_TYPES.has(value) ? value : value;
}
function parseRound(value) {
    if (value === undefined)
        return DEFAULT_ROUND;
    const parsed = Number.parseInt(value, 10);
    return Number.isInteger(parsed) && parsed > 0 ? parsed : DEFAULT_ROUND;
}
function fieldValue(value) {
    return value.trim().replace(/\s+/g, '_');
}
//# sourceMappingURL=annotation.js.map