import fs from "node:fs";
import path from "node:path";
import { loadPlugin } from "./loader.js";
import { discoverPlugins } from "./registry.js";
export function enabledPluginManifests(projectPath, dataDir) {
    return discoverPlugins(projectPath, dataDir)
        .filter((plugin) => plugin.enabled)
        .map((plugin) => loadPlugin(projectPath, dataDir, plugin.name))
        .filter((plugin) => Boolean(plugin?.manifest));
}
export function pluginExtensionDirs(projectPath, dataDir, kind) {
    const dirs = [];
    for (const plugin of enabledPluginManifests(projectPath, dataDir)) {
        const entries = plugin.manifest?.[kind] ?? [];
        for (const entry of entries) {
            const resolved = resolvePluginPath(plugin.path, entry);
            if (!resolved || !fs.existsSync(resolved) || !fs.statSync(resolved).isDirectory())
                continue;
            dirs.push({ plugin, path: resolved });
        }
    }
    return dirs;
}
function resolvePluginPath(pluginRoot, relativePath) {
    const root = path.resolve(pluginRoot);
    const resolved = path.resolve(root, relativePath);
    return resolved === root || resolved.startsWith(`${root}${path.sep}`) ? resolved : null;
}
