/**
 * SkillsMP (Skills Marketplace) integration.
 * Delegates to @cleocode/caamp MarketplaceClient for core marketplace operations.
 *
 * @epic T4454
 * @task T4521
 */
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { searchSkills as caampSearchSkills, MarketplaceClient } from '@cleocode/caamp';
import { getProjectRoot } from '../paths.js';
/**
 * Load SkillsMP configuration from skillsmp.json.
 * @task T4521
 */
export function loadConfig(cwd) {
    const projectRoot = getProjectRoot(cwd);
    const configFile = join(projectRoot, '.cleo', 'skillsmp.json');
    if (!existsSync(configFile))
        return null;
    try {
        const data = JSON.parse(readFileSync(configFile, 'utf-8'));
        if (data.enabled !== true)
            return null;
        return {
            enabled: true,
            cacheDir: data.cacheDir ?? '',
        };
    }
    catch {
        return null;
    }
}
// ============================================================================
// API Operations (delegated to CAAMP)
// ============================================================================
/**
 * Convert a CAAMP MarketplaceResult to CLEO's MarketplaceSkill interface.
 */
function toMarketplaceSkill(result) {
    return {
        id: result.scopedName,
        name: result.name,
        description: result.description,
        version: '0.0.0', // MarketplaceResult does not carry version
        author: result.author,
        tags: result.source ? [result.source] : [],
        downloadUrl: result.githubUrl,
    };
}
/**
 * Search the skills marketplace.
 * Delegates to CAAMP's searchSkills for the actual API call.
 * @task T4521
 */
export async function searchSkills(query, _config) {
    try {
        const results = await caampSearchSkills(query);
        return results.map(toMarketplaceSkill);
    }
    catch {
        return [];
    }
}
/**
 * Get a specific skill from the marketplace.
 * Uses CAAMP's MarketplaceClient for retrieval.
 * @task T4521
 */
export async function getSkill(skillId, _config) {
    try {
        const client = new MarketplaceClient();
        const result = await client.getSkill(skillId);
        if (!result)
            return null;
        return toMarketplaceSkill(result);
    }
    catch {
        return null;
    }
}
/**
 * Check if the marketplace is enabled and reachable.
 * @task T4521
 */
export function isEnabled(cwd) {
    const config = loadConfig(cwd);
    return config?.enabled === true;
}
//# sourceMappingURL=marketplace.js.map