// tools/manage-plugins-handler.ts — Handler for manage_plugins tool (SPEC-085 AC-09)
import { t, ti } from '../i18n/index.js';
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) {
    /* v8 ignore next -- process.cwd() fallback never reached when projectPath is always provided */
    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(t('tools.manage_plugins.noPlugins'));
    }
    return text(JSON.stringify({ plugins: items }, null, 2));
}
async function handleInstall(input) {
    if (!input.pluginPath) {
        return {
            content: [{ type: 'text', text: t('tools.manage_plugins.pluginPathRequired') }],
            isError: true,
        };
    }
    const projectPath = resolveProjectPath(input);
    const reg = await installPlugin(input.pluginPath, projectPath, DATA_DIR);
    return text(ti('tools.manage_plugins.installSuccess', {
        name: reg.manifest.name,
        id: reg.manifest.id,
        version: reg.manifest.version,
    }));
}
async function handleActivate(input) {
    if (!input.pluginId) {
        return {
            content: [
                { type: 'text', text: ti('tools.manage_plugins.pluginIdRequired', { action: 'activate' }) },
            ],
            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: ti('tools.manage_plugins.activateError', { error: reg.error ?? 'unknown error' }),
                },
            ],
            isError: true,
        };
    }
    return text(ti('tools.manage_plugins.activateSuccess', { name: reg.manifest.name }));
}
async function handleDeactivate(input) {
    if (!input.pluginId) {
        return {
            content: [
                {
                    type: 'text',
                    text: ti('tools.manage_plugins.pluginIdRequired', { action: 'deactivate' }),
                },
            ],
            isError: true,
        };
    }
    const reg = await deactivatePlugin(input.pluginId, DATA_DIR);
    return text(ti('tools.manage_plugins.deactivateSuccess', { name: reg.manifest.name }));
}
async function handleUninstall(input) {
    if (!input.pluginId) {
        return {
            content: [
                {
                    type: 'text',
                    text: ti('tools.manage_plugins.pluginIdRequired', { action: 'uninstall' }),
                },
            ],
            isError: true,
        };
    }
    const projectPath = resolveProjectPath(input);
    await uninstallPlugin(input.pluginId, projectPath, DATA_DIR);
    return text(ti('tools.manage_plugins.uninstallSuccess', { id: input.pluginId }));
}
async function handleInfo(input) {
    if (!input.pluginId) {
        return {
            content: [
                { type: 'text', text: ti('tools.manage_plugins.pluginIdRequired', { action: 'info' }) },
            ],
            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: ti('tools.manage_plugins.pluginNotFound', { id: input.pluginId }) },
            ],
            isError: true,
        };
    }
    let mod;
    try {
        const projectPath = resolveProjectPath(input);
        mod = await loadPluginModule(projectPath, reg.manifest);
    }
    catch {
        /* v8 ignore next -- defensive: module may not be loadable at runtime */
    }
    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