"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileAgentProvider = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class FileAgentProvider {
    roots;
    constructor(roots = []) {
        this.roots = roots;
    }
    load() {
        const agents = [];
        for (const root of this.roots) {
            if (!fs.existsSync(root))
                continue;
            const files = this.walk(root).filter(f => f.endsWith('.md'));
            for (const file of files) {
                const fullPath = path.isAbsolute(file) ? file : path.join(root, file);
                const content = fs.readFileSync(fullPath, 'utf-8');
                agents.push(this.parseAgent(path.basename(fullPath), fullPath, content));
            }
        }
        return agents;
    }
    walk(dir) {
        const out = [];
        for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
            const full = path.join(dir, entry.name);
            if (entry.isDirectory())
                out.push(...this.walk(full));
            else
                out.push(full);
        }
        return out;
    }
    parseAgent(fileName, fullPath, content) {
        const id = path.basename(path.dirname(fullPath)) === '.' || fileName !== 'AGENT.md'
            ? fileName.replace(/\.md$/, '')
            : path.basename(path.dirname(fullPath));
        const nameMatch = content.match(/name:\s*(.+)/i);
        const roleMatch = content.match(/role:\s*"([^"]+)"/i) || content.match(/role:\s*(.+)/i);
        const squadMatch = content.match(/squad:\s*([^\n]+)/i);
        const tags = [];
        const tagRegex = /tags:\s*\[([^\]]+)\]/gi;
        let m;
        while ((m = tagRegex.exec(content)) !== null) {
            m[1].split(',').map(s => s.trim().replace(/^'+|'+$/g, '')).filter(Boolean).forEach(s => { if (!tags.includes(s))
                tags.push(s); });
        }
        const capabilities = [];
        const addCap = (s) => { if (s && !capabilities.includes(s))
            capabilities.push(s); };
        const whenToUseMatch = content.match(/whenToUse:\s*"([^"]+)"/i);
        if (whenToUseMatch)
            whenToUseMatch[1].split(',').map(s => s.trim()).filter(Boolean).forEach(addCap);
        const focusMatch = content.match(/focus:\s*"([^"]+)"/i);
        if (focusMatch)
            focusMatch[1].split(',').map(s => s.trim()).filter(Boolean).forEach(addCap);
        tags.forEach(addCap);
        let priority = 1;
        if (id.startsWith('ai-builder-'))
            priority += 5;
        if (id.includes('orchestrator'))
            priority += 3;
        if (id.includes('review'))
            priority += 2;
        if (id.includes('builder'))
            priority += 2;
        return { id, name: nameMatch?.[1]?.trim() || id, role: roleMatch?.[1]?.trim() || 'specialist', capabilities, sourcePath: fullPath, priority, squad: squadMatch?.[1]?.trim(), tags };
    }
}
exports.FileAgentProvider = FileAgentProvider;
