"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "getCommands", {
    enumerable: true,
    get: function() {
        return getCommands;
    }
});
function _commander() {
    const data = require("commander");
    _commander = function() {
        return data;
    };
    return data;
}
const _profile = require("./profile");
function _axios() {
    const data = /*#__PURE__*/ _interop_require_default(require("axios"));
    _axios = function() {
        return data;
    };
    return data;
}
const _exceptions = require("./exceptions");
const _common = require("./common");
function _chalk() {
    const data = /*#__PURE__*/ _interop_require_default(require("chalk"));
    _chalk = function() {
        return data;
    };
    return data;
}
function _prompts() {
    const data = require("@inquirer/prompts");
    _prompts = function() {
        return data;
    };
    return data;
}
function _result() {
    const data = require("~/base/js/result");
    _result = function() {
        return data;
    };
    return data;
}
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const getCommands = ()=>{
    return [
        new (_commander()).Command("login").argument("[api key]").action(async (apiKey)=>{
            const handle = async (apiKey, verbose)=>{
                const profile = await _profile.Profile.load();
                profile.data.apiKey = apiKey;
                const client = await profile.getClient(false);
                try {
                    const user = await client.me();
                    verbose && console.log(`${_common.QPACE_BG_PREFIX}Logged in as ${_chalk().default.yellowBright(user.name)}`);
                } catch (e) {
                    if (_axios().default.isAxiosError(e) && e.response?.status == 403) {
                        return _result().Result.err(new _exceptions.CliError("Invalid API key. Try authenticating again."));
                    }
                    return _result().Result.err(e);
                }
                return _result().Result.ok(profile);
            };
            console.log(`${_common.QPACE_BG_PREFIX}Logging into qpace.dev`);
            console.log(`${_common.QPACE_BG_PREFIX}You can find your API key in your browser here: ${_chalk().default.cyanBright(`https://qpace.dev/auth`)}`);
            if (apiKey == null) {
                console.log(`${_common.QPACE_BG_PREFIX}Paste an API key here and press enter, or press ctrl+c to quit`);
                apiKey = await (0, _prompts().input)({
                    message: "Enter your API key",
                    transformer: (apiKey)=>apiKey?.trim(),
                    validate: async (apiKey)=>{
                        if (!apiKey.length) return "API key is required";
                        const res = await handle(apiKey, false);
                        if (_result().Result.isErr(res)) return "API key is invalid";
                        return true;
                    }
                });
            }
            const profile = _result().Result.unwrap(await handle(apiKey, true));
            await profile.save();
        }),
        new (_commander()).Command("logout").action(async ()=>{
            const profile = await _profile.Profile.load();
            profile.data.apiKey = undefined;
            await profile.save();
            console.log(`${_common.QPACE_BG_PREFIX}Logged out successfully.`);
        })
    ];
};
