// engine/frontmatter-parser.ts — Parse YAML frontmatter from spec.md files
// Zero-dependency: no external YAML parser needed for the simple key:value format.
const FRONTMATTER_RE = /^---\n([\s\S]*?)\n---\n([\s\S]*)$/;
const KV_LINE_RE = /^(\w+):\s*(.+)$/;
/**
 * Parse YAML frontmatter from markdown content.
 * Supports simple key: value pairs, arrays `[a, b]`, null, integers, and quoted strings.
 * Returns the original content as body if no frontmatter is found.
 */
export function parseFrontmatter(content) {
    const match = FRONTMATTER_RE.exec(content);
    if (!match?.[1] || match[2] === undefined) {
        return { metadata: {}, body: content };
    }
    const metadata = {};
    for (const line of match[1].split('\n')) {
        const kv = KV_LINE_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 if (val === 'true') {
            metadata[key] = true;
        }
        else if (val === 'false') {
            metadata[key] = false;
        }
        else {
            metadata[key] = val.replace(/^["']|["']$/g, '');
        }
    }
    return { metadata, body: match[2] };
}
/**
 * Strip YAML frontmatter from markdown content, returning only the body.
 * If no frontmatter is present, returns the original content unchanged.
 */
export function stripFrontmatter(content) {
    return content.replace(/^---\n[\s\S]*?\n---\n/, '');
}
//# sourceMappingURL=frontmatter-parser.js.map