import fs from "node:fs";
import path from "node:path";
import { bundledRuntimeSkillContent, discoverSkills } from "./discovery.js";
import { parseSkillDocument } from "./manifest.js";
export function loadSkill(projectPath, dataDir, name) {
    const skill = discoverSkills(projectPath, dataDir).find((candidate) => candidate.name === name);
    if (!skill)
        return null;
    if (skill.path.startsWith("builtin:")) {
        const prompt = bundledRuntimeSkillContent(skill.name) ?? "";
        const parsed = parseSkillDocument(prompt);
        return {
            ...skill,
            prompt,
            frontmatter: parsed.frontmatter,
        };
    }
    const skillMd = path.join(skill.path, "SKILL.md");
    const manifestJson = path.join(skill.path, "skill.json");
    const prompt = fs.existsSync(skillMd) ? fs.readFileSync(skillMd, "utf8") : "";
    const parsed = parseSkillDocument(prompt);
    return {
        ...skill,
        prompt,
        frontmatter: parsed.frontmatter,
        manifest: fs.existsSync(manifestJson)
            ? JSON.parse(fs.readFileSync(manifestJson, "utf8"))
            : undefined,
    };
}
