import inquirer from 'inquirer';
import chalk from 'chalk';
import { PromptCommand } from '../../../lib/prompt-command.js';
import { machineOutputFlags } from '../../../lib/pmo/index.js';
import { getWorkspaceInfo } from '../../../lib/agents/commands.js';
import { ensureBuiltinThemes } from '../../../lib/themes.js';
import { getThemes, getAvailableThemeNames } from '../../../lib/database/index.js';
import { shouldOutputJson, outputPromptAsJson, createMetadata, buildPromptConfig, } from '../../../lib/prompt-json.js';
export default class AgentThemes extends PromptCommand {
    static description = 'Manage agent naming themes';
    static examples = [
        '<%= config.bin %> <%= command.id %> list',
        '<%= config.bin %> <%= command.id %> create greek-gods',
        '<%= config.bin %> <%= command.id %> add-names greek-gods zeus athena',
    ];
    static flags = {
        ...machineOutputFlags,
    };
    async run() {
        const { flags } = await this.parse(AgentThemes);
        const jsonMode = shouldOutputJson(flags);
        const menuChoices = [
            { id: 'list', name: 'List themes', command: 'prlt agent themes list --format json' },
            { id: 'create', name: 'Create a new theme', command: 'prlt agent themes create --machine' },
            { id: 'add-names', name: 'Add names to a theme', command: 'prlt agent themes add-names --machine' },
            { id: 'cancel', name: 'Cancel', command: '' },
        ];
        const message = 'What would you like to do?';
        if (jsonMode) {
            outputPromptAsJson(buildPromptConfig('list', 'action', message, menuChoices.map(c => ({
                name: c.name,
                value: c.id,
                command: c.command,
            }))), createMetadata('agent themes', flags));
            return;
        }
        this.log(chalk.bold('\nAgent Themes'));
        this.log(chalk.dim('Optional themed name pools for your agents.\n'));
        const { action } = await this.prompt([{
                type: 'list',
                name: 'action',
                message,
                choices: [
                    ...menuChoices.slice(0, 3).map(c => ({ name: c.name, value: c.id })),
                    new inquirer.Separator(),
                    { name: menuChoices[3].name, value: menuChoices[3].id }
                ]
            }], null);
        if (action === 'cancel') {
            this.log(chalk.dim('Cancelled.'));
            return;
        }
        try {
            switch (action) {
                case 'list': {
                    const { default: ListCommand } = await import('./list.js');
                    const cmd = new ListCommand([], this.config);
                    await cmd.run();
                    break;
                }
                case 'create': {
                    const { themeName } = await this.prompt([{
                            type: 'input',
                            name: 'themeName',
                            message: 'Theme name:',
                            validate: (input) => {
                                if (!input.trim())
                                    return 'Theme name is required';
                                return true;
                            }
                        }], null);
                    const normalized = themeName.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '');
                    if (themeName.trim() !== normalized) {
                        this.log(chalk.blue(`Normalized: ${themeName.trim()} → ${normalized}`));
                    }
                    const { default: CreateCommand } = await import('./create.js');
                    const cmd = new CreateCommand([normalized], this.config);
                    await cmd.run();
                    const { addNamesNow } = await this.prompt([{
                            type: 'list',
                            name: 'addNamesNow',
                            message: 'Add names to this theme now?',
                            choices: [
                                { name: 'Yes', value: true },
                                { name: 'No', value: false },
                            ],
                        }], null);
                    if (addNamesNow) {
                        const { names } = await this.prompt([{
                                type: 'input',
                                name: 'names',
                                message: 'Enter names (space-separated):',
                                validate: (input) => input.trim() ? true : 'At least one name is required'
                            }], null);
                        const addArgs = [normalized, ...names.trim().split(/\s+/)];
                        const { default: AddNamesCommand } = await import('./add-names.js');
                        const addCmd = new AddNamesCommand(addArgs, this.config);
                        await addCmd.run();
                    }
                    break;
                }
                case 'add-names': {
                    const workspaceInfo = getWorkspaceInfo();
                    ensureBuiltinThemes(workspaceInfo.path);
                    const themes = getThemes(workspaceInfo.path);
                    if (themes.length === 0) {
                        this.log(chalk.yellow('No themes found. Create one first.'));
                        return;
                    }
                    const themeChoices = themes.map(t => {
                        const availableNames = getAvailableThemeNames(workspaceInfo.path, t.id);
                        const builtinTag = t.builtin ? chalk.dim(' [built-in]') : '';
                        return {
                            name: `${t.display_name}${builtinTag} ${chalk.dim(`(${availableNames.length} names available)`)}`,
                            value: t.id
                        };
                    });
                    const { selectedTheme } = await this.prompt([{
                            type: 'list',
                            name: 'selectedTheme',
                            message: 'Select theme to add names to:',
                            choices: themeChoices
                        }], null);
                    const { names } = await this.prompt([{
                            type: 'input',
                            name: 'names',
                            message: 'Names to add (space-separated):',
                            validate: (input) => input.trim() ? true : 'At least one name is required'
                        }], null);
                    const addArgs = [selectedTheme, ...names.trim().split(/\s+/)];
                    const { default: AddNamesCommand } = await import('./add-names.js');
                    const cmd = new AddNamesCommand(addArgs, this.config);
                    await cmd.run();
                    break;
                }
                default:
                    this.error(`Unknown action: ${action}`);
            }
        }
        catch (error) {
            this.error(error instanceof Error ? error.message : String(error));
        }
    }
}
//# sourceMappingURL=index.js.map