// engine/plugin-installer/marketplace-registrar.ts — Ensure Anthropic marketplace registered (SPEC-596)
//
// Calls `claude plugin marketplace add anthropics/claude-code` idempotently.
// Uses `claude plugin marketplace list` to check if already registered.
// CLI calls use execFile (NOT shell) to prevent injection.
import { promisify } from 'node:util';
import { execFile as nodeExecFile } from 'node:child_process';
const DEFAULT_EXEC = promisify(nodeExecFile);
const CLI_TIMEOUT_MS = 5000;
const MARKETPLACE_SLUG = 'anthropics/claude-code';
/** Check if the Anthropic marketplace is already registered. */
async function isMarketplaceRegistered(exec) {
    try {
        const { stdout } = await exec('claude', ['plugin', 'marketplace', 'list'], {
            timeout: CLI_TIMEOUT_MS,
        });
        return stdout.includes(MARKETPLACE_SLUG) || stdout.includes('anthropics');
    }
    catch {
        // CLI not available or command failed — assume not registered
        return false;
    }
}
/**
 * Ensure the Anthropic Claude Code marketplace is registered.
 * Idempotent: checks before registering. Safe when `claude` CLI is absent.
 *
 * @param dryRun - When true, skip CLI execution and report intent only.
 * @param exec - Optional execFile override for testing.
 */
export async function ensureAnthropicMarketplaceRegistered(dryRun = false, exec = DEFAULT_EXEC) {
    if (dryRun) {
        return { alreadyRegistered: false, registered: false, skipped: true };
    }
    const alreadyRegistered = await isMarketplaceRegistered(exec);
    if (alreadyRegistered) {
        return { alreadyRegistered: true, registered: false, skipped: false };
    }
    try {
        await exec('claude', ['plugin', 'marketplace', 'add', MARKETPLACE_SLUG], {
            timeout: CLI_TIMEOUT_MS,
        });
        return { alreadyRegistered: false, registered: true, skipped: false };
    }
    catch {
        // Registration failed — non-fatal, skip silently
        return { alreadyRegistered: false, registered: false, skipped: true };
    }
}
//# sourceMappingURL=marketplace-registrar.js.map