import { Command, flags } from '@heroku-cli/command';
import * as color from '@heroku/heroku-cli-util/color';
import { ux } from '@oclif/core/ux';
import { getTeamInvites, isTeamInviteFeatureEnabled } from '../../lib/members/team-invite-utils.js';
const revokeInvite = async (email, team, heroku) => {
    ux.action.start(`Revoking invite for ${color.user(email)} in ${color.team(team)}`);
    await heroku.delete(`/teams/${team}/invitations/${email}`, {
        headers: {
            Accept: 'application/vnd.heroku+json; version=3.team-invitations',
        },
    });
    ux.action.stop();
};
const removeUserMembership = async (email, team, heroku) => {
    ux.action.start(`Removing ${color.user(email)} from ${color.team(team)}`);
    await heroku.delete(`/teams/${team}/members/${encodeURIComponent(email)}`);
    ux.action.stop();
};
export default class MembersRemove extends Command {
    static description = 'removes a user from a team';
    static flags = {
        team: flags.team({ required: true }),
    };
    static strict = false;
    static topic = 'members';
    async run() {
        const { argv, flags } = await this.parse(MembersRemove);
        const { team } = flags;
        const email = argv[0];
        const teamInviteFeatureEnabled = await isTeamInviteFeatureEnabled(team, this.heroku);
        let isInvitedUser = false;
        if (teamInviteFeatureEnabled) {
            const invites = await getTeamInvites(team, this.heroku);
            isInvitedUser = Boolean(invites.some(m => m.user?.email === email));
        }
        if (teamInviteFeatureEnabled && isInvitedUser) {
            await revokeInvite(email, team, this.heroku);
        }
        else {
            await removeUserMembership(email, team, this.heroku);
        }
    }
}
