import { readFile } from "node:fs/promises";
import { createApiClient } from "../../../common/api/client.js";
import { printCriticalFailureToConsoleAndExit, printDiagnosticsToConsole, printResultToConsoleAndExitGracefully, } from "../../../common/output.js";
import { pollProvisioningOperation } from "./poll-provisioning-operations.js";
export async function updateServices(argv) {
    let contents;
    try {
        contents = await readFile(argv["configuration-file"], "utf-8");
    }
    catch (err) {
        await printCriticalFailureToConsoleAndExit(`Error reading the configuration file at ${argv["configuration-file"]}: ${err.message}`);
        return;
    }
    const { account } = argv;
    const client = createApiClient({ authToken: argv.authToken });
    const provisioningOperation = await client.put(`/v1/accounts/${account}/tunnels/${argv["tunnel-id"]}/services-configuration`, {
        operation: "Failed to update tunnel for account",
        errorMessage: "Error: Failed to update the tunnel for your account. Check the arguments.",
        body: contents,
    });
    printDiagnosticsToConsole(`Updating services on tunnel ${argv["tunnel-id"]} on account ${account}...`);
    const polledProvisioningOperation = await pollProvisioningOperation({
        argv,
        account,
        provisioningOperationId: provisioningOperation.id,
    });
    if (polledProvisioningOperation.status === "success") {
        await printResultToConsoleAndExitGracefully(`Tunnel ${argv["tunnel-id"]} updated successfully.`);
    }
    else {
        printDiagnosticsToConsole(polledProvisioningOperation.details);
        await printCriticalFailureToConsoleAndExit(`Tunnel ${argv["tunnel-id"]} failed to update. Here's the error: ${polledProvisioningOperation.message}`);
    }
}
//# sourceMappingURL=handler.js.map