// engine/skill-generation/progressive-splitter.ts — SPEC-607
// Splits large generated skill content into a compact SKILL.md + reference files.
const DEFAULT_MAX_LINES = 200;
/** Extract the YAML frontmatter block (including --- delimiters) from content. */
function extractFrontmatter(content) {
    if (!content.startsWith('---\n')) {
        return { frontmatter: '', body: content };
    }
    const end = content.indexOf('\n---\n', 4);
    if (end === -1) {
        return { frontmatter: '', body: content };
    }
    const frontmatter = content.slice(0, end + 5); // includes closing ---\n
    const body = content.slice(end + 5);
    return { frontmatter, body };
}
/** Split a Markdown body into sections by ## headings. */
function splitIntoSections(body) {
    const sections = [];
    const lines = body.split('\n');
    let currentHeading = '';
    let currentLines = [];
    for (const line of lines) {
        if (line.startsWith('## ')) {
            if (currentLines.length > 0) {
                sections.push({ heading: currentHeading, content: currentLines.join('\n') });
            }
            currentHeading = line;
            currentLines = [line];
        }
        else {
            currentLines.push(line);
        }
    }
    if (currentLines.length > 0) {
        sections.push({ heading: currentHeading, content: currentLines.join('\n') });
    }
    return sections;
}
/** Convert a section heading to a filename-safe slug. */
function headingToSlug(heading) {
    return heading
        .replace(/^##\s+/, '')
        .toLowerCase()
        .replace(/[^a-z0-9]+/g, '-')
        .replace(/^-|-$/g, '');
}
/**
 * Split generated skill content for progressive disclosure.
 *
 * If the content is under maxLines, returns it unchanged with empty references.
 * If over maxLines, keeps frontmatter + first section(s) in skillMd and moves
 * additional sections to references/ files with linking comments.
 */
export function splitGeneratedSkill(content, maxLines = DEFAULT_MAX_LINES) {
    const totalLines = content.split('\n').length;
    if (totalLines <= maxLines) {
        return { skillMd: content, references: new Map() };
    }
    const { frontmatter, body } = extractFrontmatter(content);
    const sections = splitIntoSections(body);
    if (sections.length <= 1) {
        // Cannot split further — return as-is
        return { skillMd: content, references: new Map() };
    }
    const references = new Map();
    const keptSections = [];
    let lineCount = frontmatter.split('\n').length;
    for (const section of sections) {
        const sectionLineCount = section.content.split('\n').length;
        if (lineCount + sectionLineCount <= maxLines || keptSections.length === 0) {
            // Keep in main file (always keep at least one section)
            keptSections.push(section.content);
            lineCount += sectionLineCount;
        }
        else {
            // Move to references
            const slug = headingToSlug(section.heading) || 'details';
            const refName = `${slug}.md`;
            references.set(refName, section.content);
        }
    }
    // Add reference links to the main skill
    if (references.size > 0) {
        keptSections.push('');
        keptSections.push('## References');
        keptSections.push('');
        for (const name of references.keys()) {
            keptSections.push(`- See [references/${name}](references/${name}) for details`);
        }
    }
    const skillMd = frontmatter + keptSections.join('\n');
    return { skillMd, references };
}
//# sourceMappingURL=progressive-splitter.js.map