// ---------------------------------------------------------------------------
// Section header patterns that signal breaking changes
// ---------------------------------------------------------------------------
const BREAKING_SECTION_PATTERNS = [
    /#{1,3}\s*(?:breaking\s*changes?|BREAKING\s*CHANGES?)/i,
    /#{1,3}\s*(?:removed|REMOVED)/i,
    /#{1,3}\s*(?:migration\s*guide|MIGRATION\s*GUIDE)/i,
    /⚠️/,
    /!{2,}/,
    /BREAKING:/,
];
const BREAKING_INLINE_PATTERNS = [
    /\bBREAKING\b/,
    /\bremoved?\b.*\bAPI\b/i,
    /\bdeprecated?\b.*\bremoved?\b/i,
    /\bmust\s+update\b/i,
    /\bno\s+longer\s+support/i,
    /\bwill\s+throw\b/i,
];
// ---------------------------------------------------------------------------
// Version range extraction
// ---------------------------------------------------------------------------
function isInVersionRange(sectionTitle, fromVersion, toVersion) {
    const versionPattern = /v?(\d+\.\d+[\d.]*)/g;
    const versionMatches = Array.from(sectionTitle.matchAll(versionPattern));
    if (versionMatches.length === 0) {
        return true;
    }
    for (const m of versionMatches) {
        const ver = m[1];
        if (ver && isVersionBetween(ver, fromVersion, toVersion)) {
            return true;
        }
    }
    return false;
}
function toVersionNum(v) {
    const parts = v.replace(/^v/, '').split('.').map(Number);
    /* v8 ignore next */
    return (parts[0] ?? 0) * 1_000_000 + (parts[1] ?? 0) * 1_000 + (parts[2] ?? 0);
}
function isVersionBetween(ver, from, to) {
    const verNum = toVersionNum(ver);
    const fromNum = toVersionNum(from);
    const toNum = toVersionNum(to);
    return verNum > fromNum && verNum <= toNum;
}
// ---------------------------------------------------------------------------
// Line extraction
// ---------------------------------------------------------------------------
function extractBreakingLines(lines) {
    const found = [];
    let inBreakingSection = false;
    for (const line of lines) {
        const isHeader = /^#{1,4}/.test(line);
        if (isHeader) {
            inBreakingSection = BREAKING_SECTION_PATTERNS.some((p) => p.test(line));
            continue;
        }
        if (inBreakingSection && line.trim().length > 0) {
            found.push(line.trim());
            continue;
        }
        if (BREAKING_INLINE_PATTERNS.some((p) => p.test(line)) && line.trim().length > 10) {
            found.push(line.trim());
        }
    }
    return found;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Parse changelog text and extract breaking changes between fromVersion and toVersion.
 */
export function parseBreakingChanges(changelogText, fromVersion, toVersion) {
    if (!changelogText.trim()) {
        return [];
    }
    const lines = changelogText.split('\n');
    const relevantLines = [];
    let currentSectionInRange = false;
    for (const line of lines) {
        const isHeader = /^#{1,4}/.test(line);
        if (isHeader) {
            currentSectionInRange = isInVersionRange(line, fromVersion, toVersion);
        }
        if (currentSectionInRange) {
            relevantLines.push(line);
        }
    }
    const targetLines = relevantLines.length > 5 ? relevantLines : lines;
    const breakingLines = extractBreakingLines(targetLines);
    return breakingLines.slice(0, 20).map((description) => ({
        description: description.replace(/^[-*]\s*/, ''),
        affectedPattern: '',
        migrationAction: 'Review changelog and update affected code',
        impact: 'unknown',
    }));
}
//# sourceMappingURL=breaking-change-parser.js.map