/**
 * Plugin Command
 * Manage framework plugins (install, uninstall, list)
 */
import { BaseCommand } from '../BaseCommand.js';
import { Logger } from '../../config/logger.js';
import { PluginManager } from '../../runtime/PluginManager.js';
export const PluginCommand = Object.freeze({
    create() {
        return BaseCommand.create({
            name: 'plugin',
            description: 'Manage framework plugins',
            addOptions: (command) => {
                command.alias('p');
                // Options for short syntax (zin p -i a:sqlite)
                command.option('-i, --install <pluginId>', 'Install a plugin');
                command.option('-u, --uninstall <pluginId>', 'Uninstall a plugin');
                command.option('-l, --list', 'List available plugins');
                command
                    .command('list')
                    .alias('ls')
                    .description('List available plugins')
                    .action(async () => {
                    await listPlugins();
                });
                command
                    .command('install <pluginId>')
                    .alias('i')
                    .description('Install a plugin')
                    .option('--package-manager <pm>', 'Specify package manager to use (npm|yarn|pnpm)')
                    .action(async (pluginId, options) => {
                    await installPlugin(pluginId, options);
                });
                command
                    .command('uninstall <pluginId>')
                    .alias('un')
                    .description('Uninstall a plugin')
                    .action(async (pluginId) => {
                    await uninstallPlugin(pluginId);
                });
            },
            execute: async (options) => {
                const installId = options['install'];
                const uninstallId = options['uninstall'];
                const shouldList = options['list'];
                if (typeof installId === 'string' && installId.length > 0) {
                    await installPlugin(installId);
                }
                else if (typeof uninstallId === 'string' && uninstallId.length > 0) {
                    await uninstallPlugin(uninstallId);
                }
                else if (shouldList === true) {
                    await listPlugins();
                }
                else {
                    Logger.info('Use "plugin list", "plugin install <id>", or "plugin uninstall <id>"');
                    Logger.info('Shortcuts: "zin p -l", "zin p -i <id>", "zin p -u <id>"');
                }
            },
        });
    },
});
async function listPlugins() {
    const plugins = PluginManager.list();
    Logger.info('Available Plugins:');
    const pluginEntries = Object.entries(plugins);
    const installedStatuses = await Promise.all(pluginEntries.map(async ([id]) => PluginManager.isInstalled(id)));
    for (let i = 0; i < pluginEntries.length; i++) {
        const [id, plugin] = pluginEntries[i];
        const isInstalled = installedStatuses[i] ?? false;
        const status = isInstalled ? '✓ Installed' : '- Available';
        const aliases = plugin.aliases !== undefined && plugin.aliases.length > 0
            ? `(aliases: ${plugin.aliases.join(', ')})`
            : '';
        Logger.info(`  ${id.padEnd(20)} ${status.padEnd(15)} ${plugin.description} ${aliases}`);
    }
}
async function installPlugin(pluginId, options) {
    try {
        const pm = typeof options?.['packageManager'] === 'string'
            ? String(options?.['packageManager'])
            : undefined;
        await PluginManager.install(pluginId, { packageManager: pm });
    }
    catch (error) {
        Logger.error(`Failed to install plugin ${pluginId}`, error);
        process.exit(1);
    }
}
async function uninstallPlugin(pluginId) {
    try {
        await PluginManager.uninstall(pluginId);
    }
    catch (error) {
        Logger.error(`Failed to uninstall plugin ${pluginId}`, error);
        process.exit(1);
    }
}
