// spec-migrator/frontmatter-parser.ts — Lightweight frontmatter parser + validation helpers
// Avoids circular imports by not importing from other engine modules.
// --- Lightweight frontmatter parser ---
const FM_RE = /^---\n([\s\S]*?)\n---\n/;
const FM_KV_RE = /^(\w+):\s*(.+)$/;
export function parseFrontmatterLocal(content) {
    const match = FM_RE.exec(content);
    if (!match?.[1]) {
        return { metadata: {} };
    }
    const metadata = {};
    for (const line of match[1].split('\n')) {
        const kv = FM_KV_RE.exec(line.trim());
        if (!kv?.[1] || kv[2] === undefined) {
            continue;
        }
        const key = kv[1];
        const val = kv[2].trim();
        if (val.startsWith('[') && val.endsWith(']')) {
            metadata[key] = val
                .slice(1, -1)
                .split(',')
                .map((s) => s.trim().replace(/^["']|["']$/g, ''));
        }
        else if (val === 'null') {
            metadata[key] = null;
        }
        else if (/^\d+$/.test(val)) {
            metadata[key] = parseInt(val, 10);
        }
        else {
            metadata[key] = val.replace(/^["']|["']$/g, '');
        }
    }
    return { metadata };
}
// --- Validation helpers for frontmatter values ---
const SPEC_TYPES = new Set([
    'feature',
    'refactor',
    'bugfix',
    'infra',
    'docs',
    'project',
    'agent',
    'tech-debt',
]);
const SPEC_SCOPES = new Set(['trivial', 'feature', 'cross-module', 'architectural']);
const SPEC_STATUSES = new Set([
    'draft',
    'review',
    'approved',
    'implementing',
    'done',
    'discarded',
]);
const RISK_LEVELS = new Set(['low', 'medium', 'high', 'critical']);
const SPEC_TARGETS = new Set([
    'backend',
    'frontend',
    'fullstack',
    'infrastructure',
    'mobile',
    'desktop',
    'cli',
    'api',
    'database',
    'devops',
    'testing',
    'docs',
]);
const VALID_DIFFICULTIES = new Set([1, 2, 3, 4, 5]);
export function validSpecType(v) {
    return typeof v === 'string' && SPEC_TYPES.has(v) ? v : null;
}
export function validSpecScope(v) {
    return typeof v === 'string' && SPEC_SCOPES.has(v) ? v : null;
}
export function validSpecStatus(v) {
    return typeof v === 'string' && SPEC_STATUSES.has(v) ? v : null;
}
export function validRiskLevel(v) {
    return typeof v === 'string' && RISK_LEVELS.has(v) ? v : null;
}
export function validSpecTarget(v) {
    return typeof v === 'string' && SPEC_TARGETS.has(v) ? v : null;
}
export function validDifficulty(v) {
    const n = typeof v === 'number' ? v : typeof v === 'string' ? parseInt(v, 10) : NaN;
    return !isNaN(n) && VALID_DIFFICULTIES.has(n) ? n : null;
}
//# sourceMappingURL=frontmatter-parser.js.map