// tools/manage-plugins-handler.ts — Handler for manage_plugins tool (SPEC-085 AC-09)
import { discoverPlugins, installPlugin, activatePlugin, deactivatePlugin, uninstallPlugin, readPluginState, loadPluginModule, generatePluginDocs, getPluginToolCount, getPluginWorkerCount, getPluginAnalyzerCount, getPluginTemplateCount, } from '../engine/plugins/index.js';
function text(content) {
    return { content: [{ type: 'text', text: content }] };
}
function resolveProjectPath(input) {
    return input.projectPath ?? process.cwd();
}
const DATA_DIR = 'data';
async function handleList(input) {
    const projectPath = resolveProjectPath(input);
    const plugins = await discoverPlugins(projectPath);
    const state = await readPluginState(DATA_DIR);
    const items = plugins.map((p) => {
        const persisted = state.plugins.find((s) => s.manifest.id === p.manifest.id);
        const currentState = persisted?.state ?? p.state;
        return {
            id: p.manifest.id,
            name: p.manifest.name,
            version: p.manifest.version,
            state: currentState,
            toolCount: getPluginToolCount(p.manifest.id),
            workerCount: getPluginWorkerCount(p.manifest.id),
            analyzerCount: getPluginAnalyzerCount(p.manifest.id),
            templateCount: getPluginTemplateCount(p.manifest.id),
        };
    });
    if (items.length === 0) {
        return text('No plugins found. Install plugins into planu-plugins/ or use manage_plugins with action "install".');
    }
    return text(JSON.stringify({ plugins: items }, null, 2));
}
async function handleInstall(input) {
    if (!input.pluginPath) {
        return {
            content: [{ type: 'text', text: 'Error: pluginPath is required for install action' }],
            isError: true,
        };
    }
    const projectPath = resolveProjectPath(input);
    const reg = await installPlugin(input.pluginPath, projectPath, DATA_DIR);
    return text(`Plugin "${reg.manifest.name}" (${reg.manifest.id}) v${reg.manifest.version} installed successfully.`);
}
async function handleActivate(input) {
    if (!input.pluginId) {
        return {
            content: [{ type: 'text', text: 'Error: pluginId is required for activate action' }],
            isError: true,
        };
    }
    const projectPath = resolveProjectPath(input);
    const builtInTools = new Set(); // Would be populated from server
    const reg = await activatePlugin(input.pluginId, projectPath, DATA_DIR, builtInTools);
    if (reg.state === 'error') {
        return {
            content: [{ type: 'text', text: `Error activating plugin: ${reg.error ?? 'unknown error'}` }],
            isError: true,
        };
    }
    return text(`Plugin "${reg.manifest.name}" activated successfully.`);
}
async function handleDeactivate(input) {
    if (!input.pluginId) {
        return {
            content: [{ type: 'text', text: 'Error: pluginId is required for deactivate action' }],
            isError: true,
        };
    }
    const reg = await deactivatePlugin(input.pluginId, DATA_DIR);
    return text(`Plugin "${reg.manifest.name}" deactivated.`);
}
async function handleUninstall(input) {
    if (!input.pluginId) {
        return {
            content: [{ type: 'text', text: 'Error: pluginId is required for uninstall action' }],
            isError: true,
        };
    }
    const projectPath = resolveProjectPath(input);
    await uninstallPlugin(input.pluginId, projectPath, DATA_DIR);
    return text(`Plugin "${input.pluginId}" uninstalled and removed.`);
}
async function handleInfo(input) {
    if (!input.pluginId) {
        return {
            content: [{ type: 'text', text: 'Error: pluginId is required for info action' }],
            isError: true,
        };
    }
    const state = await readPluginState(DATA_DIR);
    const reg = state.plugins.find((p) => p.manifest.id === input.pluginId);
    if (!reg) {
        return {
            content: [{ type: 'text', text: `Plugin "${input.pluginId}" not found. Is it installed?` }],
            isError: true,
        };
    }
    let mod;
    try {
        const projectPath = resolveProjectPath(input);
        mod = await loadPluginModule(projectPath, reg.manifest);
    }
    catch {
        // Module may not be loadable — generate docs from manifest only
    }
    const docs = generatePluginDocs(reg.manifest, mod);
    return text(docs);
}
/**
 * Routes manage_plugins actions to the appropriate handler.
 */
export async function handleManagePlugins(input) {
    switch (input.action) {
        case 'list':
            return handleList(input);
        case 'install':
            return handleInstall(input);
        case 'activate':
            return handleActivate(input);
        case 'deactivate':
            return handleDeactivate(input);
        case 'uninstall':
            return handleUninstall(input);
        case 'info':
            return handleInfo(input);
    }
}
//# sourceMappingURL=manage-plugins-handler.js.map