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';
export class Uaas {
    constructor() {
        this.authenticated = false;
        this.dev = false;
        this.errorMessage = '';
        this.api = new Api();
    }
    init(id, config) {
        return __awaiter(this, void 0, void 0, function* () {
            this.config = config;
            this.id = id;
            this.api.init({
                host: config['api_endpoints']['PROD_URL'] + config['api_endpoints']['API_URL'],
                headers: config['api_headers'],
                routes: config['api_routes']
            });
            this.api.requestCredentials = config.requestCredentials;
            this.api.corsMode = config.corsMode;
            console.log(config);
            // fixme:
            if (this.dev) {
                this.api.baseUrl = config['api_endpoints']['DEV_URL'] + config['api_endpoints']['API_URL'];
            }
            yield this.api.get(this.api.route('dwp/currentUser')).then((res) => {
                console.log(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) => {
                console.log(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;
    }
}
//# sourceMappingURL=uaas.js.map