/**
 * Extracts analytics event data from review report markdown.
 *
 * Review reports follow a consistent structure with finding tables
 * under "new", "persists", and "resolved" sections. Each table row
 * contains a finding_id column.
 */
import { writeAnalyticsEvent } from './writer.js';
const SECTION_PATTERNS = [
    { pattern: /^##\s+.*\bnew\b/i, status: 'new' },
    { pattern: /^##\s+.*\bpersists\b/i, status: 'persists' },
    { pattern: /^##\s+.*\bresolved\b/i, status: 'resolved' },
];
export function parseFindingsFromReport(reportContent) {
    const lines = reportContent.split('\n');
    const findings = [];
    let currentStatus = null;
    let columnIndices = null;
    let headerParsed = false;
    for (const line of lines) {
        const sectionMatch = matchSection(line);
        if (sectionMatch) {
            currentStatus = sectionMatch;
            columnIndices = null;
            headerParsed = false;
            continue;
        }
        if (line.startsWith('## ')) {
            currentStatus = null;
            columnIndices = null;
            headerParsed = false;
            continue;
        }
        if (!currentStatus)
            continue;
        const trimmed = line.trim();
        if (!trimmed.startsWith('|'))
            continue;
        if (isSeparatorRow(trimmed))
            continue;
        if (!headerParsed) {
            columnIndices = detectColumnIndices(trimmed);
            headerParsed = true;
            continue;
        }
        if (!columnIndices || columnIndices.findingId < 0)
            continue;
        const finding = parseTableRow(line, currentStatus, columnIndices);
        if (finding) {
            findings.push(finding);
        }
    }
    return findings;
}
export function extractDecisionFromReport(reportContent) {
    const resultMatch = reportContent.match(/^##\s+(?:結果|Result)\s*:\s*(\w+)/m);
    const decision = resultMatch?.[1];
    if (!decision)
        return null;
    return decision.toUpperCase() === 'REJECT' ? 'reject' : 'approve';
}
function matchSection(line) {
    for (const { pattern, status } of SECTION_PATTERNS) {
        if (pattern.test(line))
            return status;
    }
    return null;
}
function isSeparatorRow(trimmed) {
    return /^\|[\s-]+\|/.test(trimmed);
}
function detectColumnIndices(headerRow) {
    const cells = headerRow.split('|').map((c) => c.trim()).filter(Boolean);
    const findingId = cells.findIndex((c) => c.toLowerCase() === 'finding_id');
    const category = cells.findIndex((c) => {
        const lower = c.toLowerCase();
        return lower === 'category' || lower === 'カテゴリ';
    });
    return { findingId, category };
}
function parseTableRow(line, status, indices) {
    const cells = line.split('|').map((c) => c.trim()).filter(Boolean);
    if (cells.length <= indices.findingId)
        return null;
    const findingId = cells[indices.findingId];
    if (!findingId)
        return null;
    const categoryValue = indices.category >= 0 ? cells[indices.category] : undefined;
    const ruleId = categoryValue ?? findingId;
    const locationCell = findLocation(cells);
    const { file, line: lineNum } = parseLocation(locationCell);
    return { findingId, status, ruleId, file, line: lineNum };
}
function findLocation(cells) {
    for (const cell of cells) {
        if (cell.includes('/') || cell.includes('.ts') || cell.includes('.js') || cell.includes('.py')) {
            return cell;
        }
    }
    return '';
}
function parseLocation(location) {
    const cleaned = location.replace(/`/g, '');
    const lineMatch = cleaned.match(/:(\d+)/);
    const lineStr = lineMatch?.[1];
    const lineNum = lineStr ? parseInt(lineStr, 10) : 0;
    const file = cleaned.replace(/:\d+.*$/, '').trim();
    return { file, line: lineNum };
}
export function inferSeverity(findingId) {
    const id = findingId.toUpperCase();
    if (id.includes('SEC'))
        return 'error';
    return 'warning';
}
const FINDING_ID_PATTERN = /\b[A-Z]{2,}-(?:NEW-)?[\w-]+\b/g;
export function emitFixActionEvents(responseContent, iteration, runId, timestamp) {
    emitActionEvents(responseContent, 'fixed', iteration, runId, timestamp);
}
export function emitRebuttalEvents(responseContent, iteration, runId, timestamp) {
    emitActionEvents(responseContent, 'rebutted', iteration, runId, timestamp);
}
function emitActionEvents(responseContent, action, iteration, runId, timestamp) {
    const matches = responseContent.match(FINDING_ID_PATTERN);
    if (!matches)
        return;
    const uniqueIds = [...new Set(matches)];
    for (const findingId of uniqueIds) {
        const event = {
            type: 'fix_action',
            findingId,
            action,
            iteration,
            runId,
            timestamp: timestamp.toISOString(),
        };
        writeAnalyticsEvent(event);
    }
}
//# sourceMappingURL=report-parser.js.map