import { join } from "node:path";
import { resolveConfigLevels } from "./paths.js";
import { parseSkill } from "./skill_parser.js";
function loadSkillsFromDir(dir, deps) {
    if (!deps.fs.exists(dir)) {
        return { skills: [], errors: [] };
    }
    let entries;
    try {
        entries = deps.fs.listDir(dir);
    }
    catch {
        return { skills: [], errors: [`failed to read directory: ${dir}`] };
    }
    const skills = [];
    const errors = [];
    for (const entry of entries) {
        const skillDir = join(dir, entry);
        let stats;
        try {
            stats = deps.fs.stat(skillDir);
        }
        catch {
            errors.push(`failed to stat path: ${skillDir}`);
            continue;
        }
        if (!stats.isDirectory()) {
            continue;
        }
        const skillFile = join(skillDir, "SKILL.md");
        if (!deps.fs.exists(skillFile)) {
            continue;
        }
        let content = "";
        try {
            content = deps.fs.readFile(skillFile);
        }
        catch {
            errors.push(`failed to read file: ${skillFile}`);
            continue;
        }
        const result = parseSkill(skillFile, content);
        if (result.skill) {
            skills.push(result.skill);
            continue;
        }
        if (result.error) {
            errors.push(result.error);
        }
    }
    return { skills, errors };
}
export async function loadSkillsContent(_config, options) {
    const deps = options.deps;
    const levels = options.levels;
    const skillsByName = new Map();
    const errors = [];
    for (const level of levels) {
        for (const dir of [level.skillsDir, level.agentsSkillsDir]) {
            const result = loadSkillsFromDir(dir, deps);
            errors.push(...result.errors);
            for (const skill of result.skills) {
                skillsByName.set(skill.name.toLowerCase(), skill);
            }
        }
    }
    const skills = Array.from(skillsByName.values()).sort((a, b) => a.name.localeCompare(b.name, undefined, { sensitivity: "base" }));
    return { skills, errors };
}
export async function loadSkillsForPromptContext(args) {
    const levels = resolveConfigLevels(args.deps, { cwd: args.cwd });
    return loadSkillsContent(args.config, {
        deps: args.deps,
        levels,
    });
}
//# sourceMappingURL=skills_loader.js.map