"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MablApiClientFactory = void 0;
const mablApiClient_1 = require("./mablApiClient");
const authenticationProvider_1 = require("../providers/authenticationProvider");
const cliConfigProvider_1 = require("../providers/cliConfigProvider");
const types_1 = require("./types");
const chalk_1 = __importDefault(require("chalk"));
class MablApiClientFactory {
    static createApiClientFromOptionalApiKey(apiKey, shouldExitOnError) {
        if (apiKey) {
            return MablApiClientFactory.createApiClient({
                authType: types_1.AuthType.ApiKey,
                token: apiKey,
            }, shouldExitOnError ?? true);
        }
        return MablApiClientFactory.createApiClient(undefined, shouldExitOnError ?? true);
    }
    static createApiClientFromApiKey(apiKey, apiUrl) {
        return MablApiClientFactory.createApiClient({
            apiUrl,
            authType: types_1.AuthType.ApiKey,
            token: apiKey,
        });
    }
    static createApiClientFromBearerToken(bearerToken) {
        return MablApiClientFactory.createApiClient({
            authType: types_1.AuthType.Bearer,
            token: bearerToken,
        });
    }
    static async createApiClient(opts, shouldExitOnError) {
        const { mabl } = (await cliConfigProvider_1.CliConfigProvider.getCliConfig()).http;
        const optsFromCliHttpConfig = {
            proxyUrl: mabl.proxyHost,
            sslVerify: mabl.sslVerify,
            proxyType: mabl.proxyType,
        };
        if (opts) {
            return new mablApiClient_1.MablApiClient({
                ...optsFromCliHttpConfig,
                ...opts,
            });
        }
        const authProvider = new authenticationProvider_1.AuthenticationProvider(shouldExitOnError ?? true);
        const authConfig = await authProvider.getAuthConfigWithAutoRenew();
        if (authConfig?.accessToken) {
            if (authConfig.authType === types_1.AuthType.Bearer) {
                return new mablApiClient_1.MablApiClient({
                    ...optsFromCliHttpConfig,
                    authType: authConfig.authType,
                    tokenAccessor: async () => {
                        const refreshedConfig = await authProvider.getAuthConfigWithAutoRenew();
                        return refreshedConfig?.accessToken;
                    },
                });
            }
            return new mablApiClient_1.MablApiClient({
                ...optsFromCliHttpConfig,
                authType: authConfig.authType,
                token: authConfig.accessToken,
            });
        }
        throw new Error('Please supply an API key or authenticate the mabl CLI tool');
    }
    static async createUserApiClient(shouldExitOnError) {
        if ((await authenticationProvider_1.AuthenticationProvider.getAuthType()) !== types_1.AuthType.Bearer) {
            MablApiClientFactory.throwUserAuthTypeError();
        }
        return MablApiClientFactory.createApiClient(undefined, shouldExitOnError);
    }
    static async createApiClientForAccessToken(accessToken, apiUrlOverride) {
        const httpConfig = (await cliConfigProvider_1.CliConfigProvider.getCliConfig()).http.mabl;
        return new mablApiClient_1.MablApiClient({
            apiUrl: apiUrlOverride,
            authType: types_1.AuthType.Bearer,
            token: accessToken,
            proxyUrl: httpConfig.proxyHost,
            sslVerify: httpConfig.sslVerify,
            proxyType: httpConfig.proxyType,
        });
    }
    static throwUserAuthTypeError() {
        throw new Error(chalk_1.default.red(`Auth with user credentials required. Please run ${chalk_1.default.magenta('mabl auth login')} to log in with your mabl account`));
    }
}
exports.MablApiClientFactory = MablApiClientFactory;
