import { logger } from "../../common/logger.js";
import { printDiagnosticsToConsole, printResultToConsoleAndExitGracefully, printTableToConsoleAndExitGracefully, } from "../../common/output.js";
import settings from "../../common/settings.js";
export async function list(argv) {
    const { account } = await argv;
    const listResponse = await fetch(`${settings.ZUPLO_DEVELOPER_API_ENDPOINT}/v1/accounts/${account}/tunnels`, {
        method: "GET",
        headers: {
            Authorization: `Bearer ${argv.authToken}`,
        },
    });
    if (listResponse.ok) {
        const tunnels = await listResponse.json();
        if (tunnels.data.length === 0) {
            const output = "No tunnels found";
            await printResultToConsoleAndExitGracefully(output);
        }
        else {
            const table = tunnels.data.map((tunnel) => {
                return { "tunnel-id": tunnel.id, name: tunnel.name };
            });
            await printTableToConsoleAndExitGracefully(table);
        }
    }
    else {
        const response = await listResponse.json();
        logger.error({
            status: listResponse.status,
            statusText: listResponse.statusText,
            response,
        }, "Failed to list tunnels for account");
        printDiagnosticsToConsole("Error: Failed to list tunnels for your account. Try again later.", response);
    }
}
//# sourceMappingURL=handler.js.map