import { getRootLogger } from '../utils/logger.js';
import { discoverSkills, loadSkill } from './loader.js';
const logger = getRootLogger().child({ component: 'SkillRegistry' });
export class SkillRegistry {
    skills = new Map();
    async register(dirPath, level = 'metadata') {
        const result = await loadSkill(dirPath, level);
        this.skills.set(result.skill.metadata.name, result.skill);
        return result.skill;
    }
    async registerAll(parentDir, level = 'metadata') {
        const dirs = await discoverSkills(parentDir);
        const results = [];
        for (const dir of dirs) {
            const skill = await this.register(dir, level);
            results.push(skill);
        }
        logger.debug('Registered skills from directory', {
            parentDir,
            count: results.length,
            names: results.map((s) => s.metadata.name),
        });
        return results;
    }
    get(name) {
        return this.skills.get(name);
    }
    async load(name, level = 'full') {
        const existing = this.skills.get(name);
        if (!existing)
            return undefined;
        if (level === 'metadata' || existing.body) {
            return {
                skill: existing,
                disclosureLevel: existing.body ? 'full' : 'metadata',
                tokenEstimate: 0,
            };
        }
        const result = await loadSkill(existing.dirPath, level);
        this.skills.set(name, result.skill);
        return result;
    }
    list() {
        return [...this.skills.values()];
    }
    get size() {
        return this.skills.size;
    }
    has(name) {
        return this.skills.has(name);
    }
}
export async function resolveSkillChain(categorySkillsDir, agentSkillsDir, level = 'metadata') {
    const categoryRegistry = new SkillRegistry();
    const agentRegistry = new SkillRegistry();
    const inherited = categorySkillsDir
        ? await categoryRegistry.registerAll(categorySkillsDir, level)
        : [];
    const own = agentSkillsDir ? await agentRegistry.registerAll(agentSkillsDir, level) : [];
    const resolvedMap = new Map();
    for (const skill of inherited) {
        resolvedMap.set(skill.metadata.name, skill);
    }
    for (const skill of own) {
        resolvedMap.set(skill.metadata.name, skill);
    }
    const resolved = [...resolvedMap.values()];
    logger.debug('Resolved skill chain', {
        inherited: inherited.map((s) => s.metadata.name),
        own: own.map((s) => s.metadata.name),
        resolved: resolved.map((s) => s.metadata.name),
    });
    return { inherited, own, resolved };
}
//# sourceMappingURL=registry.js.map