import { Command, flags } from '@heroku-cli/command';
import * as color from '@heroku/heroku-cli-util/color';
import { Args, ux } from '@oclif/core';
import { getOwner, isTeamApp } from '../../lib/teamUtils.js';
export default class AccessAdd extends Command {
    static args = {
        email: Args.string({ description: 'email address of the team member', required: true }),
    };
    static description = 'add new users to your app';
    static examples = [
        `${color.command('heroku access:add user@email.com --app APP')} # add a collaborator to your app`,
        `${color.command('heroku access:add user@email.com --app APP --permissions deploy,manage,operate')} # permissions must be comma separated`,
    ];
    static flags = {
        app: flags.app({ required: true }),
        permissions: flags.string({ char: 'p', description: 'list of permissions comma separated' }),
        remote: flags.remote({ char: 'r' }),
    };
    async run() {
        const { args, flags } = await this.parse(AccessAdd);
        const { email } = args;
        const { app: appName, permissions } = flags;
        const { body: appInfo } = await this.heroku.get(`/apps/${appName}`);
        let output = `Adding ${color.user(email)} access to the app ${color.app(appName)}`;
        let teamFeatures = [];
        if (isTeamApp(appInfo?.owner?.email)) {
            const teamName = getOwner(appInfo?.owner?.email);
            const teamFeaturesRequest = await this.heroku.get(`/teams/${teamName}/features`);
            teamFeatures = teamFeaturesRequest.body;
        }
        if (teamFeatures.some(feature => feature.name === 'org-access-controls')) {
            if (!permissions)
                this.error('Missing argument: permissions', { exit: 1 });
            const permissionsArray = permissions ? permissions.split(',') : [];
            permissionsArray.push('view');
            const permissionsArraySorted = [...new Set(permissionsArray.sort())];
            output += ` with ${color.green(permissionsArraySorted.join(', '))} permissions`;
            ux.action.start(output);
            await this.heroku.post(`/teams/apps/${appName}/collaborators`, {
                body: { permissions: permissionsArraySorted, user: email },
            });
            ux.action.stop();
        }
        else {
            ux.action.start(output);
            await this.heroku.post(`/apps/${appName}/collaborators`, { body: { user: email } });
            ux.action.stop();
        }
    }
}
