import inquirer from 'inquirer';
import { PMOCommand, pmoBaseFlags } from '../../lib/pmo/index.js';
import { styles } from '../../lib/styles.js';
import { shouldOutputJson, outputPromptAsJson, createMetadata, buildPromptConfig, } from '../../lib/prompt-json.js';
export default class TicketBulk extends PMOCommand {
    static description = 'Manage tickets in bulk (interactive menu)';
    static examples = [
        '<%= config.bin %> <%= command.id %>',
    ];
    static flags = {
        ...pmoBaseFlags,
    };
    getPMOOptions() {
        return { promptIfMultiple: false };
    }
    async execute() {
        const { flags } = await this.parse(TicketBulk);
        // This command requires project context - store the projectId
        const projectId = await this.requireProject();
        // Check if JSON output mode is active
        const jsonMode = shouldOutputJson(flags);
        // Define choices with emojis for interactive mode, plain names for JSON
        const menuChoices = [
            { name: 'List all tickets', value: 'list', emoji: '📋', command: `prlt ticket list -P ${projectId} --format json` },
            { name: 'Move multiple tickets', value: 'move', emoji: '📦', command: `prlt ticket move -P ${projectId} --bulk --json` },
            { name: 'Complete multiple tickets', value: 'complete', emoji: '✅', command: `prlt ticket complete -P ${projectId} --bulk --json` },
            { name: 'Reassign tickets (change assignee)', value: 'reassign', emoji: '👤', command: `prlt ticket reassign -P ${projectId} --bulk --json` },
            { name: 'Link tickets to epic', value: 'epic', emoji: '🔗', command: `prlt ticket epic -P ${projectId} --bulk --json` },
            { name: 'Link tickets to spec', value: 'spec', emoji: '📄', command: `prlt ticket spec -P ${projectId} --bulk --json` },
            { name: 'Move tickets to project', value: 'project', emoji: '📁', command: `prlt ticket project -P ${projectId} --bulk --json` },
            { name: 'Update tickets (priority/category)', value: 'update', emoji: '✏️ ', command: `prlt ticket update -P ${projectId} --bulk --json` },
            { name: 'Delete multiple tickets', value: 'delete', emoji: '🗑️ ', command: `prlt ticket delete -P ${projectId} --bulk --json` },
            { name: 'Cancel', value: 'cancel', emoji: '', command: '' },
        ];
        const message = 'Ticket Management (Bulk Operations) - What would you like to do?';
        // In JSON mode, output action selection prompt (without emojis)
        if (jsonMode) {
            outputPromptAsJson(buildPromptConfig('list', 'action', message, menuChoices.map(c => ({ name: c.name, value: c.value, command: c.command }))), createMetadata('ticket bulk', flags));
            return;
        }
        this.log(styles.emphasis('🎫 ' + message.split(' - ')[0]));
        this.log('');
        // Helper to get choice with emoji by value
        const withEmoji = (value) => {
            const choice = menuChoices.find(c => c.value === value);
            return { name: choice.emoji ? `${choice.emoji} ${choice.name}` : choice.name, value: choice.value };
        };
        const { action } = await this.prompt([{
                type: 'list',
                name: 'action',
                message: 'What would you like to do?',
                choices: [
                    withEmoji('list'),
                    withEmoji('move'),
                    withEmoji('complete'),
                    new inquirer.Separator(),
                    withEmoji('reassign'),
                    withEmoji('epic'),
                    withEmoji('spec'),
                    withEmoji('project'),
                    withEmoji('update'),
                    new inquirer.Separator(),
                    withEmoji('delete'),
                    new inquirer.Separator(),
                    withEmoji('cancel'),
                ]
            }], null);
        if (action === 'cancel') {
            this.log(styles.muted('Operation cancelled.'));
            return;
        }
        // Build args for the sub-command
        const projectArgs = ['--project', projectId, '--bulk'];
        try {
            this.log(styles.muted(`\nExecuting: ticket ${action} --bulk`));
            switch (action) {
                case 'list': {
                    // List doesn't need bulk mode
                    const { default: ListCommand } = await import('./list.js');
                    const cmd = new ListCommand(['--project', projectId], this.config);
                    await cmd.run();
                    break;
                }
                case 'move': {
                    const { default: MoveCommand } = await import('./move.js');
                    const cmd = new MoveCommand(projectArgs, this.config);
                    await cmd.run();
                    break;
                }
                case 'delete': {
                    const { default: DeleteCommand } = await import('./delete.js');
                    const cmd = new DeleteCommand(projectArgs, this.config);
                    await cmd.run();
                    break;
                }
                case 'complete': {
                    const { default: CompleteCommand } = await import('./complete.js');
                    const cmd = new CompleteCommand(projectArgs, this.config);
                    await cmd.run();
                    break;
                }
                case 'reassign': {
                    const { default: ReassignCommand } = await import('./reassign.js');
                    const cmd = new ReassignCommand(projectArgs, this.config);
                    await cmd.run();
                    break;
                }
                case 'epic': {
                    const { default: EpicCommand } = await import('./epic.js');
                    const cmd = new EpicCommand(projectArgs, this.config);
                    await cmd.run();
                    break;
                }
                case 'spec': {
                    const { default: SpecCommand } = await import('./spec.js');
                    const cmd = new SpecCommand(projectArgs, this.config);
                    await cmd.run();
                    break;
                }
                case 'project': {
                    const { default: ProjectCommand } = await import('./project.js');
                    const cmd = new ProjectCommand(projectArgs, this.config);
                    await cmd.run();
                    break;
                }
                case 'update': {
                    const { default: UpdateCommand } = await import('./update.js');
                    const cmd = new UpdateCommand(projectArgs, this.config);
                    await cmd.run();
                    break;
                }
                default:
                    this.error(`Unknown action: ${action}`);
            }
        }
        catch (error) {
            this.error(`Failed to execute ticket ${action}: ${error instanceof Error ? error.message : String(error)}`);
        }
    }
}
//# sourceMappingURL=bulk.js.map