"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Profile", {
    enumerable: true,
    get: function() {
        return Profile;
    }
});
function _fs() {
    const data = require("~/base/node/fs");
    _fs = function() {
        return data;
    };
    return data;
}
function _client() {
    const data = require("~/lib/node/client");
    _client = function() {
        return data;
    };
    return data;
}
function _path() {
    const data = require("path");
    _path = function() {
        return data;
    };
    return data;
}
function _os() {
    const data = require("os");
    _os = function() {
        return data;
    };
    return data;
}
function _promises() {
    const data = require("fs/promises");
    _promises = function() {
        return data;
    };
    return data;
}
function _axios() {
    const data = /*#__PURE__*/ _interop_require_default(require("axios"));
    _axios = function() {
        return data;
    };
    return data;
}
const _exceptions = require("./exceptions");
function _chalk() {
    const data = /*#__PURE__*/ _interop_require_default(require("chalk"));
    _chalk = function() {
        return data;
    };
    return data;
}
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const getProfileDataPath = (id)=>{
    return (0, _path().resolve)((0, _os().homedir)(), ".qpace", `profile_${id}.json`);
};
class Profile {
    static async load(id = "prod") {
        const path = getProfileDataPath(id);
        let data = {};
        if (await (0, _fs().exists)(path)) {
            data = await (0, _fs().readJson)(path);
        }
        if (Profile.instance == null) {
            Profile.instance = new Profile(id, data);
        }
        return Profile.instance;
    }
    async save() {
        const path = getProfileDataPath(this.id);
        await (0, _promises().mkdir)((0, _path().dirname)(path), {
            recursive: true
        });
        await (0, _fs().writeJson)(path, this.data);
    }
    async getClient(cache = true) {
        if (this.client == null || !cache) {
            let apiBase = "https://api.qpace.dev/v1";
            let grpcApiBase = "grpc.qpace.dev";
            let apiKey = this.data.apiKey;
            if (process.env["DEV"]) {
                apiBase = "http://0.0.0.0:3000/v1";
                grpcApiBase = "0.0.0.0:3001";
            }
            if (process.env["QPACE_API_KEY"]?.length) {
                apiKey = process.env["QPACE_API_KEY"];
            }
            if (process.env["QPACE_API_BASE"]?.length) {
                apiBase = process.env["QPACE_API_BASE"];
            }
            if (process.env["QPACE_GRPC_API_BASE"]?.length) {
                grpcApiBase = process.env["QPACE_GRPC_API_BASE"];
            }
            if (!apiKey?.length) {
                throw new _exceptions.CliError(`API key is required`);
            }
            this.client = new (_client()).Client({
                apiKey,
                apiBase,
                grpcApiBase
            });
        }
        return this.client;
    }
    async ping() {
        const client = await this.getClient();
        try {
            await client.me();
        } catch (e) {
            if (_axios().default.isAxiosError(e) && e.response?.status == 403) {
                throw new _exceptions.CliError(`Invalid API key. Try authenticating again using "${_chalk().default.yellowBright(`qpace login`)}"`);
            }
            throw e;
        }
    }
    constructor(id, data){
        this.id = id;
        this.data = data;
    }
}
