"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseArchitectureDecisionRecord = parseArchitectureDecisionRecord;
const supportedAdrStatuses = ['proposed', 'accepted', 'deprecated', 'superseded'];
function normalizeAdrIdFromFilename(fileName) {
    const match = fileName.match(/^adr-(\d{3})(?:-.+)?\.md$/i);
    if (!match) {
        return undefined;
    }
    return `ADR-${match[1]}`;
}
function findHeading(lines) {
    for (const line of lines.slice(0, 20)) {
        const trimmed = line.trim();
        if (trimmed.startsWith('# ')) {
            const heading = trimmed.slice(2).trim();
            if (heading.length > 0) {
                return heading;
            }
        }
        if (trimmed.length > 0 && !trimmed.startsWith('<!--')) {
            return undefined;
        }
    }
    return undefined;
}
function normalizeAdrIdFromHeading(heading) {
    if (!heading) {
        return undefined;
    }
    const match = heading.match(/^ADR-(\d{3})\b/i);
    if (!match) {
        return undefined;
    }
    return `ADR-${match[1]}`;
}
function normalizeTitleFromHeading(heading) {
    if (!heading) {
        return '';
    }
    return heading.replace(/^ADR-\d{3}\s*[:\-]?\s*/i, '').trim();
}
function normalizeStatus(value) {
    const normalized = value.trim().toLowerCase();
    return supportedAdrStatuses.includes(normalized)
        ? normalized
        : undefined;
}
function findStatus(lines) {
    for (const line of lines) {
        const match = line.match(/^status\s*:\s*(.+)$/i);
        if (!match || match[1].trim().length === 0) {
            continue;
        }
        return {
            raw: match[1].trim(),
            normalized: normalizeStatus(match[1]),
        };
    }
    return {};
}
function parseArchitectureDecisionRecord(input) {
    const lines = input.rawContent.split(/\r?\n/);
    const heading = findHeading(lines);
    const filenameAdrId = normalizeAdrIdFromFilename(input.fileName);
    const headingAdrId = normalizeAdrIdFromHeading(heading);
    const parsedStatus = findStatus(lines);
    return {
        fileName: input.fileName,
        filePath: input.filePath,
        adrId: headingAdrId ?? filenameAdrId ?? 'ADR-UNKNOWN',
        title: normalizeTitleFromHeading(heading),
        ...(parsedStatus.normalized ? { status: parsedStatus.normalized } : {}),
        rawContent: input.rawContent,
    };
}
//# sourceMappingURL=adrParser.js.map