/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { listExtensions } from '@vybestack/llxprt-code-core';
import { getErrorMessage } from '../../utils/errors.js';
import { MessageType } from '../types.js';
import { CommandKind, } from './types.js';
function showMessageIfNoExtensions(context, extensions) {
    if (extensions.length === 0) {
        context.ui.addItem({
            type: MessageType.INFO,
            text: 'No extensions installed. Run `/extensions explore` to check out the gallery.',
        }, Date.now());
        return true;
    }
    return false;
}
async function listAction(context) {
    const extensions = context.services.config
        ? listExtensions(context.services.config)
        : [];
    if (showMessageIfNoExtensions(context, extensions)) {
        return;
    }
    const historyItem = {
        type: MessageType.EXTENSIONS_LIST,
        extensions,
    };
    context.ui.addItem(historyItem, Date.now());
}
function updateAction(context, args) {
    const updateArgs = args.split(' ').filter((value) => value.length > 0);
    const all = updateArgs.length === 1 && updateArgs[0] === '--all';
    const names = all ? null : updateArgs;
    if (!all && names?.length === 0) {
        context.ui.addItem({
            type: MessageType.ERROR,
            text: 'Usage: /extensions update <extension-names>|--all',
        }, Date.now());
        return Promise.resolve();
    }
    let resolveUpdateComplete;
    const updateComplete = new Promise((resolve) => (resolveUpdateComplete = resolve));
    const extensions = context.services.config
        ? listExtensions(context.services.config)
        : [];
    if (showMessageIfNoExtensions(context, extensions)) {
        return Promise.resolve();
    }
    const historyItem = {
        type: MessageType.EXTENSIONS_LIST,
        extensions,
    };
    // eslint-disable-next-line @typescript-eslint/no-floating-promises
    updateComplete.then((updateInfos) => {
        if (updateInfos.length === 0) {
            context.ui.addItem({
                type: MessageType.INFO,
                text: 'No extensions to update.',
            }, Date.now());
        }
        context.ui.addItem(historyItem, Date.now());
        context.ui.setPendingItem(null);
    });
    try {
        context.ui.setPendingItem(historyItem);
        context.ui.dispatchExtensionStateUpdate({
            type: 'SCHEDULE_UPDATE',
            payload: {
                all,
                names,
                onComplete: (updateInfos) => {
                    resolveUpdateComplete(updateInfos);
                },
            },
        });
        if (names?.length) {
            const extensions = listExtensions(context.services.config);
            for (const name of names) {
                const extension = extensions.find((extension) => extension.name === name);
                if (!extension) {
                    context.ui.addItem({
                        type: MessageType.ERROR,
                        text: `Extension ${name} not found.`,
                    }, Date.now());
                    continue;
                }
            }
        }
    }
    catch (error) {
        resolveUpdateComplete([]);
        context.ui.addItem({
            type: MessageType.ERROR,
            text: getErrorMessage(error),
        }, Date.now());
    }
    return updateComplete.then((_) => { });
}
const listExtensionsCommand = {
    name: 'list',
    description: 'List active extensions',
    kind: CommandKind.BUILT_IN,
    autoExecute: true,
    action: listAction,
};
const updateExtensionsCommand = {
    name: 'update',
    description: 'Update extensions. Usage: update <extension-names>|--all',
    kind: CommandKind.BUILT_IN,
    action: updateAction,
    completion: async (context, partialArg) => {
        const extensions = context.services.config
            ? listExtensions(context.services.config)
            : [];
        const extensionNames = extensions.map((ext) => ext.name);
        const suggestions = extensionNames.filter((name) => name.startsWith(partialArg));
        if ('--all'.startsWith(partialArg) || 'all'.startsWith(partialArg)) {
            suggestions.unshift('--all');
        }
        return suggestions;
    },
};
async function restartAction(context, args) {
    const extensionLoader = context.services.config?.getExtensionLoader();
    if (!extensionLoader) {
        context.ui.addItem({
            type: MessageType.ERROR,
            text: "Extensions are not yet loaded, can't restart yet",
        }, Date.now());
        return;
    }
    const extensions = extensionLoader.getExtensions();
    if (showMessageIfNoExtensions(context, extensions)) {
        return;
    }
    const restartArgs = args.split(' ').filter((value) => value.length > 0);
    const all = restartArgs.length === 1 && restartArgs[0] === '--all';
    const names = all ? null : restartArgs;
    if (!all && names?.length === 0) {
        context.ui.addItem({
            type: MessageType.ERROR,
            text: 'Usage: /extensions restart <extension-names>|--all',
        }, Date.now());
        return Promise.resolve();
    }
    let extensionsToRestart = extensionLoader
        .getExtensions()
        .filter((extension) => extension.isActive);
    if (names) {
        extensionsToRestart = extensionsToRestart.filter((extension) => names.includes(extension.name));
        if (names.length !== extensionsToRestart.length) {
            const notFound = names.filter((name) => !extensionsToRestart.some((extension) => extension.name === name));
            if (notFound.length > 0) {
                context.ui.addItem({
                    type: MessageType.WARNING,
                    text: `Extension(s) not found or not active: ${notFound.join(', ')}`,
                }, Date.now());
            }
        }
    }
    if (extensionsToRestart.length === 0) {
        // We will have logged a different message above already.
        return;
    }
    const s = extensionsToRestart.length > 1 ? 's' : '';
    const restartingMessage = {
        type: MessageType.INFO,
        text: `Restarting ${extensionsToRestart.length} extension${s}...`,
    };
    context.ui.addItem(restartingMessage, Date.now());
    const results = await Promise.allSettled(extensionsToRestart.map(async (extension) => {
        if (extension.isActive) {
            await extensionLoader.restartExtension(extension);
            context.ui.dispatchExtensionStateUpdate({
                type: 'RESTARTED',
                payload: {
                    name: extension.name,
                },
            });
        }
    }));
    const failureMessages = results
        .map((result, index) => result.status === 'rejected'
        ? `${extensionsToRestart[index].name}: ${getErrorMessage(result.reason)}`
        : null)
        .filter((message) => message !== null);
    if (failureMessages.length > 0) {
        const errorMessages = failureMessages.join('\n  ');
        context.ui.addItem({
            type: MessageType.ERROR,
            text: `Failed to restart some extensions:\n  ${errorMessages}`,
        }, Date.now());
    }
    else {
        context.ui.addItem({
            type: MessageType.INFO,
            text: `${extensionsToRestart.length} extension${s} restarted successfully.`,
        }, Date.now());
    }
}
async function completeExtensions(context, partialArg) {
    let extensions = context.services.config
        ? listExtensions(context.services.config)
        : [];
    // Filter by active state based on the command
    if (context.invocation?.name === 'restart') {
        extensions = extensions.filter((ext) => ext.isActive);
    }
    const extensionNames = extensions.map((ext) => ext.name);
    return extensionNames.filter((name) => name.startsWith(partialArg));
}
const restartCommand = {
    name: 'restart',
    description: 'Restart extensions. Usage: restart <extension-names>|--all',
    kind: CommandKind.BUILT_IN,
    action: restartAction,
    completion: completeExtensions,
};
export const extensionsCommand = {
    name: 'extensions',
    description: 'Manage extensions',
    kind: CommandKind.BUILT_IN,
    subCommands: [listExtensionsCommand, updateExtensionsCommand, restartCommand],
    action: (context, args) => 
    // Default to list if no subcommand is provided
    listExtensionsCommand.action(context, args),
};
//# sourceMappingURL=extensionsCommand.js.map