// engine/skill-discovery/multi-agent-writer.ts — SPEC-606: Write skill files for multiple AI agents
// Maps skill content to the file location expected by each supported AI agent/IDE.
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
// ---------------------------------------------------------------------------
// Agent path resolvers
// ---------------------------------------------------------------------------
/** Return the target file path for a given agent and skill name. */
function agentSkillPath(projectPath, skillName, agent) {
    switch (agent) {
        case 'claude':
            return path.join(projectPath, '.claude', 'skills', `${skillName}.md`);
        case 'cursor':
            return path.join(projectPath, '.cursor', 'rules', `${skillName}.mdc`);
        case 'copilot':
            return path.join(projectPath, '.github', 'copilot-instructions.md');
        case 'gemini':
            return path.join(projectPath, 'GEMINI.md');
        default:
            return path.join(projectPath, '.claude', 'skills', `${skillName}.md`);
    }
}
/** Return true if this agent appends to an existing shared file (vs owns its own file). */
function isAppendMode(agent) {
    return agent === 'copilot' || agent === 'gemini';
}
// ---------------------------------------------------------------------------
// Writers
// ---------------------------------------------------------------------------
/** Write a standalone skill file (Claude, Cursor). */
async function writeStandaloneFile(filePath, content, agent) {
    await fs.mkdir(path.dirname(filePath), { recursive: true });
    const existed = await fileExists(filePath);
    await fs.writeFile(filePath, content, 'utf-8');
    return { path: filePath, target: agent, created: !existed };
}
/** Append skill content to a shared instructions file (Copilot, Gemini). */
async function appendToSharedFile(filePath, skillName, content, agent) {
    await fs.mkdir(path.dirname(filePath), { recursive: true });
    const existed = await fileExists(filePath);
    const section = [
        '',
        `<!-- skill:${skillName} -->`,
        content,
        `<!-- /skill:${skillName} -->`,
        '',
    ].join('\n');
    if (existed) {
        const existing = await fs.readFile(filePath, 'utf-8');
        // Replace existing section if already present, otherwise append
        const marker = `<!-- skill:${skillName} -->`;
        if (existing.includes(marker)) {
            const updated = replaceSection(existing, skillName, section);
            await fs.writeFile(filePath, updated, 'utf-8');
        }
        else {
            await fs.appendFile(filePath, section, 'utf-8');
        }
    }
    else {
        await fs.writeFile(filePath, section.trimStart(), 'utf-8');
    }
    return { path: filePath, target: agent, created: !existed };
}
/** Replace an existing skill section in shared file content. */
function replaceSection(fileContent, skillName, newSection) {
    const startMarker = `<!-- skill:${skillName} -->`;
    const endMarker = `<!-- /skill:${skillName} -->`;
    const start = fileContent.indexOf(startMarker);
    const end = fileContent.indexOf(endMarker);
    if (start === -1 || end === -1) {
        return fileContent + newSection;
    }
    return (fileContent.slice(0, start) + newSection.trimStart() + fileContent.slice(end + endMarker.length));
}
/** Check if a file exists on disk. */
async function fileExists(filePath) {
    try {
        await fs.access(filePath);
        return true;
    }
    catch {
        return false;
    }
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Write skill content to the appropriate locations for each requested agent.
 *
 * @param skillContent - The full skill markdown content.
 * @param skillName    - Skill identifier used in file names and section markers.
 * @param projectPath  - Absolute path to the project root.
 * @param agents       - Which AI agents to write files for.
 * @returns Array of written file descriptors (one per agent).
 */
export async function writeSkillForAgents(skillContent, skillName, projectPath, agents) {
    const writes = agents.map(async (agent) => {
        const filePath = agentSkillPath(projectPath, skillName, agent);
        if (isAppendMode(agent)) {
            return appendToSharedFile(filePath, skillName, skillContent, agent);
        }
        return writeStandaloneFile(filePath, skillContent, agent);
    });
    const settled = await Promise.allSettled(writes);
    const results = [];
    for (const outcome of settled) {
        if (outcome.status === 'fulfilled') {
            results.push(outcome.value);
        }
        // Silently skip failures — partial writes still deliver value
    }
    return results;
}
//# sourceMappingURL=multi-agent-writer.js.map