import fs from "node:fs";
import path from "node:path";
import { discoverPlugins } from "./registry.js";
import { parsePluginManifestJson } from "./manifest.js";
export function loadPlugin(projectPath, dataDir, name) {
    const plugin = discoverPlugins(projectPath, dataDir).find((candidate) => candidate.name === name);
    if (!plugin)
        return null;
    const manifestPath = pluginManifestPath(plugin.path);
    const manifest = readManifest(manifestPath);
    return {
        ...plugin,
        ...manifest,
    };
}
export function pluginManifestPath(pluginPath) {
    const codexPath = path.join(pluginPath, ".codex-plugin", "plugin.json");
    if (fs.existsSync(codexPath))
        return codexPath;
    return path.join(pluginPath, ".claude-plugin", "plugin.json");
}
function readManifest(manifestPath) {
    if (!fs.existsSync(manifestPath))
        return {};
    try {
        return {
            manifest: parsePluginManifestJson(fs.readFileSync(manifestPath, "utf8")),
        };
    }
    catch (error) {
        return {
            manifestError: error instanceof Error ? error.message : String(error),
        };
    }
}
