// packages/plugin-awf/src/utils/counts.ts
// Dynamic counting for installed skills, workflows, and rules
// Replaces hardcoded counts (93, 41, 21) with runtime directory scans
import { existsSync, readdirSync, statSync } from "node:fs";
import { join } from "node:path";
/**
 * Count installed skills from .agent/skills/ directory.
 * Skills are directories containing a SKILL.md file, organized by category.
 * Structure: .agent/skills/{category}/{skill-name}/SKILL.md
 */
export function countInstalledSkills(agentDir) {
    const skillsDir = join(agentDir, "skills");
    if (!existsSync(skillsDir))
        return 0;
    let count = 0;
    try {
        const categories = readdirSync(skillsDir).filter((d) => statSync(join(skillsDir, d)).isDirectory());
        for (const cat of categories) {
            const catDir = join(skillsDir, cat);
            const skills = readdirSync(catDir).filter((d) => {
                const skillDir = join(catDir, d);
                return (statSync(skillDir).isDirectory() &&
                    existsSync(join(skillDir, "SKILL.md")));
            });
            count += skills.length;
        }
    }
    catch {
        // Non-critical — return 0 on error
    }
    return count;
}
/**
 * Count installed workflows from .agent/workflows/ directory.
 * Workflows are .md files in the workflows directory.
 */
export function countInstalledWorkflows(agentDir) {
    const workflowsDir = join(agentDir, "workflows");
    if (!existsSync(workflowsDir))
        return 0;
    try {
        return readdirSync(workflowsDir).filter((f) => f.endsWith(".md")).length;
    }
    catch {
        return 0;
    }
}
/**
 * Count installed rule modules from .agent/rules/modules/ directory.
 * Rules are .yaml files in the modules directory.
 */
export function countInstalledRules(agentDir) {
    const modulesDir = join(agentDir, "rules", "modules");
    if (!existsSync(modulesDir))
        return 0;
    try {
        return readdirSync(modulesDir).filter((f) => f.endsWith(".yaml")).length;
    }
    catch {
        return 0;
    }
}
