var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [0, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
/*! Built with http://stenciljs.com */
webcomponents.loadBundle("chunk-d71dfff3.js", ["exports", "./chunk-3b741dc6.js"], function (t, e) { window.webcomponents.h; var s = function (t, e, s, i) { return new (s || (s = Promise))(function (n, a) { function o(t) { try {
    h(i.next(t));
}
catch (t) {
    a(t);
} } function r(t) { try {
    h(i.throw(t));
}
catch (t) {
    a(t);
} } function h(t) { t.done ? n(t.value) : new s(function (e) { e(t.value); }).then(o, r); } h((i = i.apply(t, e || [])).next()); }); };
    var i = /** @class */ (function () {
        function i() {
            this.authenticated = !1, this.dev = !1, this.errorMessage = "", this.api = new e.Api;
        }
        i.prototype.init = function (t) { return s(this, void 0, void 0, function () {
            var e;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        e = this.config = this.applications[t];
                        this.id = t, this.api.init({ host: e.suitecrm.PROD_URL + e.suitecrm.API_URL, headers: e.api_headers, routes: e.api_routes }), this.api.requestCredentials = e.requestCredentials, this.api.corsMode = e.corsMode, this.dev && (this.api.baseUrl = e.suitecrm.DEV_URL + e.suitecrm.API_URL);
                        return [4 /*yield*/, this.api.get(this.api.route("dwp/currentUser")).then(function (t) { return t.statusText && "Unauthorized" === t.statusText ? (_this.authenticated = !1, t) : (_this.authenticated = !0, "undefined" != typeof window && window.localStorage && window.localStorage.setItem("preferred_language", t.data.preferred_language), t); })];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        }); };
        i.prototype.login = function (t) { return s(this, void 0, void 0, function () {
            var e;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        e = t.detail;
                        return [4 /*yield*/, this.api.post(this.api.route("dwp/login"), e).then(function (t) { t.status >= 400 || t.message && "credentials do not meet the requirements" === t.message ? _this.onLoginFailure(t) : _this.onLoginSucces(t); })];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        }); };
        i.prototype.logout = function () { return s(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.api.post("logout", {}).then(function () { _this.authenticated = !1; })];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        }); };
        i.prototype.onLoginSucces = function (t) { this.errorMessage = "", this.authenticated = !0; };
        i.prototype.onLoginFailure = function (t) { t.message && "credentials do not meet the requirements" === t.message && (this.errorMessage = t.message), t.flashMessages && t.flashMessages.length && (this.errorMessage = t.flashMessages[0].text), t.statusText && (this.errorMessage = t.statusText), console.error("loginFailure: ", this.errorMessage), this.authenticated = !1; };
        Object.defineProperty(i.prototype, "applications", {
            get: function () { return { "e-be": { suitecrm: { DEV_URL: "http://localhost:9005", PROD_URL: "http://localhost:9005", API_URL: "/Api/V8_Custom/", IDP_URL: "/Api/V8_Custom/idp.php" }, requestCredentials: "same-origin", corsMode: "cors", 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", PROD_URL: "http://localhost:8585", API_URL: "/essent-crm-osgi/rest/" }, requestCredentials: "same-origin", corsMode: "no-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" }] } }; },
            enumerable: true,
            configurable: true
        });
        return i;
    }());  var n = Symbol(), a = Symbol();
    var o = /** @class */ (function (_super) {
        __extends(o, _super);
        function o(t) {
            var _this = this;
            if (_this = _super.call(this) || this, t !== a)
                throw new Error("Cannot construct singleton");
            _this._type = "UaasService";
            return _this;
        }
        Object.defineProperty(o, "instance", {
            get: function () { return this[n] || (this[n] = new o(a)), this[n]; },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(o.prototype, "type", {
            get: function () { return this._type; },
            set: function (t) { this._type = t; },
            enumerable: true,
            configurable: true
        });
        return o;
    }(i));  var r = function (t, e, s, i) { return new (s || (s = Promise))(function (n, a) { function o(t) { try {
    h(i.next(t));
}
catch (t) {
    a(t);
} } function r(t) { try {
    h(i.throw(t));
}
catch (t) {
    a(t);
} } function h(t) { t.done ? n(t.value) : new s(function (e) { e(t.value); }).then(o, r); } h((i = i.apply(t, e || [])).next()); }); }; var h = Symbol(), c = Symbol();
    var u = /** @class */ (function () {
        function u(t) {
            if (this.baseUrl = "", this.loading = !0, this.authenticated = !1, this.errorMessage = "", t !== c)
                throw new Error("Cannot construct singleton");
            this._type = "AppService", this.uaas = o.instance, this.config = e.ConfigService.instance, this.config.setDefaults(e.configDefaults);
        }
        u.prototype.init = function (t) { return r(this, void 0, void 0, function () { var _a, _b, _c, _d; return __generator(this, function (_e) {
            switch (_e.label) {
                case 0: return [4 /*yield*/, this.config.fetch(this.baseUrl + "/assets/config.json")];
                case 1:
                    _e.sent(), this.baseUrl = t, this.data = {};
                    _a = this.data;
                    return [4 /*yield*/, this.fetchJson(this.baseUrl + "/assets/collection-manifest.json")];
                case 2:
                    _a.manifest = _e.sent();
                    _b = this.data;
                    return [4 /*yield*/, this.fetchJson(this.baseUrl + "/assets/data/mock-data.json")];
                case 3:
                    _b.mockData = _e.sent();
                    _c = this.data;
                    return [4 /*yield*/, this.fetchJson(this.baseUrl + "/assets/data/mock-dwp.json")];
                case 4:
                    _c.dwpData = _e.sent();
                    _d = this.data;
                    return [4 /*yield*/, this.fetchJson(this.baseUrl + "/assets/data/uaasApiSwagger.json")];
                case 5: return [2 /*return*/, (_d.swaggerData = _e.sent(), this.collectionManifest = this.data.manifest, this.parseComponents(), this.initHistory(), document.location.hostname.match("localhost") && this.config.set("dev", !0), this.data)];
            }
        }); }); };
        u.prototype.initHistory = function () { "undefined" != typeof window && window.history; };
        u.prototype.parseComponents = function () {
            var _this = this;
            var t = [];
            e.Util.copy(this.data.manifest.components).map(function (e) { var s = e.tag, i = !1; _this.config.get("componentsIgnorePatterns").forEach(function (t) { s.match(t) && (i = !0); }), i || t.push(e); }), this.components = t;
        };
        u.prototype.fetchJson = function (t) { return r(this, void 0, void 0, function () { return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, fetch(t)];
                case 1: return [4 /*yield*/, (_a.sent()).json()];
                case 2: return [2 /*return*/, _a.sent()];
            }
        }); }); };
        u.prototype.showFlashMessage = function (t, e, s) {
            if (s === void 0) { s = 2e3; }
            document.querySelector("ema-flash-message").show(t, e, s);
        };
        Object.defineProperty(u, "instance", {
            get: function () { return this[h] || (this[h] = new u(c)), this[h]; },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(u.prototype, "type", {
            get: function () { return this._type; },
            set: function (t) { this._type = t; },
            enumerable: true,
            configurable: true
        });
        return u;
    }());  t.AppService = u; });
