import { Command } from '@heroku-cli/command';
import * as color from '@heroku/heroku-cli-util/color';
import { Args, ux } from '@oclif/core';
import AccountsModule from '../../lib/accounts/accounts.js';
export default class Add extends Command {
    static args = {
        name: Args.string({ description: 'name of Heroku account to add', required: true }),
    };
    static description = 'add a Heroku account to your cache';
    static example = `${color.command('heroku accounts:add my-account')}`;
    async run() {
        const { args } = await this.parse(Add);
        const { name } = args;
        const logInMessage = 'You must be logged in to run this command.';
        if (AccountsModule.list().some(a => a.name === name)) {
            ux.error(`${name} already exists`);
        }
        const { body: account } = await this.heroku.get('/account');
        const email = account.email || '';
        const token = this.heroku.auth || '';
        if (token === '') {
            ux.error(logInMessage);
        }
        if (email === '') {
            ux.error(logInMessage);
        }
        AccountsModule.add(name, email, token);
    }
}
