import { color, hux } from '@heroku/heroku-cli-util';
import { Command, flags } from '@heroku-cli/command';
import { ux } from '@oclif/core/ux';
import tsheredoc from 'tsheredoc';
import { displayVPNStatus } from '../../../lib/spaces/format.js';
const heredoc = tsheredoc.default;
export default class Connections extends Command {
    static description = 'list the VPN Connections for a space';
    static example = heredoc `
  ${color.command('heroku spaces:vpn:connections --space my-space')}
    === my-space VPN Connections
     Name   Status Tunnels
     ────── ────── ───────
     office active UP/UP`;
    static flags = {
        json: flags.boolean({ description: 'output in json format' }),
        space: flags.string({ char: 's', description: 'space to get VPN connections from', required: true }),
    };
    static topic = 'spaces';
    displayVPNConnections(space, connections) {
        if (connections.length === 0) {
            ux.stdout('No VPN Connections have been created yet');
            return;
        }
        hux.styledHeader(`${space} VPN Connections`);
        hux.table(connections, {
            Name: {
                get: c => c.name || c.id,
            },
            Status: {
                get: c => displayVPNStatus(c.status),
            },
            Tunnels: {
                get: c => this.tunnelFormat(c.tunnels),
            },
        });
    }
    render(space, connections, json) {
        if (json) {
            hux.styledJSON(connections);
        }
        else {
            this.displayVPNConnections(space, connections);
        }
    }
    async run() {
        const { flags } = await this.parse(Connections);
        const { json, space } = flags;
        const { body: connections } = await this.heroku.get(`/spaces/${space}/vpn-connections`);
        this.render(space, connections, json);
    }
    tunnelFormat(t) {
        return t.map(tunnel => displayVPNStatus(tunnel.status)).join('/');
    }
}
