// engine/skill-generation/agent-skills-formatter.ts — SPEC-607
// Formats skill content as Agent Skills spec-compliant SKILL.md files.
/** Serialize a string[] as a YAML list block. */
function yamlList(items, indent = '  ') {
    return items.map((item) => `${indent}- "${item}"`).join('\n');
}
/** Serialize a string[] as a YAML list block (unquoted values). */
function yamlListUnquoted(items, indent = '  ') {
    return items.map((item) => `${indent}- ${item}`).join('\n');
}
/** Serialize a Record<string, string> as flat YAML key-value pairs. */
function yamlRecord(record) {
    return Object.entries(record)
        .map(([k, v]) => `${k}: ${v}`)
        .join('\n');
}
/**
 * Format skill content as a SKILL.md with YAML frontmatter following the
 * Agent Skills spec (agentskills.io).
 */
export function formatAsAgentSkill(metadata, body) {
    const lines = ['---'];
    lines.push(`name: ${metadata.name}`);
    lines.push(`description: ${metadata.description}`);
    if (metadata.version !== undefined) {
        lines.push(`version: ${metadata.version}`);
    }
    if (metadata.license !== undefined) {
        lines.push(`license: ${metadata.license}`);
    }
    if (metadata.compatibility !== undefined && metadata.compatibility.length > 0) {
        lines.push('compatibility:');
        lines.push(yamlListUnquoted(metadata.compatibility));
    }
    if (metadata.allowedTools !== undefined && metadata.allowedTools.length > 0) {
        lines.push('allowed-tools:');
        lines.push(yamlListUnquoted(metadata.allowedTools));
    }
    if (metadata.triggers !== undefined && metadata.triggers.length > 0) {
        lines.push('triggers:');
        lines.push(yamlList(metadata.triggers));
    }
    if (metadata.metadata !== undefined && Object.keys(metadata.metadata).length > 0) {
        lines.push(yamlRecord(metadata.metadata));
    }
    lines.push('---');
    lines.push('');
    lines.push(body);
    return lines.join('\n');
}
/** Converts raw parsed field maps into AgentSkillMetadata. */
function buildMetadataFromFields(scalars, lists) {
    const name = scalars.name ?? '';
    if (!name) {
        return null;
    }
    const result = {
        name,
        description: scalars.description ?? '',
    };
    if (scalars.version) {
        result.version = scalars.version;
    }
    if (scalars.license) {
        result.license = scalars.license;
    }
    // 'compatibility', 'allowed-tools', 'triggers' use bracket notation (hyphen in key)
    const compatibility = lists.compatibility ?? [];
    if (compatibility.length > 0) {
        result.compatibility = compatibility;
    }
    const allowedTools = lists['allowed-tools'] ?? [];
    if (allowedTools.length > 0) {
        result.allowedTools = allowedTools;
    }
    const triggers = lists.triggers ?? [];
    if (triggers.length > 0) {
        result.triggers = triggers;
    }
    const reservedKeys = ['name', 'description', 'version', 'license'];
    const extraKeys = Object.keys(scalars).filter((k) => !reservedKeys.includes(k));
    if (extraKeys.length > 0) {
        result.metadata = Object.fromEntries(extraKeys.map((k) => [k, scalars[k] ?? '']));
    }
    return result;
}
/**
 * Parse Agent Skills frontmatter from a SKILL.md string.
 * Returns the parsed metadata or null if the content has no valid frontmatter.
 */
export function parseAgentSkillFrontmatter(content) {
    if (!content.startsWith('---\n')) {
        return null;
    }
    const endIndex = content.indexOf('\n---\n', 4);
    if (endIndex === -1) {
        return null;
    }
    const frontmatterBlock = content.slice(4, endIndex);
    const frontmatterLines = frontmatterBlock.split('\n');
    // Accumulate into plain maps (no param mutation)
    const scalars = {};
    const lists = {};
    let currentListKey = '';
    for (const line of frontmatterLines) {
        if (line.startsWith('  - ') && currentListKey !== '') {
            const value = line.slice(4).replace(/^"(.*)"$/, '$1');
            const existing = lists[currentListKey] ?? [];
            lists[currentListKey] = [...existing, value];
            continue;
        }
        const colonIdx = line.indexOf(': ');
        if (colonIdx !== -1) {
            const key = line.slice(0, colonIdx).trim();
            const value = line.slice(colonIdx + 2).trim();
            scalars[key] = value;
            currentListKey = '';
        }
        else if (line.endsWith(':') && line.trim().length > 1) {
            currentListKey = line.slice(0, -1).trim();
        }
    }
    return buildMetadataFromFields(scalars, lists);
}
//# sourceMappingURL=agent-skills-formatter.js.map