import { logger } from "../../common/logger.js";
import { printResultToConsoleAndExitGracefully, printSpinnerToConsole, } from "../../common/output.js";
import settings from "../../common/settings.js";
export async function create(argv) {
    const account = argv.account;
    if (!account) {
        throw new Error("Invalid state: account is not set.");
    }
    if (!argv.name || typeof argv.name !== "string") {
        throw new Error("Invalid state: name is not set.");
    }
    const success = await createProject({
        name: argv.name,
        account,
        authToken: argv.authToken,
    });
    if (success) {
        await printResultToConsoleAndExitGracefully("Project created successfully.");
    }
}
export async function createProject({ name, account, authToken, }) {
    const spinner = printSpinnerToConsole(`Creating project ${name} for account ${account}...`);
    const response = await fetch(`${settings.ZUPLO_DEVELOPER_API_ENDPOINT}/v1/projects?accountName=${encodeURIComponent(account)}`, {
        method: "POST",
        headers: {
            Authorization: `Bearer ${authToken}`,
            "Content-Type": "application/json",
        },
        body: JSON.stringify({
            name,
        }),
    });
    if (!response.ok) {
        let errorJson;
        try {
            errorJson = await response.json();
        }
        catch {
        }
        logger.error(errorJson, "Failed to create project.");
        await printResultToConsoleAndExitGracefully(errorJson?.detail ?? errorJson?.title ?? `Failed to create project.`, spinner);
        return false;
    }
    spinner.succeed(`Project ${name} created successfully.`);
    return true;
}
//# sourceMappingURL=handler.js.map