"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExternalCatalogRegistry = void 0;
const ToolsetGuard_1 = require("./toolset/ToolsetGuard");
class ExternalCatalogRegistry {
    list() {
        return [
            { id: 'skills-sh', type: 'skills', url: 'https://skills.sh/', trust: 'reference', notes: 'Skill discovery reference' },
            { id: 'clawhub', type: 'skills', url: 'https://clawhub.ai/', trust: 'reference', notes: 'Skill/agent references' },
            { id: 'mcpmarket', type: 'mcps', url: 'https://mcpmarket.com/', trust: 'reference', notes: 'MCP catalog reference' },
            { id: 'mcpservers', type: 'mcps', url: 'https://mcpservers.org/', trust: 'reference', notes: 'MCP catalog reference' },
            { id: 'github', type: 'skills', url: 'https://github.com/', trust: 'import-allowed', notes: 'Allowlisted import host with guard scan' }
        ];
    }
    /**
     * Initiate an import from an external catalog source.
     *
     * Story 5.4 — OpenLife v1.3 Agent OS Integration.
     *
     * Hard-gated by trust='import-allowed'. Reference-only sources are
     * refused. The actual download/scan/install path goes through
     * SecurityDownloadGuard — this method validates the policy decision
     * only; the caller wires the actual fetch.
     *
     * Returns a structured ImportResult; never throws.
     */
    import(sourceId, packId) {
        if (!sourceId || !packId) {
            return { ok: false, error: 'invalid_arguments' };
        }
        const source = this.list().find((s) => s.id === sourceId);
        if (!source) {
            return { ok: false, error: 'source_not_found', sourceId };
        }
        if (source.trust !== 'import-allowed') {
            return {
                ok: false,
                error: 'source_is_reference_only',
                sourceId,
                trust: source.trust,
                notes: `Source '${sourceId}' is reference-only. Use the URL manually and bring vetted manifests into .catalog/.`,
            };
        }
        // Policy decision OK. v1.4: if `url` opt provided, run the
        // download+scan pipeline; else return policy-only OK (caller will
        // do the fetch).
        return {
            ok: true,
            sourceId,
            packId,
            trust: source.trust,
            notes: 'Policy allows import. Use importAndFetch() for the real download+scan pipeline.',
        };
    }
    /**
     * Story 9.2 — wires ExternalCatalogRegistry.import policy decision to
     * SecurityDownloadGuard.downloadAndScan. Single-method "do the right
     * thing" entry for CLI handlers.
     *
     * Returns the same ImportResult shape augmented with downloadedTo
     * when successful. Never throws.
     */
    async importAndFetch(sourceId, packId, url, targetDir) {
        (0, ToolsetGuard_1.assertToolsetAllowed)('mcp', 'ExternalCatalogRegistry.importAndFetch');
        const policy = this.import(sourceId, packId);
        if (!policy.ok)
            return policy;
        // Lazy require to avoid circular dependencies.
        // eslint-disable-next-line @typescript-eslint/no-var-requires
        const { SecurityDownloadGuard } = require('./SecurityDownloadGuard');
        const guard = new SecurityDownloadGuard();
        const r = await guard.downloadAndScan(url, targetDir);
        if (!r.ok) {
            return { ok: false, sourceId, packId, error: 'download_or_scan_failed', notes: r.errors.join(';') };
        }
        return {
            ...policy,
            downloadedTo: r.downloadedTo,
            warnings: r.warnings,
        };
    }
}
exports.ExternalCatalogRegistry = ExternalCatalogRegistry;
