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-f6f7386b.js", ["exports", "./chunk-8d4cd20c.js", "./chunk-6409091d.js", "./chunk-4dfa44ca.js"], function (e, t, s, r) { window.webcomponents.h;
    var n = /** @class */ (function () {
        function n() {
        }
        n.isObservable = function (e) { return e instanceof t.Observable; };
        n.log = function () { return console.log.bind(console); };
        return n;
    }());  var o = new t.Subject, i = function (e, r) {
    if (r === void 0) { r = { timestamp: s.Util.getTimeStamp() }; }
    return o.pipe(t.mergeMap(function (e) { return n.isObservable(e) ? e : t.from([e]); }), t.startWith(r), t.scan(e));
}, a = function (e) { return function () {
    var t = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        t[_i] = arguments[_i];
    }
    var s = e.call.apply(e, [null].concat(t));
    return o.next(s), n.isObservable(s.payload) && o.next(s.payload), s;
}; }; function c(e, t) { switch (t.type) {
    case "CONFIG_LOADING": return Object.assign({}, e, { isLoading: !0, timestamp: s.Util.getTimeStamp() });
    case "CONFIG_LOADED":
        var r_1 = Object.assign({}, e, { isLoading: !1 });
        return Object.keys(t.payload).forEach(function (e) { r_1[e] = t.payload[e]; }), r_1.timestamp = s.Util.getTimeStamp(), r_1;
    case "ADD_CONFIG":
        var n_1 = Object.assign({}, e);
        return Object.keys(t.payload).forEach(function (e) { n_1[e] = t.payload[e]; }), n_1.timestamp = s.Util.getTimeStamp(), n_1;
    default: return e;
} } var u = "undefined" != typeof window && window, p = "undefined" != typeof self && "undefined" != typeof WorkerGlobalScope && self instanceof WorkerGlobalScope && self, l = void 0 !== s.global && s.global, h = u || l || p; function d(e, t) { return void 0 === t && (t = null), new O({ method: "GET", url: e, headers: t }); } function f(e, t, s) { return new O({ method: "POST", url: e, body: t, headers: s }); } function b(e, t) { return new O({ method: "DELETE", url: e, headers: t }); } function y(e, t, s) { return new O({ method: "PUT", url: e, body: t, headers: s }); } function m(e, t, s) { return new O({ method: "PATCH", url: e, body: t, headers: s }); } var g = t.map(function (e, t) { return e.response; }); function w(e, t) { return g(new O({ method: "GET", url: e, responseType: "json", headers: t })); } var O = function (e) { function s(t) { var s = e.call(this) || this, r = { async: !0, createXHR: function () { return this.crossDomain ? function () { if (h.XMLHttpRequest)
        return new h.XMLHttpRequest; if (h.XDomainRequest)
        return new h.XDomainRequest; throw new Error("CORS is not supported by your browser"); }() : function () { if (h.XMLHttpRequest)
        return new h.XMLHttpRequest; var e = void 0; try {
        for (var t = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP", "Msxml2.XMLHTTP.4.0"], s = 0; s < 3; s++)
            try {
                if (e = t[s], new h.ActiveXObject(e))
                    break;
            }
            catch (e) { }
        return new h.ActiveXObject(e);
    }
    catch (e) {
        throw new Error("XMLHttpRequest is not supported by your browser");
    } }(); }, crossDomain: !0, withCredentials: !1, headers: {}, method: "GET", responseType: "json", timeout: 0 }; if ("string" == typeof t)
    r.url = t;
else
    for (var n in t)
        t.hasOwnProperty(n) && (r[n] = t[n]); return s.request = r, s; } var r; return t.__extends(s, e), s.prototype._subscribe = function (e) { return new T(e, this.request); }, s.create = ((r = function (e) { return new s(e); }).get = d, r.post = f, r.delete = b, r.put = y, r.patch = m, r.getJSON = w, r), s; }(t.Observable), T = function (e) { function s(t, s) { var r = e.call(this, t) || this; r.request = s, r.done = !1; var n = s.headers = s.headers || {}; return s.crossDomain || n["X-Requested-With"] || (n["X-Requested-With"] = "XMLHttpRequest"), "Content-Type" in n || h.FormData && s.body instanceof h.FormData || void 0 === s.body || (n["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8"), s.body = r.serializeBody(s.body, s.headers["Content-Type"]), r.send(), r; } return t.__extends(s, e), s.prototype.next = function (e) { this.done = !0; var t = this.xhr, s = this.request, r = this.destination, n = new v(e, t, s); r.next(n); }, s.prototype.send = function () { var e = this.request, s = this.request, r = s.user, n = s.method, o = s.url, i = s.async, a = s.password, c = s.headers, u = s.body, p = e.createXHR, l = t.tryCatch(p).call(e); if (l === t.errorObject)
    this.error(t.errorObject.e);
else {
    if (this.xhr = l, this.setupEvents(l, e), (r ? t.tryCatch(l.open).call(l, n, o, i, r, a) : t.tryCatch(l.open).call(l, n, o, i)) === t.errorObject)
        return this.error(t.errorObject.e), null;
    if (i && (l.timeout = e.timeout, l.responseType = e.responseType), "withCredentials" in l && (l.withCredentials = !!e.withCredentials), this.setHeaders(l, c), (u ? t.tryCatch(l.send).call(l, u) : t.tryCatch(l.send).call(l)) === t.errorObject)
        return this.error(t.errorObject.e), null;
} return l; }, s.prototype.serializeBody = function (e, t) { if (!e || "string" == typeof e)
    return e; if (h.FormData && e instanceof h.FormData)
    return e; if (t) {
    var s = t.indexOf(";");
    -1 !== s && (t = t.substring(0, s));
} switch (t) {
    case "application/x-www-form-urlencoded": return Object.keys(e).map(function (t) { return encodeURIComponent(t) + "=" + encodeURIComponent(e[t]); }).join("&");
    case "application/json": return JSON.stringify(e);
    default: return e;
} }, s.prototype.setHeaders = function (e, t) { for (var s in t)
    t.hasOwnProperty(s) && e.setRequestHeader(s, t[s]); }, s.prototype.setupEvents = function (e, t) { var s, r, n = t.progressSubscriber; function o(e) { var t = o, s = t.subscriber, r = t.progressSubscriber, n = t.request; r && r.error(e), s.error(new j(this, n)); } (e.ontimeout = o, o.request = t, o.subscriber = this, o.progressSubscriber = n, e.upload && "withCredentials" in e) && (n && (s = function (e) { s.progressSubscriber.next(e); }, h.XDomainRequest ? e.onprogress = s : e.upload.onprogress = s, s.progressSubscriber = n), r = function (e) { var t = r, s = t.progressSubscriber, n = t.subscriber, o = t.request; s && s.error(e), n.error(new E("ajax error", this, o)); }, e.onerror = r, r.request = t, r.subscriber = this, r.progressSubscriber = n); function i(e) { } function a(e) { var t = a, s = t.subscriber, r = t.progressSubscriber, n = t.request; if (4 === this.readyState) {
    var o = 1223 === this.status ? 204 : this.status, i = "text" === this.responseType ? this.response || this.responseText : this.response;
    0 === o && (o = i ? 200 : 0), o < 400 ? (r && r.complete(), s.next(e), s.complete()) : (r && r.error(e), s.error(new E("ajax error " + o, this, n)));
} } e.onreadystatechange = i, i.subscriber = this, i.progressSubscriber = n, i.request = t, e.onload = a, a.subscriber = this, a.progressSubscriber = n, a.request = t; }, s.prototype.unsubscribe = function () { var t = this.done, s = this.xhr; !t && s && 4 !== s.readyState && "function" == typeof s.abort && s.abort(), e.prototype.unsubscribe.call(this); }, s; }(t.Subscriber), v = function () { return function (e, t, s) { this.originalEvent = e, this.xhr = t, this.request = s, this.status = t.status, this.responseType = t.responseType || s.responseType, this.response = D(this.responseType, t); }; }(), E = function (e) { function s(t, r, n) { var o = e.call(this, t) || this; return o.name = "AjaxError", o.message = t, o.xhr = r, o.request = n, o.status = r.status, o.responseType = r.responseType || n.responseType, o.response = D(o.responseType, r), Object.setPrototypeOf(o, s.prototype), o; } return t.__extends(s, e), s; }(Error); function D(e, t) { switch (e) {
    case "json": return "response" in t ? t.responseType ? t.response : JSON.parse(t.response || t.responseText || "null") : JSON.parse(t.responseText || "null");
    case "xml": return t.responseXML;
    case "text":
    default: return "response" in t ? t.response : t.responseText;
} } var j = function (e) { function s(t, r) { var n = e.call(this, "ajax timeout", t, r) || this; return n.name = "AjaxTimeoutError", Object.setPrototypeOf(n, s.prototype), n; } return t.__extends(s, e), s; }(E), S = O.create; var C = { loadConfig: a(function (e) { return "string" == typeof e ? { type: "CONFIG_LOADING", payload: S(e).pipe(t.map(function (e) { return { type: "CONFIG_LOADED", payload: e.response }; })) } : { type: "CONFIG_LOADED", payload: e }; }), addConfig: a(function (e) { return { type: "ADD_CONFIG", payload: e }; }), setConfig: a(function (e, t) { var s = {}; return s[e] = t, { type: "ADD_CONFIG", payload: s }; }) }; function x(e, t) { switch (t.type) {
    case "GITHUB_FOLLOWERS_LOADING": return Object.assign({}, e, { isLoading: !0 });
    case "GITHUB_FOLLOWERS_LOADED": return Object.assign({}, e, { isLoading: !1, users: t.payload });
    case "NAME_CHANGED": return Object.assign({}, e, { isLoading: !1, timestamp: s.Util.getTimeStamp(), name: t.payload });
    default: return e;
} } var L = { changeName: a(function (e) { return ({ type: "NAME_CHANGED", payload: e }); }), loadGithubFollowers: a(function (e) { var s = "https://api.github.com/users/" + e.username + "/followers"; return { type: "GITHUB_FOLLOWERS_LOADING", payload: S(s).pipe(t.map(function (e) { return t.map(e.response, "login"); }), t.map(function (e) { return ({ type: "GITHUB_FOLLOWERS_LOADED", payload: e }); })) }; }) }; var U = function (e, t, s, r) { return new (s || (s = Promise))(function (n, o) { function i(e) { try {
    c(r.next(e));
}
catch (e) {
    o(e);
} } function a(e) { try {
    c(r.throw(e));
}
catch (e) {
    o(e);
} } function c(e) { e.done ? n(e.value) : new s(function (t) { t(e.value); }).then(i, a); } c((r = r.apply(e, t || [])).next()); }); }; var A = Symbol(), R = Symbol();
    var q = /** @class */ (function () {
        function q(e) {
            if (this.actions = L, this.baseUrl = "", this.loading = !0, this.authenticated = !1, this.errorMessage = "", e !== R)
                throw new Error("Cannot construct singleton");
            this.base = r.BaseService.instance, this.permissions = r.PermissionsService.instance, this.config = this.base.config, this.user = new r.User("visitor"), this.initStores();
        }
        q.prototype.init = function (e, t) {
            if (t === void 0) { t = ""; }
            return U(this, void 0, void 0, function () { var _a, _b, _c, _d; return __generator(this, function (_e) {
                switch (_e.label) {
                    case 0:
                        "" !== t && (this.baseUrl = t), this.appRoot = e;
                        return [4 /*yield*/, this.base.initApp(this.baseUrl + "/assets/config.json", !0, "_emaApp")];
                    case 1:
                        _e.sent();
                        return [4 /*yield*/, this.permissions.loadRules(this.baseUrl + "/assets/permissions.json", !0, "rules")];
                    case 2:
                        _e.sent(), this.baseUrl = t, this.base.translation.use(this.config.get("language")), this.data = {};
                        _a = this.data;
                        return [4 /*yield*/, s.Util.fetchJson(this.baseUrl + "/assets/collection-manifest.json")];
                    case 3:
                        _a.manifest = _e.sent();
                        _b = this.data;
                        return [4 /*yield*/, s.Util.fetchJson(this.baseUrl + "/assets/data/mock-data.json")];
                    case 4:
                        _b.mockData = _e.sent();
                        _c = this.data;
                        return [4 /*yield*/, s.Util.fetchJson(this.baseUrl + "/assets/data/mock-dwp.json")];
                    case 5:
                        _c.dwpData = _e.sent();
                        _d = this.data;
                        return [4 /*yield*/, s.Util.fetchJson(this.baseUrl + "/assets/data/apiSwagger.json")];
                    case 6: return [2 /*return*/, (_d.swaggerData = _e.sent(), this.collectionManifest = this.data.manifest, this.parseComponents(), this.initHistory(), window.document.location.hostname.match("localhost") && this.config.set("dev", !0), this.data)];
                }
            }); });
        };
        q.prototype.initStores = function () { return U(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                this.store$ = i(x, {}), this.config$ = i(c), this.store$.pipe(t.tap(n.log())).subscribe(function (e) { }), this.config$.pipe().subscribe(function (e) { _this.base.config.add(e, !0); });
                return [2 /*return*/];
            });
        }); };
        q.prototype.test = function () { this.actions.changeName("Bob"), C.loadConfig(this.baseUrl + "/assets/config.json"), C.setConfig("foo", "bar"), console.log(this.user.roles), this.user.addRole("AUTHENTICATED"), this.user.addRole("DEVELOPER"), console.log(this.user.roles), this.user.removeRole("AUTHENTICATED"), console.log(this.user.roles), console.log(this.user.hasRole("DEVELOPER")), console.log(this.user.hasRole("AUTHENTICATED")), console.log(this.config.all()); };
        q.prototype.initHistory = function () { "undefined" != typeof window && window.history; };
        q.prototype.parseComponents = function () {
            var _this = this;
            var e = [];
            s.Util.copy(this.data.manifest.components).map(function (t) { var s = t.tag, r = !1; _this.config.get("componentsIgnorePatterns").forEach(function (e) { s.match(e) && (r = !0); }), r || e.push(t); }), this.components = e;
        };
        q.prototype.showFlashMessage = function (e, t, s) {
            if (s === void 0) { s = 2e3; }
            document.querySelector("ema-flash-message").show(e, t, s);
        };
        Object.defineProperty(q, "instance", {
            get: function () { return this[A] || (this[A] = new q(R)), this[A]; },
            enumerable: true,
            configurable: true
        });
        return q;
    }());  e.AppService = q; });
