// tools/tool-group-handler.ts — Handlers for tool group management (SPEC-074)
import { t, ti } from '../i18n/index.js';
import { analyzeContext } from '../engine/tool-groups/context-analyzer.js';
// Singleton reference set from register-tool-groups.ts
let groupManager = null;
export function setGroupManager(manager) {
    groupManager = manager;
}
// eslint-disable-next-line @typescript-eslint/require-await -- sync logic, async signature required by safe() wrapper
export async function handleActivateGroup(args) {
    if (!groupManager) {
        return {
            content: [{ type: 'text', text: t('tools.tool_groups.notInitialized') }],
            isError: true,
        };
    }
    try {
        let result;
        switch (args.action) {
            case 'enable':
                result = groupManager.enableGroup(args.group);
                break;
            case 'disable':
                result = groupManager.disableGroup(args.group);
                break;
            case 'toggle':
                result = groupManager.toggleGroup(args.group);
                break;
        }
        // Check for context-based suggestions
        const enabledGroups = new Set(result.allGroups.filter((g) => g.enabled).map((g) => g.id));
        const suggestions = analyzeContext(args.group, enabledGroups);
        const lines = [
            ti('tools.tool_groups.groupToggled', {
                group: args.group,
                state: result.newState ? 'enabled' : 'disabled',
            }),
            '',
            ti('tools.tool_groups.activeTools', {
                active: String(result.totalActiveTools),
                total: String(result.totalTools),
            }),
            '',
            t('tools.tool_groups.groupStatusHeading'),
            ...result.allGroups.map((g) => `- ${g.enabled ? '✅' : '⬜'} **${g.name}** (${String(g.toolCount)} tools)`),
        ];
        if (suggestions.length > 0) {
            lines.push('', t('tools.tool_groups.suggestionsHeading'));
            for (const s of suggestions) {
                lines.push(`- ${ti('tools.tool_groups.suggestActivating', { group: s.suggestedGroup, reason: s.reason })}`);
            }
        }
        return { content: [{ type: 'text', text: lines.join('\n') }] };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        return { content: [{ type: 'text', text: `Error: ${message}` }], isError: true };
    }
}
// eslint-disable-next-line @typescript-eslint/require-await -- sync logic, async signature required by safe() wrapper
export async function handleListGroups() {
    if (!groupManager) {
        return {
            content: [{ type: 'text', text: t('tools.tool_groups.notInitialized') }],
            isError: true,
        };
    }
    const groups = groupManager.getGroupStatus();
    const activeCount = groupManager.getActiveToolCount();
    const totalCount = groupManager.getTotalToolCount();
    const lines = [
        ti('tools.tool_groups.listHeading', { active: String(activeCount), total: String(totalCount) }),
        '',
    ];
    for (const g of groups) {
        const icon = g.enabled ? '✅' : '⬜';
        lines.push(`## ${icon} ${g.name} (${String(g.toolCount)} tools)`);
        lines.push(g.description);
        lines.push('');
        lines.push(ti('tools.tool_groups.toolsLabel', { tools: g.tools.join(', ') }));
        lines.push('');
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
//# sourceMappingURL=tool-group-handler.js.map