import { logger } from "../../common/logger.js";
import { printDiagnosticsToConsole } from "../../common/output.js";
import settings from "../../common/settings.js";
function wait(duration = settings.PROVISIONING_POLL_INTERVAL) {
    return new Promise((resolve) => setTimeout(resolve, duration));
}
export async function pollTeardownOperation(args) {
    const authToken = args.argv.authToken;
    const tunnelId = args.argv["tunnel-id"];
    for (let pollRetry = 0; pollRetry < settings.MAX_PROVISIONING_RETRIES; pollRetry++) {
        printDiagnosticsToConsole(`Polling for teardown operation status... (${pollRetry}/${settings.MAX_PROVISIONING_RETRIES})`);
        const response = await fetch(`${settings.ZUPLO_DEVELOPER_API_ENDPOINT}/v1/accounts/${args.account}/tunnels/${tunnelId}/teardown-operations/${args.teardownOperationId}`, {
            method: "GET",
            headers: {
                Authorization: `Bearer ${authToken}`,
            },
        });
        if (response.ok) {
            const teardownOperation = await response.json();
            switch (teardownOperation.status) {
                case "error":
                    return teardownOperation;
                case "in-progress":
                    await wait();
                    continue;
                case "success":
                    return teardownOperation;
            }
        }
        else {
            logger.error(`Unexpected error from server while polling for teardown operation status: ${response.status} ${response.statusText} ${await response.json()}`);
            throw new Error(`Unexpected response from server while polling for teardown operation status: ${response.status} ${response.statusText}`);
        }
    }
    throw new Error("Unexpected error while polling for teardown operation status");
}
//# sourceMappingURL=poll-teardown-operation.js.map