// Pure premium-plugin MCP directory discovery.
//
// Two shapes:
//
//   Standalone:   <installDir>/premium-plugins/<bundle>/mcp/package.json
//   Bundle sub:   <installDir>/premium-plugins/<bundle>/plugins/<plugin>/mcp/package.json
//
// Recursive walks would match nested dirs that aren't real MCP plugins;
// the explicit walk eliminates that class of false positive. Each level
// is gated on isDirectory() before descending, so a stray file at any
// level is ignored.
import { existsSync, readdirSync } from "node:fs";
import { join } from "node:path";
export function findPremiumMcpDirs(installDir) {
    const root = join(installDir, "premium-plugins");
    if (!existsSync(root))
        return [];
    const out = [];
    for (const bundle of readdirSync(root, { withFileTypes: true })) {
        if (!bundle.isDirectory())
            continue;
        // Standalone shape: premium-plugins/<bundle>/mcp/
        const standaloneMcp = join(root, bundle.name, "mcp");
        if (existsSync(join(standaloneMcp, "package.json"))) {
            out.push(standaloneMcp);
        }
        // Bundle-sub shape: premium-plugins/<bundle>/plugins/<plugin>/mcp/
        const pluginsRoot = join(root, bundle.name, "plugins");
        if (!existsSync(pluginsRoot))
            continue;
        for (const plugin of readdirSync(pluginsRoot, { withFileTypes: true })) {
            if (!plugin.isDirectory())
                continue;
            const mcpDir = join(pluginsRoot, plugin.name, "mcp");
            if (!existsSync(join(mcpDir, "package.json")))
                continue;
            out.push(mcpDir);
        }
    }
    return out.sort();
}
