import { readFileSync } from "node:fs";
import { logger } from "../../common/logger.js";
import { printDiagnosticsToConsole, printResultToConsoleAndExitGracefully, textOrJson, } from "../../common/output.js";
import settings from "../../common/settings.js";
export async function create(argv) {
    const { account, fleet } = argv;
    let configData;
    try {
        const fileContent = readFileSync(argv["configuration-file"], "utf-8");
        configData = JSON.parse(fileContent);
    }
    catch (error) {
        logger.error({ error }, `Failed to read or parse configuration file: ${argv["configuration-file"]}`);
        printDiagnosticsToConsole(`Error: Failed to read or parse configuration file: ${argv["configuration-file"]}`);
        process.exit(1);
    }
    const createResponse = await fetch(`${settings.ZUPLO_DEVELOPER_API_ENDPOINT}/v1/accounts/${account}/fleets/${fleet}/proxy-configuration`, {
        method: "PUT",
        headers: {
            Authorization: `Bearer ${argv.authToken}`,
            "Content-Type": "application/json",
        },
        body: JSON.stringify(configData),
    });
    if (createResponse.ok) {
        await printResultToConsoleAndExitGracefully(`Successfully created proxy configuration for fleet ${fleet}`);
    }
    else {
        const response = textOrJson(await createResponse.text());
        logger.error({
            status: createResponse.status,
            statusText: createResponse.statusText,
            response,
        }, "Failed to create proxy configuration");
        printDiagnosticsToConsole("Error: Failed to create proxy configuration. Check the arguments and configuration file.", response);
        process.exit(1);
    }
}
//# sourceMappingURL=handler.js.map