import { RegistryError } from './types.js';
const INDEX_URL = 'https://raw.githubusercontent.com/skillssh/skills.sh/HEAD/data/index.json';
export const skillsShAdapter = {
    descriptor: {
        id: 'skills-sh',
        label: 'skills.sh directory',
        kinds: ['skill'],
        requiresAuth: false,
        enabled: true
    },
    async search(query, limit = 50) {
        let data;
        try {
            const res = await fetch(INDEX_URL, { headers: { accept: 'application/json' } });
            if (!res.ok)
                return [];
            data = (await res.json());
        }
        catch {
            return [];
        }
        const q = query.trim().toLowerCase();
        const records = data.skills ?? [];
        return records
            .filter((record) => Boolean(record.name || record.slug))
            .filter((record) => !q || (record.name ?? '').toLowerCase().includes(q) || (record.description ?? '').toLowerCase().includes(q))
            .slice(0, limit)
            .map((record) => ({
            id: record.slug ?? record.name,
            source: 'skills-sh',
            kind: 'skill',
            name: record.name ?? record.slug,
            title: record.name ?? null,
            description: record.description ?? null,
            version: null,
            repository: record.repo ?? null,
            homepage: 'https://www.skills.sh',
            installManifest: record
        }));
    },
    async resolveInstall(entry) {
        const raw = entry.installManifest;
        if (!raw.repo)
            throw new RegistryError('skills.sh entry missing repo url', 422);
        const repoMatch = raw.repo.match(/github\.com\/([^/]+)\/([^/.]+)/);
        if (!repoMatch)
            throw new RegistryError('skills.sh repo must point to GitHub', 422);
        const [, owner, repo] = repoMatch;
        const skillPath = raw.skillPath ?? 'SKILL.md';
        const url = `https://raw.githubusercontent.com/${owner}/${repo}/HEAD/${skillPath}`;
        const res = await fetch(url);
        if (!res.ok)
            throw new RegistryError(`skills.sh skill fetch failed: ${res.status}`);
        const text = await res.text();
        return {
            name: entry.name,
            skillMarkdown: text,
            source: {
                registry: 'skills-sh',
                ref: `${owner}/${repo}`,
                version: null,
                url: `https://github.com/${owner}/${repo}`
            }
        };
    }
};
//# sourceMappingURL=skills-sh.js.map