"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.listAgentIds = listAgentIds;
exports.listSkillIds = listSkillIds;
exports.listSquadIds = listSquadIds;
exports.listMcpIds = listMcpIds;
exports.getEntryDetail = getEntryDetail;
// src/cli/CatalogReader.ts
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
function parseFrontMatter(content) {
    const match = content.match(/^---\n([\s\S]*?)\n---/);
    if (!match)
        return {};
    const result = {};
    for (const line of match[1].split('\n')) {
        const colonIdx = line.indexOf(':');
        if (colonIdx > 0) {
            const key = line.slice(0, colonIdx).trim();
            const val = line.slice(colonIdx + 1).trim().replace(/^["']|["']$/g, '');
            if (key)
                result[key] = val;
        }
    }
    return result;
}
const TAG_NOISE = new Set(['lara', 'agente', 'skill', 'squad']);
function extractTags(content) {
    const m = content.match(/\btags:\s*\[([^\]]+)\]/);
    if (!m)
        return [];
    return m[1].split(',').map(t => t.trim().replace(/^["']|["']$/g, '')).filter(t => t && !TAG_NOISE.has(t));
}
function catalogDirs(catalogRoot, section) {
    const dir = path.join(catalogRoot, '.catalog', section);
    if (!fs.existsSync(dir))
        return [];
    try {
        return fs.readdirSync(dir)
            .filter(e => !e.startsWith('test-') && !e.startsWith('.'))
            .filter(e => { try {
            return fs.statSync(path.join(dir, e)).isDirectory();
        }
        catch {
            return false;
        } })
            .sort();
    }
    catch {
        return [];
    }
}
function readFirstFile(dir) {
    for (const name of ['AGENT.md', 'SKILL.md', 'SQUAD.md', 'index.md']) {
        const p = path.join(dir, name);
        try {
            if (fs.existsSync(p))
                return fs.readFileSync(p, 'utf-8');
        }
        catch { /* skip */ }
    }
    return null;
}
function listAgentIds(catalogRoot) {
    return catalogDirs(catalogRoot, 'agents');
}
function listSkillIds(catalogRoot) {
    return catalogDirs(catalogRoot, 'skills');
}
function listSquadIds(catalogRoot) {
    return catalogDirs(catalogRoot, 'squads');
}
function listMcpIds(catalogRoot) {
    return catalogDirs(catalogRoot, 'mcps');
}
function getEntryDetail(catalogRoot, section, id) {
    const catalogBase = path.resolve(catalogRoot, '.catalog', section);
    const dir = path.resolve(catalogBase, id);
    if (!dir.startsWith(catalogBase + path.sep))
        return null;
    if (!fs.existsSync(dir))
        return null;
    const content = readFirstFile(dir);
    if (!content)
        return null;
    const fm = parseFrontMatter(content);
    const tags = extractTags(content);
    const desc = fm.description ? fm.description.slice(0, 150) : undefined;
    return { id, name: fm.name || id, role: fm.role || undefined, description: desc, tags };
}
