import fs from 'node:fs';
import path from 'node:path';
import { parse as parseYaml } from 'yaml';
export function readSquadManifest(squadDir) {
    const yamlPath = path.join(squadDir, 'squad.yaml');
    if (!fs.existsSync(yamlPath)) {
        throw new Error(`squad.yaml not found in ${squadDir}`);
    }
    const raw = fs.readFileSync(yamlPath, 'utf-8');
    const parsed = parseYaml(raw);
    const squad = parsed?.squad;
    if (!squad) {
        throw new Error('Invalid squad.yaml: missing "squad" root key');
    }
    return {
        code: String(squad.code ?? path.basename(squadDir)),
        name: String(squad.name ?? squad.code ?? 'Unnamed Squad'),
        description: String(squad.description ?? ''),
        icon: String(squad.icon ?? 'folder'),
        version: String(squad.version ?? '1.0.0'),
        category: String(squad.category ?? 'general'),
        tags: Array.isArray(squad.tags) ? squad.tags.map(String) : [],
        author: squad.author ? String(squad.author) : undefined,
        license: squad.license ? String(squad.license) : undefined,
    };
}
export function collectSquadFiles(squadDir) {
    const files = [];
    function walk(dir, base = '') {
        const entries = fs.readdirSync(dir, { withFileTypes: true });
        for (const entry of entries) {
            const fullPath = path.join(dir, entry.name);
            const relativePath = base ? `${base}/${entry.name}` : entry.name;
            if (entry.isDirectory()) {
                walk(fullPath, relativePath);
            }
            else if (entry.isFile() && !entry.name.startsWith('.')) {
                files.push({
                    path: relativePath,
                    content: fs.readFileSync(fullPath, 'utf-8'),
                });
            }
        }
    }
    walk(squadDir);
    return files;
}
export function validateSquadForPublish(squadDir) {
    const errors = [];
    // Check squad.yaml exists
    const yamlPath = path.join(squadDir, 'squad.yaml');
    if (!fs.existsSync(yamlPath)) {
        errors.push('squad.yaml not found');
        return { valid: false, errors };
    }
    try {
        const manifest = readSquadManifest(squadDir);
        if (!manifest.name)
            errors.push('Missing squad.name');
        if (!manifest.description)
            errors.push('Missing squad.description');
        if (!manifest.code)
            errors.push('Missing squad.code');
        // Check code format (lowercase, no spaces)
        if (!/^[a-z0-9-]+$/.test(manifest.code)) {
            errors.push('squad.code must be lowercase with hyphens only (e.g., "marketing-squad")');
        }
        // Check agents directory
        const agentsDir = path.join(squadDir, 'agents');
        if (fs.existsSync(agentsDir)) {
            const agentFiles = fs.readdirSync(agentsDir).filter(f => f.endsWith('.md'));
            if (agentFiles.length === 0) {
                errors.push('agents/ directory exists but contains no .md files');
            }
        }
    }
    catch (err) {
        errors.push(`Failed to parse squad.yaml: ${err instanceof Error ? err.message : String(err)}`);
    }
    return { valid: errors.length === 0, errors };
}
export function extractSquadToDir(files, targetDir) {
    fs.mkdirSync(targetDir, { recursive: true });
    for (const file of files) {
        const filePath = path.join(targetDir, file.path);
        const fileDir = path.dirname(filePath);
        fs.mkdirSync(fileDir, { recursive: true });
        fs.writeFileSync(filePath, file.content, 'utf-8');
    }
}
//# sourceMappingURL=squad-registry.js.map