import { logger } from "../../common/logger.js";
import { printCriticalFailureToConsoleAndExit, printDiagnosticsToConsole, printResultToConsoleAndExitGracefully, textOrJson, } from "../../common/output.js";
import settings from "../../common/settings.js";
import { pollTeardownOperation } from "./poll-teardown-operation.js";
export async function deleteTunnel(argv) {
    const { account } = argv;
    const deleteResponse = await fetch(`${settings.ZUPLO_DEVELOPER_API_ENDPOINT}/v1/accounts/${account}/tunnels/${argv["tunnel-id"]}`, {
        method: "DELETE",
        headers: {
            Authorization: `Bearer ${argv.authToken}`,
        },
    });
    if (deleteResponse.ok) {
        const teardownOperation = await deleteResponse.json();
        printDiagnosticsToConsole(`Deleting tunnel ${argv["tunnel-id"]} on account ${account}...`);
        const polledTearDownOperation = await pollTeardownOperation({
            argv,
            account,
            teardownOperationId: teardownOperation.id,
        });
        if (polledTearDownOperation.status === "success") {
            await printResultToConsoleAndExitGracefully(`Tunnel ${argv["tunnel-id"]} deleted successfully.`);
        }
        else {
            printDiagnosticsToConsole(polledTearDownOperation.details);
            await printCriticalFailureToConsoleAndExit(`Tunnel ${argv["tunnel-id"]} failed to delete. Here's the error: ${polledTearDownOperation.message}`);
        }
    }
    else {
        const response = textOrJson(await deleteResponse.text());
        logger.error({
            status: deleteResponse.status,
            statusText: deleteResponse.statusText,
            response,
        }, "Failed to delete tunnel for account");
        printDiagnosticsToConsole("Error: Failed to delete the tunnel for your account. Check the arguments.", response);
    }
}
//# sourceMappingURL=handler.js.map