// engine/spec-template-cleaner.ts — Strip unfilled template sections from spec content.
// Applied before writing HU.md and FICHA-TECNICA.md to prevent generic placeholders
// from leaking into committed spec files.
/**
 * Patterns that identify template sections with no real project-specific content.
 * A section is considered "empty" if its body only contains these patterns.
 */
/** Lines that are considered pure placeholders (no real content). */
const PLACEHOLDER_LINE_PATTERNS = [
    /^\s*-\s*\(List (modules|files|affected environments|edge cases|step|section)\b.*\)\s*$/i,
    /^\s*-\s*\(Define what is NOT included.*\)\s*$/i,
    /^\s*-\s*\(Describe .*\)\s*$/i,
    /^\s*-\s*\(Analyze .*\)\s*$/i,
    /^\s*-\s*\(Why .*\)\s*$/i,
    /^\s*-\s*\(List .*\)\s*$/i,
    /^\s*1\.\s*\(.*\)\s*$/,
    /^\s*2\.\s*\(.*\)\s*$/,
];
/** Section headings whose entire block is replaceable when the body is template-only. */
const STRIPPABLE_SECTION_HEADINGS = [
    /^## Affected Modules$/i,
    /^## Entity Relationship/i,
];
/** Mermaid diagram blocks considered empty (no real domain nodes). */
const EMPTY_MERMAID_PATTERNS = [
    // ER diagrams with only ENTITY_A / ENTITY_B placeholders
    /```mermaid\s*\nerDiagram\s*\n(?:\s*%%[^\n]*\n)*\s*ENTITY_A\s*\{[\s\S]*?\}\s*\n\s*ENTITY_B\s*\{[\s\S]*?\}\s*\n\s*ENTITY_A\s*\|\|--o\{[^\n]*\n\s*```/,
];
/** Inline content patterns that mark sections as template-only. */
const ENTITY_AB_CONTENT = /ENTITY_A|ENTITY_B/;
const TBD_ONLY = /^\s*-?\s*(TBD|TODO|N\/A|None|n\/a)\s*$/i;
/**
 * Returns true if a text block (section body without the heading line) contains
 * only placeholder/template content and no real project-specific information.
 */
function isBodyPlaceholderOnly(body) {
    const lines = body
        .split('\n')
        .map((l) => l.trim())
        .filter((l) => l.length > 0);
    if (lines.length === 0) {
        return true;
    }
    // Contains generic ENTITY_A / ENTITY_B references
    if (ENTITY_AB_CONTENT.test(body)) {
        return true;
    }
    // All non-empty lines are placeholder patterns or TBD
    return lines.every((line) => {
        if (TBD_ONLY.test(line)) {
            return true;
        }
        return PLACEHOLDER_LINE_PATTERNS.some((p) => p.test(line));
    });
}
/**
 * Split markdown content into sections keyed by their heading line.
 * Returns an array of { heading, body } tuples preserving order.
 */
function splitSections(content) {
    const lines = content.split('\n');
    const sections = [];
    let currentHeading = '';
    let currentBody = [];
    for (const line of lines) {
        if (/^#{1,3} /.test(line)) {
            sections.push({ heading: currentHeading, body: currentBody.join('\n') });
            currentHeading = line;
            currentBody = [];
        }
        else {
            currentBody.push(line);
        }
    }
    sections.push({ heading: currentHeading, body: currentBody.join('\n') });
    return sections;
}
/**
 * Remove empty Mermaid diagram blocks that contain only generic placeholder nodes.
 * Operates on the full content string (regex-based, not line-by-line).
 */
function stripEmptyMermaidBlocks(content) {
    let result = content;
    for (const pattern of EMPTY_MERMAID_PATTERNS) {
        result = result.replace(pattern, '');
    }
    return result;
}
/**
 * Strip sections from spec content that contain only template/placeholder text.
 *
 * Targets:
 *  - "Affected Modules" sections with no real module list
 *  - "Entity Relationship" sections using ENTITY_A / ENTITY_B placeholders
 *  - Mermaid diagram blocks with no real domain nodes (graph LR with no meaningful edges)
 *
 * Keeps all sections that have real, project-specific content.
 */
export function stripEmptySections(content) {
    // First pass: remove empty mermaid diagram blocks
    const result = stripEmptyMermaidBlocks(content);
    // Second pass: remove strippable sections with placeholder-only bodies
    const sections = splitSections(result);
    const kept = [];
    for (const { heading, body } of sections) {
        const shouldStrip = STRIPPABLE_SECTION_HEADINGS.some((pattern) => pattern.test(heading));
        if (shouldStrip && isBodyPlaceholderOnly(body)) {
            // Drop this section entirely — heading + body
            continue;
        }
        kept.push(heading ? `${heading}\n${body}` : body);
    }
    // Re-join and normalize multiple consecutive blank lines to at most two
    return (kept
        .join('\n')
        .replace(/\n{3,}/g, '\n\n')
        .trimEnd() + '\n');
}
//# sourceMappingURL=spec-template-cleaner.js.map