"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 });
const Token_handler_1 = require("../Token-handler");
const Config_1 = require("../Config");
const browser_callback_server_1 = require("../browser-callback-server");
const AuthStrategy_1 = require("../AuthStrategy");
const Ewizard_1 = require("../../services/Ewizard");
const Key_cloak_1 = require("../../services/Key-cloak");
class Auth {
    constructor(options) {
        const { apiUrl, ewizardUrl } = getEwizardUrl(options);
        const tokenHandler = this.createTokenHandler(options);
        const browserCallbackServer = new browser_callback_server_1.BrowserCallbackServer();
        const keyCloakClient = new Key_cloak_1.KeyCloakClient(tokenHandler, browserCallbackServer);
        const ewizardClient = new Ewizard_1.EwizardClient(ewizardUrl, apiUrl, tokenHandler, keyCloakClient);
        this.ewizard = ewizardClient;
        this.tokenHandler = tokenHandler;
        this.authorizer = new AuthStrategy_1.default(tokenHandler, ewizardClient, keyCloakClient);
    }
    createTokenHandler(options) {
        return Token_handler_1.TokenHandlerFactory.createTokenHandler(options);
    }
    login(credentials) {
        return this.authorizer.login(credentials);
    }
    logout(silently = false) {
        if (silently) {
            return this.tokenHandler.clear();
        }
        return this.authorizer.logout()
            .then(() => this.tokenHandler.clear());
    }
    checkAuth() {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.ewizard.getComponentLibrary();
            return this.ewizard;
        });
    }
    isTokenActive() {
        return this.checkAuth()
            .catch((err) => !isNotAuth(err));
    }
}
exports.default = Auth;
function getEwizardUrl(options) {
    const config = Config_1.Config.getSync();
    const optionsUrl = options && (options.ewizardUrl || options.url);
    const configUrl = config.ewizard && config.ewizard.url;
    const apiHost = config.ewizard && config.ewizard.apiUrl;
    const apiUrl = apiHost && config.apiPath && apiHost + config.apiPath;
    return { apiUrl, ewizardUrl: optionsUrl || configUrl };
}
function isNotAuth(err) {
    return err.code === 'UNAUTHORIZED' || err.code === 'NOT_LOGGED_IN';
}
//# sourceMappingURL=index.js.map