/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { CommandKind, } from './types.js';
import { MessageType } from '../types.js';
/**
 * List all registered hooks
 */
async function listHooks(context) {
    const { config } = context.services;
    if (!config) {
        context.ui.addItem({
            type: MessageType.ERROR,
            text: 'Config not loaded.',
        }, Date.now());
        return;
    }
    const hookSystem = config.getHookSystem();
    if (!hookSystem) {
        context.ui.addItem({
            type: MessageType.INFO,
            text: 'Hooks system is not enabled. Enable it in settings with tools.enableHooks.',
        }, Date.now());
        return;
    }
    await hookSystem.initialize();
    const hookRegistry = hookSystem.getRegistry();
    const allHooks = hookRegistry.getAllHooks();
    const historyItem = {
        type: MessageType.HOOKS_LIST,
        hooks: allHooks,
    };
    context.ui.addItem(historyItem, Date.now());
}
/**
 * Enable a hook by name
 */
async function enableHook(context, hookName) {
    const { config } = context.services;
    if (!config) {
        context.ui.addItem({
            type: MessageType.ERROR,
            text: 'Config not loaded.',
        }, Date.now());
        return;
    }
    const hookSystem = config.getHookSystem();
    if (!hookSystem) {
        context.ui.addItem({
            type: MessageType.ERROR,
            text: 'Hooks system is not enabled.',
        }, Date.now());
        return;
    }
    await hookSystem.initialize();
    const hookRegistry = hookSystem.getRegistry();
    // Find the hook
    const allHooks = hookRegistry.getAllHooks();
    const matchingHook = allHooks.find((entry) => hookRegistry.getHookName(entry) === hookName);
    if (!matchingHook) {
        context.ui.addItem({
            type: MessageType.ERROR,
            text: `Hook '${hookName}' not found.`,
        }, Date.now());
        return;
    }
    // Remove from disabled list
    const disabledHooks = config.getDisabledHooks();
    const newDisabledHooks = disabledHooks.filter((name) => name !== hookName);
    config.setDisabledHooks(newDisabledHooks);
    // Update the registry
    hookRegistry.setHookEnabled(hookName, true);
    context.ui.addItem({
        type: MessageType.INFO,
        text: `Enabled hook '${hookName}'.`,
    }, Date.now());
}
/**
 * Disable a hook by name
 */
async function disableHook(context, hookName) {
    const { config } = context.services;
    if (!config) {
        context.ui.addItem({
            type: MessageType.ERROR,
            text: 'Config not loaded.',
        }, Date.now());
        return;
    }
    const hookSystem = config.getHookSystem();
    if (!hookSystem) {
        context.ui.addItem({
            type: MessageType.ERROR,
            text: 'Hooks system is not enabled.',
        }, Date.now());
        return;
    }
    await hookSystem.initialize();
    const hookRegistry = hookSystem.getRegistry();
    // Find the hook
    const allHooks = hookRegistry.getAllHooks();
    const matchingHook = allHooks.find((entry) => hookRegistry.getHookName(entry) === hookName);
    if (!matchingHook) {
        context.ui.addItem({
            type: MessageType.ERROR,
            text: `Hook '${hookName}' not found.`,
        }, Date.now());
        return;
    }
    // Add to disabled list
    const disabledHooks = config.getDisabledHooks();
    if (!disabledHooks.includes(hookName)) {
        const newDisabledHooks = [...disabledHooks, hookName];
        config.setDisabledHooks(newDisabledHooks);
    }
    // Update the registry
    hookRegistry.setHookEnabled(hookName, false);
    context.ui.addItem({
        type: MessageType.INFO,
        text: `Disabled hook '${hookName}'.`,
    }, Date.now());
}
async function completeHookNames(context, partialArg) {
    const { config } = context.services;
    if (!config) {
        return [];
    }
    const hookSystem = config.getHookSystem();
    if (!hookSystem) {
        return [];
    }
    await hookSystem.initialize();
    const hookRegistry = hookSystem.getRegistry();
    const hookNames = hookRegistry
        .getAllHooks()
        .map((entry) => hookRegistry.getHookName(entry));
    return hookNames.filter((name) => name.startsWith(partialArg));
}
const listCommand = {
    name: 'list',
    description: 'List all registered hooks',
    kind: CommandKind.BUILT_IN,
    autoExecute: true,
    action: async (context) => {
        await listHooks(context);
    },
};
const enableCommand = {
    name: 'enable',
    description: 'Enable a hook by name',
    kind: CommandKind.BUILT_IN,
    autoExecute: true,
    action: async (context, args) => {
        const hookName = args.trim();
        if (!hookName) {
            context.ui.addItem({
                type: MessageType.ERROR,
                text: 'Usage: /hooks enable <hook-name>',
            }, Date.now());
            return;
        }
        await enableHook(context, hookName);
    },
    completion: completeHookNames,
};
const disableCommand = {
    name: 'disable',
    description: 'Disable a hook by name',
    kind: CommandKind.BUILT_IN,
    autoExecute: true,
    action: async (context, args) => {
        const hookName = args.trim();
        if (!hookName) {
            context.ui.addItem({
                type: MessageType.ERROR,
                text: 'Usage: /hooks disable <hook-name>',
            }, Date.now());
            return;
        }
        await disableHook(context, hookName);
    },
    completion: completeHookNames,
};
export const hooksCommand = {
    name: 'hooks',
    description: 'View, enable, or disable hooks',
    kind: CommandKind.BUILT_IN,
    subCommands: [listCommand, enableCommand, disableCommand],
    action: async (context, args) => {
        // Default action when no subcommand is provided - show the list
        if (!args || args.trim() === '') {
            await listHooks(context);
        }
        else {
            // Try to parse as a subcommand
            const tokens = args.trim().split(/\s+/);
            const subCommandName = tokens[0];
            const subArgs = tokens.slice(1).join(' ');
            const subCommand = [listCommand, enableCommand, disableCommand].find((cmd) => cmd.name === subCommandName);
            if (subCommand && subCommand.action) {
                await subCommand.action(context, subArgs);
            }
            else {
                await listHooks(context);
            }
        }
    },
};
//# sourceMappingURL=hooksCommand.js.map