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 { getGeneration } from '../../lib/apps/generation.js';
import ConfirmCommand from '../../lib/confirmCommand.js';
import { displayNat } from '../../lib/spaces/spaces.js';
const heredoc = tsheredoc.default;
export default class Destroy extends Command {
    static args = {
        space: Args.string({ hidden: true }),
    };
    static description = heredoc `
    destroy a space
  `;
    static examples = [heredoc `
    ${color.command('heroku spaces:destroy --space my-space')}
    Destroying my-space... done
  `];
    static flags = {
        confirm: flags.string({ description: 'set to space name to bypass confirm prompt', hasValue: true }),
        space: flags.string({ char: 's', description: 'space to destroy' }),
    };
    static topic = 'spaces';
    async run() {
        const { args, flags } = await this.parse(Destroy);
        const { confirm } = flags;
        const spaceName = flags.space || args.space;
        if (!spaceName) {
            ux.error(heredoc `
        Space name required.
        USAGE: heroku spaces:destroy my-space
      `);
        }
        let natWarning = '';
        const { body: space } = await this.heroku.get(`/spaces/${spaceName}`);
        if (space.state === 'allocated') {
            ({ body: space.outbound_ips } = await this.heroku.get(`/spaces/${spaceName}/nat`));
            if (space.outbound_ips && space.outbound_ips.state === 'enabled') {
                const ipv6 = getGeneration(space) === 'fir' ? ' and IPv6' : '';
                natWarning = heredoc `
        ${color.gray('===')} ${color.label('WARNING: Outbound IPs Will Be Reused')}
        ${color.warning(`⚠️ Deleting this space frees up the following outbound IPv4${ipv6} IPs for reuse:`)}
        ${color.label(displayNat(space.outbound_ips) ?? '')}

        ${color.gray('Update the following configurations:')}
        ${color.gray('=')} IP allowlists
        ${color.gray('=')} Firewall rules
        ${color.gray('=')} Security group configurations
        ${color.gray('=')} Network ACLs

        ${color.warning(`Ensure that you remove the listed IPv4${ipv6} addresses from your security configurations.`)}
      `;
            }
        }
        await new ConfirmCommand().confirm(spaceName, confirm, `Destructive Action\nThis command will destroy the space ${color.space(spaceName)}\n${natWarning}\n`);
        ux.action.start(`Destroying space ${color.space(spaceName)}`);
        await this.heroku.delete(`/spaces/${spaceName}`);
        ux.action.stop();
    }
}
