import { Command, flags } from '@heroku-cli/command';
import * as color from '@heroku/heroku-cli-util/color';
import { Args, ux } from '@oclif/core';
import tsheredoc from 'tsheredoc';
import { appTransfer } from '../../lib/apps/app-transfer.js';
import { lazyModuleLoader } from '../../lib/lazy-module-loader.js';
import ConfirmCommand from '../../lib/confirmCommand.js';
import { getOwner, isTeamApp, isValidEmail } from '../../lib/teamUtils.js';
import AppsLock from './lock.js';
const heredoc = tsheredoc.default;
export default class AppsTransfer extends Command {
    static args = {
        recipient: Args.string({ description: 'user or team to transfer applications to', required: true }),
    };
    static description = 'transfer applications to another user or team';
    static examples = [heredoc(`
    ${color.command('heroku apps:transfer collaborator@example.com')}
    Transferring example to collaborator@example.com... done`), heredoc(`
    ${color.command('heroku apps:transfer acme-widgets')}
    Transferring example to acme-widgets... done`), heredoc(`
    ${color.command('heroku apps:transfer --bulk acme-widgets')}
    ...`)];
    static flags = {
        app: flags.app(),
        bulk: flags.boolean({ description: 'transfer applications in bulk', required: false }),
        confirm: flags.string({ char: 'c', hidden: true }),
        locked: flags.boolean({ char: 'l', description: 'lock the app upon transfer', required: false }),
        remote: flags.remote({ char: 'r' }),
    };
    static topic = 'apps';
    getAppsToTransfer(apps, inquirer) {
        return inquirer.prompt([{
                choices: apps.map(app => ({
                    name: `${color.app(app.name ?? '')} (${getOwner(app.owner?.email ?? '')})`, value: { name: app.name, owner: app.owner?.email },
                })),
                message: 'Select applications you would like to transfer',
                name: 'choices',
                pageSize: 20,
                type: 'checkbox',
            }]);
    }
    async run() {
        const inquirer = await lazyModuleLoader.loadInquirer();
        const { args, flags } = await this.parse(AppsTransfer);
        const { app, bulk, confirm, locked } = flags;
        const { recipient } = args;
        if (bulk) {
            const { body: allApps } = await this.heroku.get('/apps');
            const selectedApps = await this.getAppsToTransfer(allApps.sort((a, b) => (a.name ?? '').localeCompare(b.name ?? '')), inquirer);
            ux.warn(`Transferring applications to ${color.name(recipient)}...\n`);
            for (const app of selectedApps.choices) {
                try {
                    await appTransfer({
                        appName: app.name,
                        bulk: true,
                        heroku: this.heroku,
                        personalToPersonal: isValidEmail(recipient) && !isTeamApp(app.owner),
                        recipient,
                    });
                }
                catch (error) {
                    const { message } = error;
                    ux.error(message);
                }
            }
        }
        else {
            const { body: appInfo } = await this.heroku.get(`/apps/${app}`);
            const appName = appInfo.name ?? app ?? '';
            if (isValidEmail(recipient) && isTeamApp(appInfo.owner?.email)) {
                await new ConfirmCommand().confirm(appName, confirm, 'All collaborators will be removed from this app');
            }
            await appTransfer({
                appName,
                bulk,
                heroku: this.heroku,
                personalToPersonal: isValidEmail(recipient) && !isTeamApp(appInfo.owner?.email),
                recipient,
            });
            if (locked) {
                await AppsLock.run(['--app', appName], this.config);
            }
        }
    }
}
