// engine/facilitate/plan-parser.ts — SPEC-264 Plan Mode markdown parser
// ---------------------------------------------------------------------------
// Section extraction
// ---------------------------------------------------------------------------
const SECTION_HEADERS = /^#{1,3}\s+(.+)$/m;
const PLAN_SECTION_RE = /^#{1,3}\s+(?:plan|approach|steps|implementation|solution)\s*$/im;
const TITLE_RE = /^#\s+(.+)$/m;
const CHECKBOX_RE = /^[-*]\s+\[[ x]\]\s+(.+)$/gim;
const ARCHITECTURAL_TERMS = /\b(?:migrate|migration|replace|redesign|overhaul|rewrite|refactor architecture)\b/i;
const COMPLEXITY_TERMS = /\b(?:service|worker|queue|cache|database|api|integration)\b/gi;
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function extractTitle(markdown) {
    const match = TITLE_RE.exec(markdown);
    if (match?.[1]) {
        return match[1].trim();
    }
    // Fallback: use first non-empty line
    const firstLine = markdown.split('\n').find((l) => l.trim().length > 0);
    return firstLine?.replace(/^#+\s*/, '').trim() ?? 'Untitled Plan';
}
function extractCheckboxes(markdown) {
    const results = [];
    let m;
    const re = new RegExp(CHECKBOX_RE.source, CHECKBOX_RE.flags);
    while ((m = re.exec(markdown)) !== null) {
        if (m[1]) {
            results.push(m[1].trim());
        }
    }
    return results;
}
function extractBullets(section) {
    const lines = section.split('\n');
    return lines
        .filter((l) => /^[-*]\s+(?!\[)/.test(l.trim()))
        .map((l) => l.replace(/^[-*]\s+/, '').trim())
        .filter((l) => l.length > 0);
}
function extractTechnicalNotes(markdown) {
    const notes = [];
    // Extract content from code blocks (language tag as note)
    const blockRe = /```(\w+)/g;
    let m;
    while ((m = blockRe.exec(markdown)) !== null) {
        if (m[1]) {
            notes.push(`Code block: ${m[1]}`);
        }
    }
    // Extract lines with technical hints (file paths, imports, type refs)
    const techLineRe = /^[-*]\s+.{10,}(?:\.ts|\.js|import|export|interface|type |class )/gim;
    let tm;
    while ((tm = techLineRe.exec(markdown)) !== null) {
        const note = tm[0].replace(/^[-*]\s+/, '').trim();
        if (!notes.includes(note)) {
            notes.push(note);
        }
    }
    return notes.slice(0, 10);
}
function estimateComplexity(markdown, acCount) {
    if (ARCHITECTURAL_TERMS.test(markdown)) {
        return 'architectural';
    }
    // Create a fresh regex each call to avoid stateful `g` flag issues
    const complexTerms = (markdown.match(new RegExp(COMPLEXITY_TERMS.source, 'gi')) ?? []).length;
    if (acCount >= 5 || complexTerms >= 3) {
        return 'feature';
    }
    if (acCount >= 2 || complexTerms >= 1) {
        return 'simple';
    }
    return 'trivial';
}
function extractPlanSection(markdown) {
    const match = PLAN_SECTION_RE.exec(markdown);
    if (!match) {
        return markdown;
    }
    const start = match.index + match[0].length;
    // Find next section header at same or higher level
    const remaining = markdown.slice(start);
    const nextHeader = SECTION_HEADERS.exec(remaining);
    return nextHeader ? remaining.slice(0, nextHeader.index) : remaining;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Extract structured spec data from Plan Mode markdown output.
 * Returns null when the input is empty or not parseable as a plan.
 */
export function parsePlanContent(planMarkdown) {
    if (!planMarkdown || planMarkdown.trim().length < 10) {
        return null;
    }
    const title = extractTitle(planMarkdown);
    // First try checkbox-style ACs
    let acceptanceCriteria = extractCheckboxes(planMarkdown);
    // Fallback: look for bullet lists inside plan/approach sections
    if (acceptanceCriteria.length === 0) {
        const section = extractPlanSection(planMarkdown);
        acceptanceCriteria = extractBullets(section);
    }
    const technicalNotes = extractTechnicalNotes(planMarkdown);
    const estimatedComplexity = estimateComplexity(planMarkdown, acceptanceCriteria.length);
    return {
        title,
        acceptanceCriteria,
        technicalNotes,
        estimatedComplexity,
    };
}
//# sourceMappingURL=plan-parser.js.map