var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { Api } from '../api';
// import { Storage} from "../index";
export class Uaas {
    constructor() {
        this.authenticated = false;
        this.dev = false;
        this.errorMessage = '';
        this.api = new Api();
    }
    init(id) {
        return __awaiter(this, void 0, void 0, function* () {
            let config = this.config = this.applications[id];
            this.id = id;
            this.api.init({
                host: config['suitecrm']['PROD_URL'] + config['suitecrm']['API_URL'],
                headers: config['api_headers'],
                routes: config['api_routes']
            });
            this.api.requestCredentials = config.requestCredentials;
            this.api.corsMode = config.corsMode;
            if (this.dev) {
                this.api.baseUrl = config['suitecrm']['DEV_URL'] + config['suitecrm']['API_URL'];
            }
            yield this.api.get(this.api.route('dwp/currentUser')).then((res) => {
                if (res.statusText && res.statusText === 'Unauthorized') {
                    this.authenticated = false;
                    return res;
                }
                this.authenticated = true;
                if (typeof window !== 'undefined' && window.localStorage) {
                    window.localStorage.setItem('preferred_language', res.data.preferred_language);
                }
                return res;
            });
        });
    }
    login(event) {
        return __awaiter(this, void 0, void 0, function* () {
            let credentials = event.detail;
            return yield this.api.post(this.api.route('dwp/login'), credentials).then((result) => {
                if (result.status >= 400 || (result.message && result.message === 'credentials do not meet the requirements')) {
                    this.onLoginFailure(result);
                }
                else {
                    this.onLoginSucces(result);
                }
            });
        });
    }
    logout() {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.api.post('logout', {}).then(() => {
                this.authenticated = false;
            });
        });
    }
    onLoginSucces(result) {
        this.errorMessage = '';
        this.authenticated = true;
    }
    onLoginFailure(result) {
        if (result.message && result.message === 'credentials do not meet the requirements') {
            this.errorMessage = result.message;
        }
        if (result.flashMessages && result.flashMessages.length) {
            this.errorMessage = result.flashMessages[0].text;
        }
        if (result.statusText) {
            this.errorMessage = result.statusText;
        }
        console.error('loginFailure: ', this.errorMessage);
        this.authenticated = false;
    }
    get applications() {
        return {
            "e-be": {
                "suitecrm": {
                    "DEV_URL": "http://localhost:9005",
                    // "DEV_URL": "http://suitecrm.essent.be.local",
                    "PROD_URL": "http://localhost:9005",
                    "API_URL": "/Api/V8_Custom/",
                    "IDP_URL": "/Api/V8_Custom/idp.php"
                },
                // "requestCredentials": "include",
                "requestCredentials": "same-origin",
                "corsMode": "cors",
                // "corsMode": "same-origin",
                "swaggerUrl": "",
                "api_headers": [
                    { "Content-Type": "application/json;charset=utf-8" }
                ],
                "api_routes": [
                    { "dwp/login": "login" },
                    { "dwp/currentUser": "user/current" },
                    { "dwp/Menu/start": "Menu/start" }
                ],
                "api_routes_dev": [
                    { "case": "/v1/case" }
                ]
            },
            "e-nl": {
                "suitecrm": {
                    "DEV_URL": "http://localhost:8585",
                    // "DEV_URL": "http://localhost:9007",
                    // "DEV_URL": "http://suitecrm.essent.nl.local",
                    "PROD_URL": "http://localhost:8585",
                    // "API_URL": "/essent-crm-osgi/rest/",
                    "API_URL": "/essent-crm-osgi/rest/",
                },
                "requestCredentials": "same-origin",
                // "requestCredentials": "include",
                "corsMode": "no-cors",
                // "corsMode": "cors",
                "swaggerUrl": "api-docs/uaasApi",
                "api_headers": [
                    { "Content-Type": "application/json;charset=utf-8" }
                ],
                "api_routes": [
                    { "dwp/login": "dwp/login" },
                    { "dwp/currentUser": "dwp/currentUser" },
                    { "dwp/Menu/start": "dwp/Menu/start" }
                ],
                "api_routes_dev": [
                    { "case": "/v1/case" }
                ]
            }
        };
    }
}
//# sourceMappingURL=uaas.js.map