/**
 * Filesystem skill content loader.
 *
 * Reads a Claude-style skill from `~/.claude/skills/<name>/` and
 * returns its full text content (SKILL.md without frontmatter +
 * every file under reference/) concatenated as a single system-
 * prompt prefix. Used by the prompt-runner when the resolved AI
 * provider is NOT claude-cli (claude-cli auto-loads skills via its
 * native mechanism; other providers — anthropic, openai-compatible,
 * stub — see only what's in the prompt body, so we have to inline
 * the skill ourselves).
 *
 * Single source of truth: the skill content lives at one filesystem
 * location. claude-cli reads it directly; non-claude-cli providers
 * read it through this loader and receive the same content.
 *
 * Project-local skills (`<cwd>/.claude/skills/<name>/`) take
 * precedence over global skills (`~/.claude/skills/<name>/`) — mirrors
 * Claude Code's own resolution order.
 *
 * Returns null when the skill isn't installed anywhere — callers fall
 * back to the thin prompt as-is.
 */
import { existsSync, readFileSync, readdirSync, statSync } from 'fs';
import { homedir } from 'os';
import { join, isAbsolute } from 'path';
/**
 * Load a skill by name. Search order:
 *   1. `<cwd>/.claude/skills/<name>/` (project-local)
 *   2. `~/.claude/skills/<name>/` (global)
 *
 * Returns null when the skill is not installed in either location.
 */
export function loadSkillContent(name, cwd) {
    const projectDir = join(cwd ?? process.cwd(), '.claude', 'skills', name);
    const globalDir = join(homedir(), '.claude', 'skills', name);
    const skillDir = existsSync(projectDir) ? projectDir : existsSync(globalDir) ? globalDir : null;
    if (!skillDir)
        return null;
    const skillMdPath = join(skillDir, 'SKILL.md');
    if (!existsSync(skillMdPath))
        return null;
    const files = [];
    const sections = [];
    // 1. SKILL.md — strip frontmatter, prepend.
    const skillMdRaw = readFileSync(skillMdPath, 'utf8');
    const skillMdBody = stripFrontmatter(skillMdRaw);
    files.push('SKILL.md');
    sections.push(`# Skill: ${name} — SKILL.md\n\n${skillMdBody.trim()}`);
    // 2. reference/*.md — append each file, in directory order.
    const referenceDir = join(skillDir, 'reference');
    if (existsSync(referenceDir) && statSync(referenceDir).isDirectory()) {
        const refFiles = readdirSync(referenceDir)
            .filter((f) => f.endsWith('.md'))
            .sort();
        for (const refFile of refFiles) {
            const refPath = join(referenceDir, refFile);
            const refContent = readFileSync(refPath, 'utf8').trim();
            files.push(`reference/${refFile}`);
            sections.push(`# Skill: ${name} — reference/${refFile}\n\n${refContent}`);
        }
    }
    return {
        name,
        skillDir,
        text: sections.join('\n\n---\n\n'),
        files,
    };
}
/**
 * Strip leading YAML frontmatter (delimited by `---\n` markers) from a
 * markdown file. Returns the body. If no frontmatter is present,
 * returns the original content.
 */
function stripFrontmatter(text) {
    if (!text.startsWith('---\n'))
        return text;
    const closingIdx = text.indexOf('\n---\n', 4);
    if (closingIdx === -1)
        return text;
    return text.slice(closingIdx + 5); // skip "\n---\n"
}
/**
 * Convenience: is the skill installed somewhere we'd find it?
 * Used by callers that want to short-circuit content loading for the
 * cheap "absent" path.
 */
export function isSkillInstalled(name, cwd) {
    const projectDir = join(cwd ?? process.cwd(), '.claude', 'skills', name);
    const globalDir = join(homedir(), '.claude', 'skills', name);
    return existsSync(join(projectDir, 'SKILL.md')) || existsSync(join(globalDir, 'SKILL.md'));
}
// Marker import to keep `isAbsolute` linked in case future helpers
// need explicit-path resolution (e.g. SPECVERSE_SKILL_DIR override).
void isAbsolute;
//# sourceMappingURL=skill-loader.js.map