"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeyCloakStrategy = void 0;
const Config_1 = require("../../../Config");
const WizError_1 = require("../../../WizError");
class KeyCloakStrategy {
    constructor(tokenHandler, ewizardClient, keyCloakClient) {
        this.tokenHandler = tokenHandler;
        this.ewizardClient = ewizardClient;
        this.keyCloakClient = keyCloakClient;
    }
    login(credentials) {
        return this.configureAuthServerData(credentials.ewizardUrl)
            .then(() => this.keyCloakClient.login(credentials))
            .then((tokens) => this.saveTokens(tokens))
            .then(() => this.reloadEwizardClientEndpoints())
            .then(() => this.collectEwizardConfig())
            .then(saveToConfig('ewizard'))
            .then(() => this.getComponentLibraryConfig())
            .then(saveToConfig('componentLibrary'))
            .then(() => this.getGraylogConfg())
            .then(saveToConfig('graylog'));
    }
    logout() {
        return this.keyCloakClient.logout();
    }
    checkAuth() {
        return this.configureAuthServerData(this.ewizardClient.url)
            .then(() => this.keyCloakClient.checkAuth());
    }
    saveTokens(tokens) {
        return this.tokenHandler.set(tokens);
    }
    configureAuthServerData(ewizardUrl) {
        if (!ewizardUrl) {
            return Promise.reject(new WizError_1.WizError('NO_EWIZARD_INSTANCE'));
        }
        this.ewizardClient.url = ewizardUrl;
        return this.ewizardClient.getConfig()
            .then(saveToConfig('authServer'));
    }
    collectEwizardConfig() {
        return __awaiter(this, void 0, void 0, function* () {
            const [userData, ewizardVersion] = yield Promise.all([
                this.ewizardClient.getUser(),
                this.ewizardClient.getEwizardVersion(),
            ]);
            return {
                apiUrl: userData.token.content.apiUrl,
                userId: userData.id,
                email: userData.email,
                version: ewizardVersion,
            };
        });
    }
    getComponentLibraryConfig() {
        return this.ewizardClient.getComponentLibrary();
    }
    reloadEwizardClientEndpoints() {
        return Config_1.Config.set({
            ewizard: {
                url: this.ewizardClient.url,
            }
        })
            .then(Config_1.Config.get)
            .then((config) => {
            this.ewizardClient.apiUrl = config.ewizard.apiUrl + config.apiPath;
        });
    }
    getGraylogConfg() {
        return this.ewizardClient.getGraylog();
    }
}
exports.KeyCloakStrategy = KeyCloakStrategy;
function saveToConfig(key) {
    return (data) => {
        const config = {
            [key]: data,
        };
        return Config_1.Config.set(config);
    };
}
//# sourceMappingURL=index.js.map