"use strict";
/**
 * Shared Routes — Plugin registry content renderer
 *
 * Renders the markdown content for an installed plugin entry,
 * pulling from installed_plugins.json, known_marketplaces.json,
 * and blocklist.json within the allowed roots.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPluginRegistryContent = void 0;
const path = __importStar(require("path"));
const shared_routes_path_guards_1 = require("./shared-routes-path-guards");
const shared_routes_plugins_1 = require("./shared-routes-plugins");
function findPluginBlocklistEntry(sharedDir, allowedRoots, pluginName) {
    const blocklist = (0, shared_routes_plugins_1.readJsonObject)(path.join(sharedDir, 'blocklist.json'), allowedRoots);
    const plugins = blocklist?.plugins;
    if (!Array.isArray(plugins)) {
        return null;
    }
    return (plugins.find((entry) => entry &&
        typeof entry === 'object' &&
        'plugin' in entry &&
        entry.plugin === pluginName) ?? null);
}
function getPluginRegistryContent(itemPath, sharedDir, allowedRoots) {
    const pluginName = (0, shared_routes_plugins_1.decodePluginRegistryPath)(itemPath);
    if (!pluginName) {
        return null;
    }
    const registryPath = path.join(sharedDir, 'installed_plugins.json');
    const resolvedRegistryPath = (0, shared_routes_path_guards_1.safeRealPath)(registryPath);
    if (!resolvedRegistryPath || !(0, shared_routes_path_guards_1.isPathWithinAny)(resolvedRegistryPath, allowedRoots)) {
        return null;
    }
    const registry = (0, shared_routes_plugins_1.readPluginInstalledRegistry)(sharedDir, allowedRoots);
    if (!registry || !Object.prototype.hasOwnProperty.call(registry.plugins, pluginName)) {
        return null;
    }
    const metadata = registry.plugins[pluginName];
    const marketplace = (0, shared_routes_plugins_1.extractMarketplaceFromPluginName)(pluginName);
    const marketplaceEntry = marketplace
        ? (0, shared_routes_plugins_1.readJsonObject)(path.join(sharedDir, 'known_marketplaces.json'), allowedRoots)?.[marketplace]
        : null;
    const blocklistEntry = findPluginBlocklistEntry(sharedDir, allowedRoots, pluginName);
    const lines = [
        `# ${pluginName}`,
        '',
        `Registry: \`${resolvedRegistryPath}\``,
        '',
        '## Installed Plugin',
        '',
        `- Source: shared \`installed_plugins.json\` registry`,
        `- Registry version: ${registry.version ?? 'unknown'}`,
        `- Marketplace: ${marketplace ?? 'not recorded in plugin name'}`,
        `- Install records: ${Array.isArray(metadata) ? metadata.length : 1}`,
    ];
    if (marketplaceEntry) {
        lines.push('', '## Marketplace Registry Entry', '', '```json', (0, shared_routes_plugins_1.stringifyJson)(marketplaceEntry), '```');
    }
    if (blocklistEntry) {
        lines.push('', '## Blocklist Notice', '', '```json', (0, shared_routes_plugins_1.stringifyJson)(blocklistEntry), '```');
    }
    lines.push('', '## Plugin Registry Entry', '', '```json', (0, shared_routes_plugins_1.stringifyJson)(metadata), '```');
    return {
        content: lines.join('\n'),
        contentPath: resolvedRegistryPath,
    };
}
exports.getPluginRegistryContent = getPluginRegistryContent;
//# sourceMappingURL=shared-routes-plugin-registry-content.js.map