/** Extract a section body after a given heading (## Heading) */
function extractSection(lines, heading) {
    const headingLower = heading.toLowerCase();
    const result = [];
    let inside = false;
    for (const line of lines) {
        const trimmed = line.trim();
        if (trimmed.startsWith('## ') && trimmed.slice(3).toLowerCase() === headingLower) {
            inside = true;
            continue;
        }
        if (inside) {
            if (trimmed.startsWith('## ')) {
                break;
            }
            result.push(line);
        }
    }
    return result;
}
/** Parse bullet/checkbox lines into plain-text criteria */
function parseCriteriaLines(lines) {
    return lines
        .map((l) => l.trim())
        .filter((l) => l.length > 0)
        .map((l) => l
        .replace(/^-\s*\[[ xX]\]\s*/, '')
        .replace(/^[-*]\s+/, '')
        .trim())
        .filter((l) => l.length > 0);
}
/** Extract tags from comma-separated or space-separated line values */
function parseTagsSection(lines) {
    const joined = lines
        .map((l) => l.trim())
        .filter((l) => l.length > 0 && !l.startsWith('#'))
        .join(',');
    return joined
        .split(/[,\s]+/)
        .map((t) => t.replace(/^[-*]\s*/, '').trim())
        .filter((t) => t.length > 0);
}
/**
 * Parse Markdown content into an ImportedSpec.
 * Recognises sections: ## Title (H1 is title), ## Description, ## Acceptance Criteria, ## Tags.
 */
export function parseMarkdown(content) {
    const lines = content.split('\n');
    // Title from first H1 line
    let title = '';
    for (const line of lines) {
        const trimmed = line.trim();
        if (trimmed.startsWith('# ') && !trimmed.startsWith('## ')) {
            title = trimmed.slice(2).trim();
            break;
        }
    }
    const descLines = extractSection(lines, 'description');
    const description = descLines
        .map((l) => l.trim())
        .filter((l) => l.length > 0)
        .join('\n')
        .trim();
    const acLines = extractSection(lines, 'acceptance criteria');
    const criteria = parseCriteriaLines(acLines);
    const tagLines = extractSection(lines, 'tags');
    const tags = parseTagsSection(tagLines);
    // Fallback: if no H1, use first non-empty line
    if (!title) {
        title = lines.find((l) => l.trim().length > 0)?.trim() ?? 'Imported Spec';
    }
    return { title, description, criteria, tags };
}
//# sourceMappingURL=markdown-importer.js.map