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 './index';
export class Uaas {
    constructor() {
        this.applications = {
            "essent-be": {
                "suitecrm": {
                    "DEV_URL": "http://suitecrm.essent.be.local",
                    "PROD_URL": "https://prod.supernova.essent.be",
                    "API_URL": "/Api/V8_Custom/",
                    "IDP_URL": "/Api/V8_Custom/idp.php"
                },
                "api_headers": [
                    { "Content-Type": "application/json;charset=utf-8" }
                ],
                "api_routes": [
                    { "login": "login" },
                    { "user|current": "user/current" },
                    { "Menu|start": "Menu/start" }
                ],
                "api_routes_dev": [
                    { "case": "/v1/case" }
                ]
            },
            "essent-nl": {
                "suitecrm": {
                    "DEV_URL": "http://suitecrm.essent.nl.local",
                    "PROD_URL": "https://prod.supernova.essent.nl",
                    "API_URL": "/Api/V8_Custom/",
                    "IDP_URL": "/Api/V8_Custom/idp.php"
                },
                "api_headers": [
                    { "Content-Type": "application/json;charset=utf-8" }
                ],
                "api_routes": [
                    { "login": "login" },
                    { "user|current": "user/current" },
                    { "Menu|start": "Menu/start" }
                ],
                "api_routes_dev": [
                    { "case": "/v1/case" }
                ]
            }
        };
        this.api = new Api();
    }
    init(id, config) {
        this.id = id;
        this.config = config;
        this.api.init({
            host: config['suitecrm']['DEV_URL'] + config['suitecrm']['API_URL'],
            headers: config['api_headers'],
            routes: config['api_routes']
        });
    }
    // fixme: session token
    login(event) {
        return __awaiter(this, void 0, void 0, function* () {
            const self = this;
            let credentials = event.detail;
            yield this.api.post('login', credentials).then((result) => {
                console.log('login');
                console.log(result);
                console.log('cookie', document.cookie);
                console.log(this.getCookie('SUITECRM_REST_API_TOKEN'));
                // fixme: get token
                self.api.setCredentials({ username: credentials.username, token: '#$#$#$#$#' }, 'uaas:essent-be');
                //self.api.setCredentials(credentials,'uaas:essent-be');
                self.onLoginSucces();
            });
        });
    }
    onLoginSucces() {
        return __awaiter(this, void 0, void 0, function* () {
            console.log('onLoginSucces');
            let data = {
                user: {},
                menuStart: {}
            };
            data.user = yield this.api.get('user/current');
            data.menuStart = yield this.api.get('Menu/start');
            console.log('data', data);
        });
    }
    getCookie(name) {
        var re = new RegExp(name + "=([^;]+)");
        var value = re.exec(document.cookie);
        return (value != null) ? unescape(value[1]) : null;
    }
}
//# sourceMappingURL=uaas.js.map