import { confirm } from "@inquirer/prompts";
import { login } from "../../login/login.js";
import { getAuthToken } from "../../login/tokens.js";
import { printCriticalFailureToConsoleAndExit } from "../output.js";
async function fail() {
    await printCriticalFailureToConsoleAndExit(`You are not authenticated. Please run \`zuplo login\` to log in or set an api key.`);
}
export async function authenticate(argv) {
    if (argv["api-key"]) {
        Object.assign(argv, { authToken: argv["api-key"] });
        return;
    }
    let token = await getAuthToken();
    if (!token) {
        const result = await confirm({
            message: "You are not authenticated. Would you like to log in?",
        });
        if (!result) {
            return fail();
        }
        await login();
        token = await getAuthToken();
        if (!token) {
            return fail();
        }
    }
    Object.assign(argv, { authToken: token });
}
//# sourceMappingURL=authentication.js.map