import fs from "node:fs";
import path from "node:path";
export function discoverPlugins(projectPath, dataDir) {
    const userHome = process.env.USERPROFILE ?? process.env.HOME ?? "";
    const roots = [
        { scope: "project", dir: path.join(projectPath, ".deepseekcode", "plugins") },
        { scope: "project", dir: path.join(projectPath, ".claude", "plugins") },
        { scope: "user", dir: userHome ? path.join(userHome, ".deepseekcode", "plugins") : "" },
        { scope: "user", dir: userHome ? path.join(userHome, ".claude", "plugins") : "" },
        { scope: "cache", dir: path.join(dataDir, "cache", "plugins") },
    ];
    const plugins = [];
    const seenNames = new Set();
    for (const root of roots) {
        if (!root.dir || !fs.existsSync(root.dir))
            continue;
        for (const entry of fs.readdirSync(root.dir, { withFileTypes: true })) {
            if (!entry.isDirectory())
                continue;
            if (seenNames.has(entry.name))
                continue;
            const pluginDir = path.join(root.dir, entry.name);
            if (!hasPluginManifest(pluginDir))
                continue;
            seenNames.add(entry.name);
            plugins.push({
                name: entry.name,
                path: pluginDir,
                scope: root.scope,
                enabled: !fs.existsSync(path.join(pluginDir, ".disabled")),
            });
        }
    }
    return plugins.sort((a, b) => scopeOrder(a.scope) - scopeOrder(b.scope) || a.name.localeCompare(b.name));
}
function hasPluginManifest(pluginPath) {
    return fs.existsSync(path.join(pluginPath, ".codex-plugin", "plugin.json"))
        || fs.existsSync(path.join(pluginPath, ".claude-plugin", "plugin.json"));
}
function scopeOrder(scope) {
    if (scope === "project")
        return 0;
    if (scope === "user")
        return 1;
    return 2;
}
