import { getSkillBootstrapConfig } from '../../storage/skill-bootstrap-store.js';
import { detectProjectStack, matchTechsToAvailable } from './stack-matcher.js';
const FETCH_TIMEOUT_MS = 5000;
const AWESOME_CURSORRULES_API = 'https://api.github.com/repos/PatrickJS/awesome-cursorrules/contents/rules';
const AWESOME_CURSORRULES_RAW = 'https://raw.githubusercontent.com/PatrickJS/awesome-cursorrules/master/rules';
const SKILLS_SH_API = 'https://registry.skills.sh/api/skills';
async function fetchWithTimeout(url) {
    const controller = new AbortController();
    const timeout = setTimeout(() => {
        controller.abort();
    }, FETCH_TIMEOUT_MS);
    try {
        const res = await fetch(url, { signal: controller.signal });
        return res;
    }
    catch {
        return null;
    }
    finally {
        clearTimeout(timeout);
    }
}
async function fetchGitHubDirContents(apiUrl) {
    const res = await fetchWithTimeout(apiUrl);
    if (!res?.ok) {
        return [];
    }
    try {
        const data = (await res.json());
        if (!Array.isArray(data)) {
            return [];
        }
        return data.filter((entry) => entry.type === 'dir').map((entry) => entry.name);
    }
    catch {
        return [];
    }
}
export async function fetchAvailableTechs(source) {
    if (source.type === 'awesome-cursorrules') {
        return fetchGitHubDirContents(AWESOME_CURSORRULES_API);
    }
    if (source.type === 'github-custom') {
        if (!source.repo) {
            return [];
        }
        const subdir = source.subdir ?? 'rules';
        const apiUrl = `https://api.github.com/repos/${source.repo}/contents/${subdir}`;
        return fetchGitHubDirContents(apiUrl);
    }
    // skills-sh: no public list available
    return [];
}
export async function fetchSkillContent(source, tech) {
    if (source.type === 'awesome-cursorrules') {
        const url = `${AWESOME_CURSORRULES_RAW}/${tech}/.cursorrules`;
        const res = await fetchWithTimeout(url);
        if (!res?.ok) {
            return null;
        }
        try {
            return await res.text();
        }
        catch {
            return null;
        }
    }
    if (source.type === 'skills-sh') {
        const url = `${SKILLS_SH_API}?q=${encodeURIComponent(tech)}`;
        const res = await fetchWithTimeout(url);
        if (!res?.ok) {
            return null;
        }
        try {
            const text = await res.text();
            return text.length > 0 ? text : null;
        }
        catch {
            return null;
        }
    }
    // github-custom
    if (!source.repo) {
        return null;
    }
    const subdir = source.subdir ?? 'rules';
    const url = `https://raw.githubusercontent.com/${source.repo}/HEAD/${subdir}/${tech}/.cursorrules`;
    const res = await fetchWithTimeout(url);
    if (!res?.ok) {
        return null;
    }
    try {
        return await res.text();
    }
    catch {
        return null;
    }
}
/**
 * SPEC-451: Discover skills from configured registries for a project.
 * Used by init_project to surface top skills immediately after initialization.
 * Returns lightweight skill references (no content blob).
 */
export async function discoverSkillsForInit(projectPath, stackOverride) {
    const config = await getSkillBootstrapConfig(projectPath);
    const detectedStack = stackOverride ?? (await detectProjectStack(projectPath));
    const results = [];
    for (const source of config.sources) {
        if (!source.enabled) {
            continue;
        }
        const availableTechs = await fetchAvailableTechs(source);
        const techsToCheck = source.type === 'skills-sh'
            ? detectedStack
            : matchTechsToAvailable(detectedStack, availableTechs);
        for (const tech of techsToCheck) {
            const url = buildSkillUrl(source, tech);
            results.push({ tech, sourceName: source.name, url });
        }
    }
    return results;
}
function buildSkillUrl(source, tech) {
    if (source.type === 'awesome-cursorrules') {
        return `https://raw.githubusercontent.com/PatrickJS/awesome-cursorrules/master/rules/${tech}/.cursorrules`;
    }
    if (source.type === 'github-custom' && source.repo) {
        return `https://raw.githubusercontent.com/${source.repo}/HEAD/${source.subdir ?? 'rules'}/${tech}/.cursorrules`;
    }
    return `https://registry.skills.sh/api/skills?q=${encodeURIComponent(tech)}`;
}
//# sourceMappingURL=registry-fetcher.js.map