// tools/suggest-tooling/skills-fetcher.ts — SPEC-022: skills.sh catalog fetcher
// Fetches and recommends AI agent skills based on project stack.
// Uses SPEC-003 web fetcher (fetchUrl) for HTTP — no direct node:https usage.
import { fetchUrl } from '../../engine/web-fetcher/http-client.js';
export function isMcpServer(knowledge) {
    const stackLower = knowledge.stack.map((s) => s.toLowerCase());
    const toolingStr = JSON.stringify(knowledge.tooling).toLowerCase();
    return (stackLower.some((s) => s.includes('mcp') || s.includes('@modelcontextprotocol')) ||
        toolingStr.includes('modelcontextprotocol') ||
        toolingStr.includes('@modelcontextprotocol'));
}
export async function fetchRecommendedSkills(knowledge) {
    const skills = [];
    // Always add mcp-builder first for MCP server projects
    if (isMcpServer(knowledge)) {
        skills.push({
            name: 'mcp-builder',
            description: 'Best practices and conventions for building high-quality MCP servers',
            installCommand: 'npx skills add mcp-builder',
            justification: 'Project detected as an MCP server (@modelcontextprotocol detected in stack/tooling)',
        });
    }
    // Fetch skills.sh catalog in real time (with graceful fallback to hardcoded skills)
    const catalogSkills = await fetchSkillsShCatalog(knowledge.stack, knowledge.language);
    if (catalogSkills.length > 0) {
        skills.push(...catalogSkills);
    }
    else {
        // Fallback: use hardcoded relevant skills when catalog is unavailable or empty
        skills.push(...buildFallbackSkills(knowledge));
    }
    // Deduplicate by name
    const seen = new Set();
    return skills.filter((s) => {
        if (seen.has(s.name)) {
            return false;
        }
        seen.add(s.name);
        return true;
    });
}
async function fetchSkillsShCatalog(stack, language) {
    const stackParam = encodeURIComponent([...stack, language].join(',').toLowerCase());
    const url = `https://skills.sh/api/catalog?stack=${stackParam}`;
    try {
        const response = await fetchUrl(url);
        if (!response) {
            return [];
        }
        const parsed = JSON.parse(response.content);
        return (parsed.skills ?? []).slice(0, 5).map((s) => ({
            name: s.name,
            description: s.description ?? `Skill: ${s.name}`,
            installCommand: s.installCommand ?? `npx skills add ${s.name}`,
            justification: `Relevant to your ${language} stack`,
        }));
    }
    catch {
        /* v8 ignore next */ // JSON.parse errors are unlikely with mocked fetchUrl in tests
        return [];
    }
}
function buildFallbackSkills(knowledge) {
    const skills = [];
    const stackLower = knowledge.stack.map((s) => s.toLowerCase());
    const lang = knowledge.language.toLowerCase();
    if (lang === 'typescript' || lang === 'javascript') {
        skills.push({
            name: 'typescript-patterns',
            description: 'TypeScript best practices, strict mode patterns, and type safety conventions',
            installCommand: 'npx skills add typescript-patterns',
            justification: `Project uses ${knowledge.language}`,
        });
    }
    if (stackLower.some((s) => s.includes('react'))) {
        skills.push({
            name: 'react-patterns',
            description: 'React component patterns, hooks best practices, and performance optimization',
            installCommand: 'npx skills add react-patterns',
            justification: 'React detected in project stack',
        });
    }
    if (stackLower.some((s) => s.includes('next'))) {
        skills.push({
            name: 'nextjs-patterns',
            description: 'Next.js App Router patterns, server components, and deployment best practices',
            installCommand: 'npx skills add nextjs-patterns',
            justification: 'Next.js detected in project stack',
        });
    }
    if (stackLower.some((s) => s.includes('python')) ||
        knowledge.language.toLowerCase() === 'python') {
        skills.push({
            name: 'python-patterns',
            description: 'Python best practices, type hints, async patterns, and testing conventions',
            installCommand: 'npx skills add python-patterns',
            justification: 'Python detected in project stack',
        });
    }
    return skills;
}
//# sourceMappingURL=skills-fetcher.js.map