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-735731b6.js", ["exports", "./chunk-6409091d.js", "./chunk-4bc80c3c.js"], function (exports, __chunk_2, __chunk_3) { var h = window.webcomponents.h; var __awaiter = function (t, e, r, n) { return new (r || (r = Promise))(function (i, o) { function s(t) { try {
    u(n.next(t));
}
catch (t) {
    o(t);
} } function a(t) { try {
    u(n.throw(t));
}
catch (t) {
    o(t);
} } function u(t) { t.done ? i(t.value) : new r(function (e) { e(t.value); }).then(s, a); } u((n = n.apply(t, e || [])).next()); }); };
    var BaseApi = /** @class */ (function () {
        function BaseApi() {
            this.headers = {}, this.routes = {}, this.baseUrl = "/api", this.idField = "id", this.requestCredentials = "same-origin", this.corsMode = "same-origin", this.cachePolicy = "no-cache", this.entities = {}, this.hasDebugger = !1;
        }
        BaseApi.prototype.init = function (t) {
            var _this = this;
            if (this.settings = t, this.settings.host && (this.baseUrl = t.host), this.settings.routes && this.initApiRoutes(this.settings.routes), this.settings.headers) {
                var t_1 = this;
                this.settings.headers.forEach(function (e) { for (var r in e)
                    t_1.headers[r] = e[r]; });
            }
            this.httpHeaders && Object.keys(this.headers).forEach(function (t) { _this.httpHeaders.set(t, _this.headers[t]); });
        };
        BaseApi.prototype.route = function (t) {
            var _this = this;
            var e;
            return Object.keys(this.routes).forEach(function (r) { r === t && (e = _this.routes[r]); }), e || t;
        };
        BaseApi.prototype.post = function (t, e) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, fetch("" + this.baseUrl + t, { body: JSON.stringify(e), cache: this.cachePolicy, credentials: this.requestCredentials, headers: this.headers, mode: this.corsMode, method: "POST" }).then(function (t) { return t.status >= 400 ? (console.log(t), console.error("Bad response from server"), t) : t.json(); }).catch(this.errorHandler).then(function (t) { return t; })];
                case 1: return [2 /*return*/, _a.sent()];
            }
        }); }); };
        BaseApi.prototype.get = function (t, e) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    e && (t += "?", Object.keys(e).forEach(function (r) { return t += r + "=" + e[r] + "&"; }));
                    return [4 /*yield*/, fetch("" + this.baseUrl + t, { cache: this.cachePolicy, credentials: this.requestCredentials, headers: this.headers, mode: this.corsMode, method: "GET" }).then(function (t) { return t.status >= 400 ? (console.log(t), console.log("Bad response from server"), t) : t.json(); }).catch(this.errorHandler).then(function (t) { return t; })];
                case 1: return [2 /*return*/, _a.sent()];
            }
        }); }); };
        BaseApi.prototype.patch = function (t, e, r) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, fetch("" + this.baseUrl + t + "/" + e, { headers: this.headers, method: "patch", body: JSON.stringify(r) })];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        }); }); };
        BaseApi.prototype.delete = function (t, e) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
            console.log(t, e);
            return [2 /*return*/];
        }); }); };
        BaseApi.prototype.login = function (t, e) { console.log(t, e.length); };
        BaseApi.prototype.logout = function (t, e) {
            if (t === void 0) { t = "app:authData"; }
            if (e === void 0) { e = !1; }
            this.clearCredentials(t), this.settings.authenticated = !1, e && (location.href = location.href);
        };
        BaseApi.prototype.setCredentials = function (t, e) {
            if (e === void 0) { e = "app-auth"; }
            this.credentials = t, "undefined" != typeof window && window.localStorage && window.localStorage.setItem(e, JSON.stringify(t));
        };
        BaseApi.prototype.getCredentials = function (t) {
            if (t === void 0) { t = "app:authData"; }
            if ("undefined" != typeof window && window.localStorage && window.localStorage.getItem(t))
                return JSON.parse(window.localStorage.getItem(t));
        };
        BaseApi.prototype.clearCredentials = function (t) {
            if (t === void 0) { t = "app:authData"; }
            "undefined" != typeof window && window.localStorage.getItem(t) && window.localStorage.removeItem(t);
        };
        BaseApi.prototype.initApiRoutes = function (t) { var e = this; t.forEach(function (t) { for (var r in t)
            e.routes[r] = t[r]; }), e.settings.dev && e.settings.routes_dev && e.settings.routes_dev.forEach(function (t) { for (var r in t)
            e.routes[r] = t[r]; }); };
        BaseApi.prototype.errorHandler = function (t) { console.error(t); };
        BaseApi.prototype.saveLocalResource = function (t) { localStorage.setItem(t[this.idField], t); };
        BaseApi.prototype.loadLocalResource = function (t) { localStorage.getItem(t) && localStorage.getItem(t); };
        BaseApi.prototype.flushLocalResource = function (t) { localStorage.getItem(t) && localStorage.removeItem(t); };
        return BaseApi;
    }());  var __awaiter$1 = function (t, e, r, n) { return new (r || (r = Promise))(function (i, o) { function s(t) { try {
    u(n.next(t));
}
catch (t) {
    o(t);
} } function a(t) { try {
    u(n.throw(t));
}
catch (t) {
    o(t);
} } function u(t) { t.done ? i(t.value) : new r(function (e) { e(t.value); }).then(s, a); } u((n = n.apply(t, e || [])).next()); }); };
    var Config = /** @class */ (function () {
        function Config() {
            this._config = {}, this._locked = ["_config", "_defaults"];
        }
        Config.prototype.get = function (t) { return this._config[t]; };
        Config.prototype.set = function (t, e, r) {
            if (r === void 0) { r = !1; }
            if (!this.islocked(t))
                return r && (e = __chunk_2.Util.castToType(e)), this._config[t] = e;
            console.error('Config: property "' + t + '" can not be changed.');
        };
        Config.prototype.add = function (t, e, r) {
            if (e === void 0) { e = !1; }
            if (r === void 0) { r = !1; }
            var n = this;
            e ? this.extend(t) : Object.keys(t).forEach(function (e) { n.set(e, t[e], r); });
        };
        Config.prototype.fetch = function (t, e) {
            if (e === void 0) { e = !1; }
            return __awaiter$1(this, void 0, void 0, function () { var r, n; return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        r = this;
                        return [4 /*yield*/, fetch(t).then(function (t) { return t.json(); }).catch(function (t) { console.log("config fetch error:", t.message); })];
                    case 1:
                        n = _a.sent();
                        return [2 /*return*/, (e ? this.extend(n) : Object.keys(n).forEach(function (t, e) { r.set(t, n[t]); }), n)];
                }
            }); });
        };
        Config.prototype.extend = function (t, e) {
            if (e === void 0) { e = this._config; }
            __chunk_2.Util.deepAssign(e, t);
        };
        Config.prototype.all = function () { return this._config; };
        Config.prototype.getLocalSettings = function (t) { "undefined" != typeof window && window.localStorage && window.localStorage.getItem(t) && this.add(__chunk_3.Storage.restoreLocalModel(t)); };
        Config.prototype.setLocalSettings = function (t, e) {
            if (e === void 0) { e = this._config; }
            "undefined" != typeof window && window.localStorage && __chunk_3.Storage.saveLocalModel(e, t);
        };
        Config.prototype.setDefaults = function (t) { var e = this; e._defaults = t, Object.keys(t).forEach(function (r) { e.set(r, __chunk_2.Util.copy(t[r])); }); };
        Config.prototype.restoreDefaults = function () { var t = this; Object.keys(t._defaults).forEach(function (e) { t.set(e, t._defaults[e]); }); };
        Config.prototype.islocked = function (t) { this._locked.forEach(function (e) { if (e === t)
            return console.log('Config error: property "' + e + '" is protected'), !0; }); };
        Config.prototype.lock = function (t) { return !1; };
        Config.prototype.unLock = function (t) { };
        Config.prototype.initXdomain = function (t) { "undefined" != typeof window && window.xdomain && (window.xdomain.masters(t.masters), window.xdomain.slaves(t.slaves)); };
        return Config;
    }());  var singleton = Symbol(), singletonEnforcer = Symbol();
    var ConfigService = /** @class */ (function (_super) {
        __extends(ConfigService, _super);
        function ConfigService(t) {
            var _this = this;
            if (_this = _super.call(this) || this, t !== singletonEnforcer)
                throw new Error("Cannot construct singleton");
            _this._type = "ConfigService";
            return _this;
        }
        Object.defineProperty(ConfigService, "instance", {
            get: function () { return this[singleton] || (this[singleton] = new ConfigService(singletonEnforcer)), this[singleton]; },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(ConfigService.prototype, "type", {
            get: function () { return this._type; },
            set: function (t) { this._type = t; },
            enumerable: true,
            configurable: true
        });
        return ConfigService;
    }(Config));  var lookup = [], revLookup = [], Arr = "undefined" != typeof Uint8Array ? Uint8Array : Array, inited = !1; function init() { inited = !0; for (var t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", e = 0, r = t.length; e < r; ++e)
    lookup[e] = t[e], revLookup[t.charCodeAt(e)] = e; revLookup["-".charCodeAt(0)] = 62, revLookup["_".charCodeAt(0)] = 63; } function toByteArray(t) { var e, r, n, i, o, s; inited || init(); var a = t.length; if (a % 4 > 0)
    throw new Error("Invalid string. Length must be a multiple of 4"); o = "=" === t[a - 2] ? 2 : "=" === t[a - 1] ? 1 : 0, s = new Arr(3 * a / 4 - o), n = o > 0 ? a - 4 : a; var u = 0; for (e = 0, r = 0; e < n; e += 4, r += 3)
    i = revLookup[t.charCodeAt(e)] << 18 | revLookup[t.charCodeAt(e + 1)] << 12 | revLookup[t.charCodeAt(e + 2)] << 6 | revLookup[t.charCodeAt(e + 3)], s[u++] = i >> 16 & 255, s[u++] = i >> 8 & 255, s[u++] = 255 & i; return 2 === o ? (i = revLookup[t.charCodeAt(e)] << 2 | revLookup[t.charCodeAt(e + 1)] >> 4, s[u++] = 255 & i) : 1 === o && (i = revLookup[t.charCodeAt(e)] << 10 | revLookup[t.charCodeAt(e + 1)] << 4 | revLookup[t.charCodeAt(e + 2)] >> 2, s[u++] = i >> 8 & 255, s[u++] = 255 & i), s; } function tripletToBase64(t) { return lookup[t >> 18 & 63] + lookup[t >> 12 & 63] + lookup[t >> 6 & 63] + lookup[63 & t]; } function encodeChunk(t, e, r) { for (var n, i = [], o = e; o < r; o += 3)
    n = (t[o] << 16) + (t[o + 1] << 8) + t[o + 2], i.push(tripletToBase64(n)); return i.join(""); } function fromByteArray(t) { var e; inited || init(); for (var r = t.length, n = r % 3, i = "", o = [], s = 0, a = r - n; s < a; s += 16383)
    o.push(encodeChunk(t, s, s + 16383 > a ? a : s + 16383)); return 1 === n ? (e = t[r - 1], i += lookup[e >> 2], i += lookup[e << 4 & 63], i += "==") : 2 === n && (e = (t[r - 2] << 8) + t[r - 1], i += lookup[e >> 10], i += lookup[e >> 4 & 63], i += lookup[e << 2 & 63], i += "="), o.push(i), o.join(""); } function read(t, e, r, n, i) { var o, s, a = 8 * i - n - 1, u = (1 << a) - 1, l = u >> 1, f = -7, h = r ? i - 1 : 0, c = r ? -1 : 1, p = t[e + h]; for (h += c, o = p & (1 << -f) - 1, p >>= -f, f += a; f > 0; o = 256 * o + t[e + h], h += c, f -= 8)
    ; for (s = o & (1 << -f) - 1, o >>= -f, f += n; f > 0; s = 256 * s + t[e + h], h += c, f -= 8)
    ; if (0 === o)
    o = 1 - l;
else {
    if (o === u)
        return s ? NaN : 1 / 0 * (p ? -1 : 1);
    s += Math.pow(2, n), o -= l;
} return (p ? -1 : 1) * s * Math.pow(2, o - n); } function write(t, e, r, n, i, o) { var s, a, u, l = 8 * o - i - 1, f = (1 << l) - 1, h = f >> 1, c = 23 === i ? Math.pow(2, -24) - Math.pow(2, -77) : 0, p = n ? 0 : o - 1, d = n ? 1 : -1, y = e < 0 || 0 === e && 1 / e < 0 ? 1 : 0; for (e = Math.abs(e), isNaN(e) || e === 1 / 0 ? (a = isNaN(e) ? 1 : 0, s = f) : (s = Math.floor(Math.log(e) / Math.LN2), e * (u = Math.pow(2, -s)) < 1 && (s--, u *= 2), (e += s + h >= 1 ? c / u : c * Math.pow(2, 1 - h)) * u >= 2 && (s++, u /= 2), s + h >= f ? (a = 0, s = f) : s + h >= 1 ? (a = (e * u - 1) * Math.pow(2, i), s += h) : (a = e * Math.pow(2, h - 1) * Math.pow(2, i), s = 0)); i >= 8; t[r + p] = 255 & a, p += d, a /= 256, i -= 8)
    ; for (s = s << i | a, l += i; l > 0; t[r + p] = 255 & s, p += d, s /= 256, l -= 8)
    ; t[r + p - d] |= 128 * y; } var toString = {}.toString, isArray = Array.isArray || function (t) { return "[object Array]" == toString.call(t); }, INSPECT_MAX_BYTES = 50; function kMaxLength() { return Buffer.TYPED_ARRAY_SUPPORT ? 2147483647 : 1073741823; } function createBuffer(t, e) { if (kMaxLength() < e)
    throw new RangeError("Invalid typed array length"); return Buffer.TYPED_ARRAY_SUPPORT ? (t = new Uint8Array(e)).__proto__ = Buffer.prototype : (null === t && (t = new Buffer(e)), t.length = e), t; } function Buffer(t, e, r) { if (!(Buffer.TYPED_ARRAY_SUPPORT || this instanceof Buffer))
    return new Buffer(t, e, r); if ("number" == typeof t) {
    if ("string" == typeof e)
        throw new Error("If encoding is specified then the first argument must be a string");
    return allocUnsafe(this, t);
} return from(this, t, e, r); } function from(t, e, r, n) { if ("number" == typeof e)
    throw new TypeError('"value" argument must not be a number'); return "undefined" != typeof ArrayBuffer && e instanceof ArrayBuffer ? fromArrayBuffer(t, e, r, n) : "string" == typeof e ? fromString(t, e, r) : fromObject(t, e); } function assertSize(t) { if ("number" != typeof t)
    throw new TypeError('"size" argument must be a number'); if (t < 0)
    throw new RangeError('"size" argument must not be negative'); } function alloc(t, e, r, n) { return assertSize(e), e <= 0 ? createBuffer(t, e) : void 0 !== r ? "string" == typeof n ? createBuffer(t, e).fill(r, n) : createBuffer(t, e).fill(r) : createBuffer(t, e); } function allocUnsafe(t, e) { if (assertSize(e), t = createBuffer(t, e < 0 ? 0 : 0 | checked(e)), !Buffer.TYPED_ARRAY_SUPPORT)
    for (var r = 0; r < e; ++r)
        t[r] = 0; return t; } function fromString(t, e, r) { if ("string" == typeof r && "" !== r || (r = "utf8"), !Buffer.isEncoding(r))
    throw new TypeError('"encoding" must be a valid string encoding'); var n = 0 | byteLength(e, r), i = (t = createBuffer(t, n)).write(e, r); return i !== n && (t = t.slice(0, i)), t; } function fromArrayLike(t, e) { var r = e.length < 0 ? 0 : 0 | checked(e.length); t = createBuffer(t, r); for (var n = 0; n < r; n += 1)
    t[n] = 255 & e[n]; return t; } function fromArrayBuffer(t, e, r, n) { if (e.byteLength, r < 0 || e.byteLength < r)
    throw new RangeError("'offset' is out of bounds"); if (e.byteLength < r + (n || 0))
    throw new RangeError("'length' is out of bounds"); return e = void 0 === r && void 0 === n ? new Uint8Array(e) : void 0 === n ? new Uint8Array(e, r) : new Uint8Array(e, r, n), Buffer.TYPED_ARRAY_SUPPORT ? (t = e).__proto__ = Buffer.prototype : t = fromArrayLike(t, e), t; } function fromObject(t, e) { if (internalIsBuffer(e)) {
    var r = 0 | checked(e.length);
    return 0 === (t = createBuffer(t, r)).length ? t : (e.copy(t, 0, 0, r), t);
} if (e) {
    if ("undefined" != typeof ArrayBuffer && e.buffer instanceof ArrayBuffer || "length" in e)
        return "number" != typeof e.length || isnan(e.length) ? createBuffer(t, 0) : fromArrayLike(t, e);
    if ("Buffer" === e.type && isArray(e.data))
        return fromArrayLike(t, e.data);
} throw new TypeError("First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object."); } function checked(t) { if (t >= kMaxLength())
    throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x" + kMaxLength().toString(16) + " bytes"); return 0 | t; } function internalIsBuffer(t) { return !(null == t || !t._isBuffer); } function byteLength(t, e) { if (internalIsBuffer(t))
    return t.length; if ("undefined" != typeof ArrayBuffer && "function" == typeof ArrayBuffer.isView && (ArrayBuffer.isView(t) || t instanceof ArrayBuffer))
    return t.byteLength; "string" != typeof t && (t = "" + t); var r = t.length; if (0 === r)
    return 0; for (var n = !1;;)
    switch (e) {
        case "ascii":
        case "latin1":
        case "binary": return r;
        case "utf8":
        case "utf-8":
        case void 0: return utf8ToBytes(t).length;
        case "ucs2":
        case "ucs-2":
        case "utf16le":
        case "utf-16le": return 2 * r;
        case "hex": return r >>> 1;
        case "base64": return base64ToBytes(t).length;
        default:
            if (n)
                return utf8ToBytes(t).length;
            e = ("" + e).toLowerCase(), n = !0;
    } } function slowToString(t, e, r) { var n = !1; if ((void 0 === e || e < 0) && (e = 0), e > this.length)
    return ""; if ((void 0 === r || r > this.length) && (r = this.length), r <= 0)
    return ""; if ((r >>>= 0) <= (e >>>= 0))
    return ""; for (t || (t = "utf8");;)
    switch (t) {
        case "hex": return hexSlice(this, e, r);
        case "utf8":
        case "utf-8": return utf8Slice(this, e, r);
        case "ascii": return asciiSlice(this, e, r);
        case "latin1":
        case "binary": return latin1Slice(this, e, r);
        case "base64": return base64Slice(this, e, r);
        case "ucs2":
        case "ucs-2":
        case "utf16le":
        case "utf-16le": return utf16leSlice(this, e, r);
        default:
            if (n)
                throw new TypeError("Unknown encoding: " + t);
            t = (t + "").toLowerCase(), n = !0;
    } } function swap(t, e, r) { var n = t[e]; t[e] = t[r], t[r] = n; } function bidirectionalIndexOf(t, e, r, n, i) { if (0 === t.length)
    return -1; if ("string" == typeof r ? (n = r, r = 0) : r > 2147483647 ? r = 2147483647 : r < -2147483648 && (r = -2147483648), r = +r, isNaN(r) && (r = i ? 0 : t.length - 1), r < 0 && (r = t.length + r), r >= t.length) {
    if (i)
        return -1;
    r = t.length - 1;
}
else if (r < 0) {
    if (!i)
        return -1;
    r = 0;
} if ("string" == typeof e && (e = Buffer.from(e, n)), internalIsBuffer(e))
    return 0 === e.length ? -1 : arrayIndexOf(t, e, r, n, i); if ("number" == typeof e)
    return e &= 255, Buffer.TYPED_ARRAY_SUPPORT && "function" == typeof Uint8Array.prototype.indexOf ? i ? Uint8Array.prototype.indexOf.call(t, e, r) : Uint8Array.prototype.lastIndexOf.call(t, e, r) : arrayIndexOf(t, [e], r, n, i); throw new TypeError("val must be string, number or Buffer"); } function arrayIndexOf(t, e, r, n, i) { var o, s = 1, a = t.length, u = e.length; if (void 0 !== n && ("ucs2" === (n = String(n).toLowerCase()) || "ucs-2" === n || "utf16le" === n || "utf-16le" === n)) {
    if (t.length < 2 || e.length < 2)
        return -1;
    s = 2, a /= 2, u /= 2, r /= 2;
} function l(t, e) { return 1 === s ? t[e] : t.readUInt16BE(e * s); } if (i) {
    var f = -1;
    for (o = r; o < a; o++)
        if (l(t, o) === l(e, -1 === f ? 0 : o - f)) {
            if (-1 === f && (f = o), o - f + 1 === u)
                return f * s;
        }
        else
            -1 !== f && (o -= o - f), f = -1;
}
else
    for (r + u > a && (r = a - u), o = r; o >= 0; o--) {
        for (var h = !0, c = 0; c < u; c++)
            if (l(t, o + c) !== l(e, c)) {
                h = !1;
                break;
            }
        if (h)
            return o;
    } return -1; } function hexWrite(t, e, r, n) { r = Number(r) || 0; var i = t.length - r; n ? (n = Number(n)) > i && (n = i) : n = i; var o = e.length; if (o % 2 != 0)
    throw new TypeError("Invalid hex string"); n > o / 2 && (n = o / 2); for (var s = 0; s < n; ++s) {
    var a = parseInt(e.substr(2 * s, 2), 16);
    if (isNaN(a))
        return s;
    t[r + s] = a;
} return s; } function utf8Write(t, e, r, n) { return blitBuffer(utf8ToBytes(e, t.length - r), t, r, n); } function asciiWrite(t, e, r, n) { return blitBuffer(asciiToBytes(e), t, r, n); } function latin1Write(t, e, r, n) { return asciiWrite(t, e, r, n); } function base64Write(t, e, r, n) { return blitBuffer(base64ToBytes(e), t, r, n); } function ucs2Write(t, e, r, n) { return blitBuffer(utf16leToBytes(e, t.length - r), t, r, n); } function base64Slice(t, e, r) { return 0 === e && r === t.length ? fromByteArray(t) : fromByteArray(t.slice(e, r)); } function utf8Slice(t, e, r) { r = Math.min(t.length, r); for (var n = [], i = e; i < r;) {
    var o, s, a, u, l = t[i], f = null, h = l > 239 ? 4 : l > 223 ? 3 : l > 191 ? 2 : 1;
    if (i + h <= r)
        switch (h) {
            case 1:
                l < 128 && (f = l);
                break;
            case 2:
                128 == (192 & (o = t[i + 1])) && (u = (31 & l) << 6 | 63 & o) > 127 && (f = u);
                break;
            case 3:
                o = t[i + 1], s = t[i + 2], 128 == (192 & o) && 128 == (192 & s) && (u = (15 & l) << 12 | (63 & o) << 6 | 63 & s) > 2047 && (u < 55296 || u > 57343) && (f = u);
                break;
            case 4: o = t[i + 1], s = t[i + 2], a = t[i + 3], 128 == (192 & o) && 128 == (192 & s) && 128 == (192 & a) && (u = (15 & l) << 18 | (63 & o) << 12 | (63 & s) << 6 | 63 & a) > 65535 && u < 1114112 && (f = u);
        }
    null === f ? (f = 65533, h = 1) : f > 65535 && (f -= 65536, n.push(f >>> 10 & 1023 | 55296), f = 56320 | 1023 & f), n.push(f), i += h;
} return decodeCodePointsArray(n); } Buffer.TYPED_ARRAY_SUPPORT = void 0 === __chunk_2.global.TYPED_ARRAY_SUPPORT || __chunk_2.global.TYPED_ARRAY_SUPPORT, Buffer.poolSize = 8192, Buffer._augment = function (t) { return t.__proto__ = Buffer.prototype, t; }, Buffer.from = function (t, e, r) { return from(null, t, e, r); }, Buffer.TYPED_ARRAY_SUPPORT && (Buffer.prototype.__proto__ = Uint8Array.prototype, Buffer.__proto__ = Uint8Array), Buffer.alloc = function (t, e, r) { return alloc(null, t, e, r); }, Buffer.allocUnsafe = function (t) { return allocUnsafe(null, t); }, Buffer.allocUnsafeSlow = function (t) { return allocUnsafe(null, t); }, Buffer.isBuffer = isBuffer, Buffer.compare = function (t, e) { if (!internalIsBuffer(t) || !internalIsBuffer(e))
    throw new TypeError("Arguments must be Buffers"); if (t === e)
    return 0; for (var r = t.length, n = e.length, i = 0, o = Math.min(r, n); i < o; ++i)
    if (t[i] !== e[i]) {
        r = t[i], n = e[i];
        break;
    } return r < n ? -1 : n < r ? 1 : 0; }, Buffer.isEncoding = function (t) { switch (String(t).toLowerCase()) {
    case "hex":
    case "utf8":
    case "utf-8":
    case "ascii":
    case "latin1":
    case "binary":
    case "base64":
    case "ucs2":
    case "ucs-2":
    case "utf16le":
    case "utf-16le": return !0;
    default: return !1;
} }, Buffer.concat = function (t, e) { if (!isArray(t))
    throw new TypeError('"list" argument must be an Array of Buffers'); if (0 === t.length)
    return Buffer.alloc(0); var r; if (void 0 === e)
    for (e = 0, r = 0; r < t.length; ++r)
        e += t[r].length; var n = Buffer.allocUnsafe(e), i = 0; for (r = 0; r < t.length; ++r) {
    var o = t[r];
    if (!internalIsBuffer(o))
        throw new TypeError('"list" argument must be an Array of Buffers');
    o.copy(n, i), i += o.length;
} return n; }, Buffer.byteLength = byteLength, Buffer.prototype._isBuffer = !0, Buffer.prototype.swap16 = function () { var t = this.length; if (t % 2 != 0)
    throw new RangeError("Buffer size must be a multiple of 16-bits"); for (var e = 0; e < t; e += 2)
    swap(this, e, e + 1); return this; }, Buffer.prototype.swap32 = function () { var t = this.length; if (t % 4 != 0)
    throw new RangeError("Buffer size must be a multiple of 32-bits"); for (var e = 0; e < t; e += 4)
    swap(this, e, e + 3), swap(this, e + 1, e + 2); return this; }, Buffer.prototype.swap64 = function () { var t = this.length; if (t % 8 != 0)
    throw new RangeError("Buffer size must be a multiple of 64-bits"); for (var e = 0; e < t; e += 8)
    swap(this, e, e + 7), swap(this, e + 1, e + 6), swap(this, e + 2, e + 5), swap(this, e + 3, e + 4); return this; }, Buffer.prototype.toString = function () { var t = 0 | this.length; return 0 === t ? "" : 0 === arguments.length ? utf8Slice(this, 0, t) : slowToString.apply(this, arguments); }, Buffer.prototype.equals = function (t) { if (!internalIsBuffer(t))
    throw new TypeError("Argument must be a Buffer"); return this === t || 0 === Buffer.compare(this, t); }, Buffer.prototype.inspect = function () { var t = "", e = INSPECT_MAX_BYTES; return this.length > 0 && (t = this.toString("hex", 0, e).match(/.{2}/g).join(" "), this.length > e && (t += " ... ")), "<Buffer " + t + ">"; }, Buffer.prototype.compare = function (t, e, r, n, i) { if (!internalIsBuffer(t))
    throw new TypeError("Argument must be a Buffer"); if (void 0 === e && (e = 0), void 0 === r && (r = t ? t.length : 0), void 0 === n && (n = 0), void 0 === i && (i = this.length), e < 0 || r > t.length || n < 0 || i > this.length)
    throw new RangeError("out of range index"); if (n >= i && e >= r)
    return 0; if (n >= i)
    return -1; if (e >= r)
    return 1; if (e >>>= 0, r >>>= 0, n >>>= 0, i >>>= 0, this === t)
    return 0; for (var o = i - n, s = r - e, a = Math.min(o, s), u = this.slice(n, i), l = t.slice(e, r), f = 0; f < a; ++f)
    if (u[f] !== l[f]) {
        o = u[f], s = l[f];
        break;
    } return o < s ? -1 : s < o ? 1 : 0; }, Buffer.prototype.includes = function (t, e, r) { return -1 !== this.indexOf(t, e, r); }, Buffer.prototype.indexOf = function (t, e, r) { return bidirectionalIndexOf(this, t, e, r, !0); }, Buffer.prototype.lastIndexOf = function (t, e, r) { return bidirectionalIndexOf(this, t, e, r, !1); }, Buffer.prototype.write = function (t, e, r, n) { if (void 0 === e)
    n = "utf8", r = this.length, e = 0;
else if (void 0 === r && "string" == typeof e)
    n = e, r = this.length, e = 0;
else {
    if (!isFinite(e))
        throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported");
    e |= 0, isFinite(r) ? (r |= 0, void 0 === n && (n = "utf8")) : (n = r, r = void 0);
} var i = this.length - e; if ((void 0 === r || r > i) && (r = i), t.length > 0 && (r < 0 || e < 0) || e > this.length)
    throw new RangeError("Attempt to write outside buffer bounds"); n || (n = "utf8"); for (var o = !1;;)
    switch (n) {
        case "hex": return hexWrite(this, t, e, r);
        case "utf8":
        case "utf-8": return utf8Write(this, t, e, r);
        case "ascii": return asciiWrite(this, t, e, r);
        case "latin1":
        case "binary": return latin1Write(this, t, e, r);
        case "base64": return base64Write(this, t, e, r);
        case "ucs2":
        case "ucs-2":
        case "utf16le":
        case "utf-16le": return ucs2Write(this, t, e, r);
        default:
            if (o)
                throw new TypeError("Unknown encoding: " + n);
            n = ("" + n).toLowerCase(), o = !0;
    } }, Buffer.prototype.toJSON = function () { return { type: "Buffer", data: Array.prototype.slice.call(this._arr || this, 0) }; }; var MAX_ARGUMENTS_LENGTH = 4096; function decodeCodePointsArray(t) { var e = t.length; if (e <= MAX_ARGUMENTS_LENGTH)
    return String.fromCharCode.apply(String, t); for (var r = "", n = 0; n < e;)
    r += String.fromCharCode.apply(String, t.slice(n, n += MAX_ARGUMENTS_LENGTH)); return r; } function asciiSlice(t, e, r) { var n = ""; r = Math.min(t.length, r); for (var i = e; i < r; ++i)
    n += String.fromCharCode(127 & t[i]); return n; } function latin1Slice(t, e, r) { var n = ""; r = Math.min(t.length, r); for (var i = e; i < r; ++i)
    n += String.fromCharCode(t[i]); return n; } function hexSlice(t, e, r) { var n = t.length; (!e || e < 0) && (e = 0), (!r || r < 0 || r > n) && (r = n); for (var i = "", o = e; o < r; ++o)
    i += toHex(t[o]); return i; } function utf16leSlice(t, e, r) { for (var n = t.slice(e, r), i = "", o = 0; o < n.length; o += 2)
    i += String.fromCharCode(n[o] + 256 * n[o + 1]); return i; } function checkOffset(t, e, r) { if (t % 1 != 0 || t < 0)
    throw new RangeError("offset is not uint"); if (t + e > r)
    throw new RangeError("Trying to access beyond buffer length"); } function checkInt(t, e, r, n, i, o) { if (!internalIsBuffer(t))
    throw new TypeError('"buffer" argument must be a Buffer instance'); if (e > i || e < o)
    throw new RangeError('"value" argument is out of bounds'); if (r + n > t.length)
    throw new RangeError("Index out of range"); } function objectWriteUInt16(t, e, r, n) { e < 0 && (e = 65535 + e + 1); for (var i = 0, o = Math.min(t.length - r, 2); i < o; ++i)
    t[r + i] = (e & 255 << 8 * (n ? i : 1 - i)) >>> 8 * (n ? i : 1 - i); } function objectWriteUInt32(t, e, r, n) { e < 0 && (e = 4294967295 + e + 1); for (var i = 0, o = Math.min(t.length - r, 4); i < o; ++i)
    t[r + i] = e >>> 8 * (n ? i : 3 - i) & 255; } function checkIEEE754(t, e, r, n, i, o) { if (r + n > t.length)
    throw new RangeError("Index out of range"); if (r < 0)
    throw new RangeError("Index out of range"); } function writeFloat(t, e, r, n, i) { return i || checkIEEE754(t, e, r, 4, 3.4028234663852886e38, -3.4028234663852886e38), write(t, e, r, n, 23, 4), r + 4; } function writeDouble(t, e, r, n, i) { return i || checkIEEE754(t, e, r, 8, 1.7976931348623157e308, -1.7976931348623157e308), write(t, e, r, n, 52, 8), r + 8; } Buffer.prototype.slice = function (t, e) { var r, n = this.length; if (t = ~~t, e = void 0 === e ? n : ~~e, t < 0 ? (t += n) < 0 && (t = 0) : t > n && (t = n), e < 0 ? (e += n) < 0 && (e = 0) : e > n && (e = n), e < t && (e = t), Buffer.TYPED_ARRAY_SUPPORT)
    (r = this.subarray(t, e)).__proto__ = Buffer.prototype;
else {
    var i = e - t;
    r = new Buffer(i, void 0);
    for (var o = 0; o < i; ++o)
        r[o] = this[o + t];
} return r; }, Buffer.prototype.readUIntLE = function (t, e, r) { t |= 0, e |= 0, r || checkOffset(t, e, this.length); for (var n = this[t], i = 1, o = 0; ++o < e && (i *= 256);)
    n += this[t + o] * i; return n; }, Buffer.prototype.readUIntBE = function (t, e, r) { t |= 0, e |= 0, r || checkOffset(t, e, this.length); for (var n = this[t + --e], i = 1; e > 0 && (i *= 256);)
    n += this[t + --e] * i; return n; }, Buffer.prototype.readUInt8 = function (t, e) { return e || checkOffset(t, 1, this.length), this[t]; }, Buffer.prototype.readUInt16LE = function (t, e) { return e || checkOffset(t, 2, this.length), this[t] | this[t + 1] << 8; }, Buffer.prototype.readUInt16BE = function (t, e) { return e || checkOffset(t, 2, this.length), this[t] << 8 | this[t + 1]; }, Buffer.prototype.readUInt32LE = function (t, e) { return e || checkOffset(t, 4, this.length), (this[t] | this[t + 1] << 8 | this[t + 2] << 16) + 16777216 * this[t + 3]; }, Buffer.prototype.readUInt32BE = function (t, e) { return e || checkOffset(t, 4, this.length), 16777216 * this[t] + (this[t + 1] << 16 | this[t + 2] << 8 | this[t + 3]); }, Buffer.prototype.readIntLE = function (t, e, r) { t |= 0, e |= 0, r || checkOffset(t, e, this.length); for (var n = this[t], i = 1, o = 0; ++o < e && (i *= 256);)
    n += this[t + o] * i; return n >= (i *= 128) && (n -= Math.pow(2, 8 * e)), n; }, Buffer.prototype.readIntBE = function (t, e, r) { t |= 0, e |= 0, r || checkOffset(t, e, this.length); for (var n = e, i = 1, o = this[t + --n]; n > 0 && (i *= 256);)
    o += this[t + --n] * i; return o >= (i *= 128) && (o -= Math.pow(2, 8 * e)), o; }, Buffer.prototype.readInt8 = function (t, e) { return e || checkOffset(t, 1, this.length), 128 & this[t] ? -1 * (255 - this[t] + 1) : this[t]; }, Buffer.prototype.readInt16LE = function (t, e) { e || checkOffset(t, 2, this.length); var r = this[t] | this[t + 1] << 8; return 32768 & r ? 4294901760 | r : r; }, Buffer.prototype.readInt16BE = function (t, e) { e || checkOffset(t, 2, this.length); var r = this[t + 1] | this[t] << 8; return 32768 & r ? 4294901760 | r : r; }, Buffer.prototype.readInt32LE = function (t, e) { return e || checkOffset(t, 4, this.length), this[t] | this[t + 1] << 8 | this[t + 2] << 16 | this[t + 3] << 24; }, Buffer.prototype.readInt32BE = function (t, e) { return e || checkOffset(t, 4, this.length), this[t] << 24 | this[t + 1] << 16 | this[t + 2] << 8 | this[t + 3]; }, Buffer.prototype.readFloatLE = function (t, e) { return e || checkOffset(t, 4, this.length), read(this, t, !0, 23, 4); }, Buffer.prototype.readFloatBE = function (t, e) { return e || checkOffset(t, 4, this.length), read(this, t, !1, 23, 4); }, Buffer.prototype.readDoubleLE = function (t, e) { return e || checkOffset(t, 8, this.length), read(this, t, !0, 52, 8); }, Buffer.prototype.readDoubleBE = function (t, e) { return e || checkOffset(t, 8, this.length), read(this, t, !1, 52, 8); }, Buffer.prototype.writeUIntLE = function (t, e, r, n) { t = +t, e |= 0, r |= 0, n || checkInt(this, t, e, r, Math.pow(2, 8 * r) - 1, 0); var i = 1, o = 0; for (this[e] = 255 & t; ++o < r && (i *= 256);)
    this[e + o] = t / i & 255; return e + r; }, Buffer.prototype.writeUIntBE = function (t, e, r, n) { t = +t, e |= 0, r |= 0, n || checkInt(this, t, e, r, Math.pow(2, 8 * r) - 1, 0); var i = r - 1, o = 1; for (this[e + i] = 255 & t; --i >= 0 && (o *= 256);)
    this[e + i] = t / o & 255; return e + r; }, Buffer.prototype.writeUInt8 = function (t, e, r) { return t = +t, e |= 0, r || checkInt(this, t, e, 1, 255, 0), Buffer.TYPED_ARRAY_SUPPORT || (t = Math.floor(t)), this[e] = 255 & t, e + 1; }, Buffer.prototype.writeUInt16LE = function (t, e, r) { return t = +t, e |= 0, r || checkInt(this, t, e, 2, 65535, 0), Buffer.TYPED_ARRAY_SUPPORT ? (this[e] = 255 & t, this[e + 1] = t >>> 8) : objectWriteUInt16(this, t, e, !0), e + 2; }, Buffer.prototype.writeUInt16BE = function (t, e, r) { return t = +t, e |= 0, r || checkInt(this, t, e, 2, 65535, 0), Buffer.TYPED_ARRAY_SUPPORT ? (this[e] = t >>> 8, this[e + 1] = 255 & t) : objectWriteUInt16(this, t, e, !1), e + 2; }, Buffer.prototype.writeUInt32LE = function (t, e, r) { return t = +t, e |= 0, r || checkInt(this, t, e, 4, 4294967295, 0), Buffer.TYPED_ARRAY_SUPPORT ? (this[e + 3] = t >>> 24, this[e + 2] = t >>> 16, this[e + 1] = t >>> 8, this[e] = 255 & t) : objectWriteUInt32(this, t, e, !0), e + 4; }, Buffer.prototype.writeUInt32BE = function (t, e, r) { return t = +t, e |= 0, r || checkInt(this, t, e, 4, 4294967295, 0), Buffer.TYPED_ARRAY_SUPPORT ? (this[e] = t >>> 24, this[e + 1] = t >>> 16, this[e + 2] = t >>> 8, this[e + 3] = 255 & t) : objectWriteUInt32(this, t, e, !1), e + 4; }, Buffer.prototype.writeIntLE = function (t, e, r, n) { if (t = +t, e |= 0, !n) {
    var i = Math.pow(2, 8 * r - 1);
    checkInt(this, t, e, r, i - 1, -i);
} var o = 0, s = 1, a = 0; for (this[e] = 255 & t; ++o < r && (s *= 256);)
    t < 0 && 0 === a && 0 !== this[e + o - 1] && (a = 1), this[e + o] = (t / s >> 0) - a & 255; return e + r; }, Buffer.prototype.writeIntBE = function (t, e, r, n) { if (t = +t, e |= 0, !n) {
    var i = Math.pow(2, 8 * r - 1);
    checkInt(this, t, e, r, i - 1, -i);
} var o = r - 1, s = 1, a = 0; for (this[e + o] = 255 & t; --o >= 0 && (s *= 256);)
    t < 0 && 0 === a && 0 !== this[e + o + 1] && (a = 1), this[e + o] = (t / s >> 0) - a & 255; return e + r; }, Buffer.prototype.writeInt8 = function (t, e, r) { return t = +t, e |= 0, r || checkInt(this, t, e, 1, 127, -128), Buffer.TYPED_ARRAY_SUPPORT || (t = Math.floor(t)), t < 0 && (t = 255 + t + 1), this[e] = 255 & t, e + 1; }, Buffer.prototype.writeInt16LE = function (t, e, r) { return t = +t, e |= 0, r || checkInt(this, t, e, 2, 32767, -32768), Buffer.TYPED_ARRAY_SUPPORT ? (this[e] = 255 & t, this[e + 1] = t >>> 8) : objectWriteUInt16(this, t, e, !0), e + 2; }, Buffer.prototype.writeInt16BE = function (t, e, r) { return t = +t, e |= 0, r || checkInt(this, t, e, 2, 32767, -32768), Buffer.TYPED_ARRAY_SUPPORT ? (this[e] = t >>> 8, this[e + 1] = 255 & t) : objectWriteUInt16(this, t, e, !1), e + 2; }, Buffer.prototype.writeInt32LE = function (t, e, r) { return t = +t, e |= 0, r || checkInt(this, t, e, 4, 2147483647, -2147483648), Buffer.TYPED_ARRAY_SUPPORT ? (this[e] = 255 & t, this[e + 1] = t >>> 8, this[e + 2] = t >>> 16, this[e + 3] = t >>> 24) : objectWriteUInt32(this, t, e, !0), e + 4; }, Buffer.prototype.writeInt32BE = function (t, e, r) { return t = +t, e |= 0, r || checkInt(this, t, e, 4, 2147483647, -2147483648), t < 0 && (t = 4294967295 + t + 1), Buffer.TYPED_ARRAY_SUPPORT ? (this[e] = t >>> 24, this[e + 1] = t >>> 16, this[e + 2] = t >>> 8, this[e + 3] = 255 & t) : objectWriteUInt32(this, t, e, !1), e + 4; }, Buffer.prototype.writeFloatLE = function (t, e, r) { return writeFloat(this, t, e, !0, r); }, Buffer.prototype.writeFloatBE = function (t, e, r) { return writeFloat(this, t, e, !1, r); }, Buffer.prototype.writeDoubleLE = function (t, e, r) { return writeDouble(this, t, e, !0, r); }, Buffer.prototype.writeDoubleBE = function (t, e, r) { return writeDouble(this, t, e, !1, r); }, Buffer.prototype.copy = function (t, e, r, n) { if (r || (r = 0), n || 0 === n || (n = this.length), e >= t.length && (e = t.length), e || (e = 0), n > 0 && n < r && (n = r), n === r)
    return 0; if (0 === t.length || 0 === this.length)
    return 0; if (e < 0)
    throw new RangeError("targetStart out of bounds"); if (r < 0 || r >= this.length)
    throw new RangeError("sourceStart out of bounds"); if (n < 0)
    throw new RangeError("sourceEnd out of bounds"); n > this.length && (n = this.length), t.length - e < n - r && (n = t.length - e + r); var i, o = n - r; if (this === t && r < e && e < n)
    for (i = o - 1; i >= 0; --i)
        t[i + e] = this[i + r];
else if (o < 1e3 || !Buffer.TYPED_ARRAY_SUPPORT)
    for (i = 0; i < o; ++i)
        t[i + e] = this[i + r];
else
    Uint8Array.prototype.set.call(t, this.subarray(r, r + o), e); return o; }, Buffer.prototype.fill = function (t, e, r, n) { if ("string" == typeof t) {
    if ("string" == typeof e ? (n = e, e = 0, r = this.length) : "string" == typeof r && (n = r, r = this.length), 1 === t.length) {
        var i = t.charCodeAt(0);
        i < 256 && (t = i);
    }
    if (void 0 !== n && "string" != typeof n)
        throw new TypeError("encoding must be a string");
    if ("string" == typeof n && !Buffer.isEncoding(n))
        throw new TypeError("Unknown encoding: " + n);
}
else
    "number" == typeof t && (t &= 255); if (e < 0 || this.length < e || this.length < r)
    throw new RangeError("Out of range index"); if (r <= e)
    return this; var o; if (e >>>= 0, r = void 0 === r ? this.length : r >>> 0, t || (t = 0), "number" == typeof t)
    for (o = e; o < r; ++o)
        this[o] = t;
else {
    var s = internalIsBuffer(t) ? t : utf8ToBytes(new Buffer(t, n).toString()), a = s.length;
    for (o = 0; o < r - e; ++o)
        this[o + e] = s[o % a];
} return this; }; var INVALID_BASE64_RE = /[^+\/0-9A-Za-z-_]/g; function base64clean(t) { if ((t = stringtrim(t).replace(INVALID_BASE64_RE, "")).length < 2)
    return ""; for (; t.length % 4 != 0;)
    t += "="; return t; } function stringtrim(t) { return t.trim ? t.trim() : t.replace(/^\s+|\s+$/g, ""); } function toHex(t) { return t < 16 ? "0" + t.toString(16) : t.toString(16); } function utf8ToBytes(t, e) { var r; e = e || 1 / 0; for (var n = t.length, i = null, o = [], s = 0; s < n; ++s) {
    if ((r = t.charCodeAt(s)) > 55295 && r < 57344) {
        if (!i) {
            if (r > 56319) {
                (e -= 3) > -1 && o.push(239, 191, 189);
                continue;
            }
            if (s + 1 === n) {
                (e -= 3) > -1 && o.push(239, 191, 189);
                continue;
            }
            i = r;
            continue;
        }
        if (r < 56320) {
            (e -= 3) > -1 && o.push(239, 191, 189), i = r;
            continue;
        }
        r = 65536 + (i - 55296 << 10 | r - 56320);
    }
    else
        i && (e -= 3) > -1 && o.push(239, 191, 189);
    if (i = null, r < 128) {
        if ((e -= 1) < 0)
            break;
        o.push(r);
    }
    else if (r < 2048) {
        if ((e -= 2) < 0)
            break;
        o.push(r >> 6 | 192, 63 & r | 128);
    }
    else if (r < 65536) {
        if ((e -= 3) < 0)
            break;
        o.push(r >> 12 | 224, r >> 6 & 63 | 128, 63 & r | 128);
    }
    else {
        if (!(r < 1114112))
            throw new Error("Invalid code point");
        if ((e -= 4) < 0)
            break;
        o.push(r >> 18 | 240, r >> 12 & 63 | 128, r >> 6 & 63 | 128, 63 & r | 128);
    }
} return o; } function asciiToBytes(t) { for (var e = [], r = 0; r < t.length; ++r)
    e.push(255 & t.charCodeAt(r)); return e; } function utf16leToBytes(t, e) { for (var r, n, i, o = [], s = 0; s < t.length && !((e -= 2) < 0); ++s)
    n = (r = t.charCodeAt(s)) >> 8, i = r % 256, o.push(i), o.push(n); return o; } function base64ToBytes(t) { return toByteArray(base64clean(t)); } function blitBuffer(t, e, r, n) { for (var i = 0; i < n && !(i + r >= e.length || i >= t.length); ++i)
    e[i + r] = t[i]; return i; } function isnan(t) { return t != t; } function isBuffer(t) { return null != t && (!!t._isBuffer || isFastBuffer(t) || isSlowBuffer(t)); } function isFastBuffer(t) { return !!t.constructor && "function" == typeof t.constructor.isBuffer && t.constructor.isBuffer(t); } function isSlowBuffer(t) { return "function" == typeof t.readFloatLE && "function" == typeof t.slice && isFastBuffer(t.slice(0, 0)); } var __awaiter$2 = function (t, e, r, n) { return new (r || (r = Promise))(function (i, o) { function s(t) { try {
    u(n.next(t));
}
catch (t) {
    o(t);
} } function a(t) { try {
    u(n.throw(t));
}
catch (t) {
    o(t);
} } function u(t) { t.done ? i(t.value) : new r(function (e) { e(t.value); }).then(s, a); } u((n = n.apply(t, e || [])).next()); }); };
    var DrupalApi = /** @class */ (function (_super) {
        __extends(DrupalApi, _super);
        function DrupalApi() {
            var _this = this;
            _this = _super.call(this) || this, _this.entities = {}, _this.collections = {}, _this.nodes = {}, _this.resources = {}, _this.views = {}, _this.cache = !1, _this.headers = { Accept: "application/vnd.api+json", "Content-Type": "application/vnd.api+json" };
            return _this;
        }
        DrupalApi.prototype.post = function (t, e) { return __awaiter$2(this, void 0, void 0, function () { return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    this.headers.Authorization = "Basic " + jDrupal.settings.auth_hash, this.headers["X-CSRF-Token"] = jDrupal.settings.user_csrf_token, this.headers["Content-Type"] = "application/vnd.api+json";
                    return [4 /*yield*/, fetch("" + this.baseUrl + t, { headers: this.headers, method: "POST", body: JSON.stringify(e) })];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        }); }); };
        DrupalApi.prototype.patch = function (t, e, r) { return __awaiter$2(this, void 0, void 0, function () { return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    this.headers.Authorization = "Basic " + jDrupal.settings.auth_hash, this.headers["X-CSRF-Token"] = jDrupal.settings.user_csrf_token, this.headers["Content-Type"] = "application/vnd.api+json", r.data.attributes.changed && (r.data.attributes.changed = void 0);
                    return [4 /*yield*/, fetch("" + this.baseUrl + t + "/" + e, { headers: this.headers, method: "PATCH", body: JSON.stringify(r) })];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        }); }); };
        DrupalApi.prototype.delete = function (t, e) { return __awaiter$2(this, void 0, void 0, function () { return __generator(this, function (_a) {
            console.log("FIXME drupal delete"), console.log(t, e), this.headers.Authorization = "Basic " + jDrupal.settings.auth_hash, this.headers["X-CSRF-Token"] = jDrupal.settings.user_csrf_token, this.headers["Content-Type"] = "application/vnd.api+json";
            return [2 /*return*/];
        }); }); };
        DrupalApi.prototype.getResourcesIndex = function (t, e) {
            if (t === void 0) { t = "jsonapi"; }
            return __awaiter$2(this, void 0, void 0, function () { var r; return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        r = this;
                        return [4 /*yield*/, new Promise(function (n, i) { var o = r.baseUrl + "index.php/"; e && (o += e + "/"), o += t, fetch(o).then(function (t) { if (t.status >= 400)
                                throw new Error("JsonApi: Bad resources response from server"); return t.json(); }).then(function (t) { r.resources = t, r.baseUrl = t.links.self + "/", Object.keys(t.links).forEach(function (t, e) { (t.match("node--") || t.match("menu--") || t.match("comment--") || t.match("file--") || t.match("taxonomy_term--") || t.match("node_type--")) && (r.collections[t] = {}); }), n(t); }); })];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            }); });
        };
        DrupalApi.prototype.getCollection = function (t, e, r) {
            if (r === void 0) { r = !1; }
            return __awaiter$2(this, void 0, void 0, function () {
                var e, n, _a, _b, t_2, _c, _d, _i, e_1;
                var _this = this;
                return __generator(this, function (_e) {
                    switch (_e.label) {
                        case 0:
                            e = t.split("--")[0] + "/" + t.split("--")[1];
                            _a = this.collections;
                            _b = t;
                            return [4 /*yield*/, this.get(e, {})];
                        case 1:
                            n = _a[_b] = _e.sent();
                            if (!r) return [3 /*break*/, 5];
                            t_2 = [];
                            Object.keys(n.data).forEach(function (e) { t_2.push(_this.getEntityRelationships({ data: n.data[e] })); });
                            _c = [];
                            for (_d in t_2)
                                _c.push(_d);
                            _i = 0;
                            _e.label = 2;
                        case 2:
                            if (!(_i < _c.length)) return [3 /*break*/, 5];
                            e_1 = _c[_i];
                            return [4 /*yield*/, t_2[e_1]];
                        case 3:
                            _e.sent();
                            _e.label = 4;
                        case 4:
                            _i++;
                            return [3 /*break*/, 2];
                        case 5: return [2 /*return*/, (Object.keys(n.data).forEach(function (t, e) { _this.entities[n.data[t].attributes.uuid] = n.data[t]; }), n)];
                    }
                });
            });
        };
        DrupalApi.prototype.getCollections = function (t, e, r) {
            if (r === void 0) { r = !1; }
            return __awaiter$2(this, void 0, void 0, function () {
                var n, _a, _b, _i, t_3;
                var _this = this;
                return __generator(this, function (_c) {
                    switch (_c.label) {
                        case 0:
                            n = [];
                            t.forEach(function (t) { n.push(_this.getCollection(t, e, r)); });
                            _a = [];
                            for (_b in n)
                                _a.push(_b);
                            _i = 0;
                            _c.label = 1;
                        case 1:
                            if (!(_i < _a.length)) return [3 /*break*/, 4];
                            t_3 = _a[_i];
                            return [4 /*yield*/, n[t_3]];
                        case 2:
                            _c.sent();
                            _c.label = 3;
                        case 3:
                            _i++;
                            return [3 /*break*/, 1];
                        case 4: return [2 /*return*/];
                    }
                });
            });
        };
        DrupalApi.prototype.getAllCollections = function (t, e) {
            if (t === void 0) { t = !1; }
            if (e === void 0) { e = []; }
            return __awaiter$2(this, void 0, void 0, function () {
                var r, _a, _b, _i, t_4;
                var _this = this;
                return __generator(this, function (_c) {
                    switch (_c.label) {
                        case 0:
                            r = [];
                            Object.keys(this.collections).forEach(function (n, i) { var o = !1; e.forEach(function (t) { t === n && (o = !0); }), o || r.push(_this.getCollection(n, {}, t)); });
                            _a = [];
                            for (_b in r)
                                _a.push(_b);
                            _i = 0;
                            _c.label = 1;
                        case 1:
                            if (!(_i < _a.length)) return [3 /*break*/, 4];
                            t_4 = _a[_i];
                            return [4 /*yield*/, r[t_4]];
                        case 2:
                            _c.sent();
                            _c.label = 3;
                        case 3:
                            _i++;
                            return [3 /*break*/, 1];
                        case 4: return [2 /*return*/];
                    }
                });
            });
        };
        DrupalApi.prototype.getResource = function (t, e) { return __awaiter$2(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        e && (t += "?", Object.keys(e).forEach(function (r) { return t += r + "=" + e[r] + "&"; }));
                        return [4 /*yield*/, fetch(t, { headers: this.headers }).then(function (t) { if (t.status >= 400)
                                throw new Error("Bad response from server"); return t.json(); }).then(function (t) { return t.data && t.data.id && (_this.entities[t.data.id] = t), t.data && Array.isArray(t.data) && t.data.forEach(function (t) { t.id && (_this.entities[t.id] = t); }), t; })];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        }); };
        DrupalApi.prototype.getResources = function (t) { return __awaiter$2(this, void 0, void 0, function () {
            var e, _a, _b, _i, t_5;
            var _this = this;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        e = [];
                        t.forEach(function (t) { e.push(_this.getResource(t)); });
                        _a = [];
                        for (_b in e)
                            _a.push(_b);
                        _i = 0;
                        _c.label = 1;
                    case 1:
                        if (!(_i < _a.length)) return [3 /*break*/, 4];
                        t_5 = _a[_i];
                        return [4 /*yield*/, e[t_5]];
                    case 2:
                        _c.sent();
                        _c.label = 3;
                    case 3:
                        _i++;
                        return [3 /*break*/, 1];
                    case 4: return [2 /*return*/];
                }
            });
        }); };
        DrupalApi.prototype.getEntityRelationships = function (t) { return __awaiter$2(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, new Promise(function (e) { var r = []; if (t.data && t.data.relationships && Object.keys(t.data.relationships).forEach(function (e, n) { e.match("field_") && r.push(t.data.relationships[e]); }), r.length) {
                            var t_6 = [];
                            r.forEach(function (e) { e.links && e.links.self && t_6.push(e.links.related); }), t_6.length ? _this.getResources(t_6).then(function () { e(); }) : e();
                        }
                        else
                            e(); })];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        }); };
        DrupalApi.prototype.getNode = function (t, e) { return __awaiter$2(this, void 0, void 0, function () {
            var r, n;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        r = this;
                        n = "uuid";
                        "number" == typeof (t = __chunk_2.Util.castToType(t)) && (n = "nid");
                        return [4 /*yield*/, new Promise(function (i) { var o; if (_this.cache) {
                                var e_2;
                                if (Object.keys(_this.entities).forEach(function (r) { _this.entities[r].data && _this.entities[r].data.attributes[n] == t && (e_2 = _this.entities[r]); }), e_2)
                                    return void i(e_2);
                            } if (_this.views.node_index && _this.views.node_index.forEach(function (r) { r[n] == t && (e = r.type); }), e)
                                o = "node/" + e, _this.get(o + "?filter[" + n + "][value]=" + t).then(function (t) { s(t); });
                            else {
                                var e_3 = [];
                                Object.keys(_this.collections).forEach(function (t) { t.match("node--") && e_3.push(t); }), e_3.forEach(function (e) { var r = e.split("--")[0] + "/" + e.split("--")[1]; _this.get(r + "?filter[" + n + "][value]=" + t).then(function (t) { s(t); }); });
                            } function s(t) { if (t.data.length) {
                                var e_4 = { data: t.data[0] };
                                r.entities[t.data[0].id] = e_4, r.collections[t.data[0].type].data || (r.collections[t.data[0].type].data = []), r.collections[t.data[0].type].data.push(e_4), r.nodes[t.data[0].attributes.nid] = e_4, e_4.data.relationships && r.getEntityRelationships(e_4).then(function () { i(e_4); });
                            } } })];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        }); };
        DrupalApi.prototype.getNodes = function (t) { return __awaiter$2(this, void 0, void 0, function () {
            var e, _a, _b, _i, t_7;
            var _this = this;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        e = [];
                        t.forEach(function (t) { e.push(_this.getNode(t)); });
                        _a = [];
                        for (_b in e)
                            _a.push(_b);
                        _i = 0;
                        _c.label = 1;
                    case 1:
                        if (!(_i < _a.length)) return [3 /*break*/, 4];
                        t_7 = _a[_i];
                        return [4 /*yield*/, e[t_7]];
                    case 2:
                        _c.sent();
                        _c.label = 3;
                    case 3:
                        _i++;
                        return [3 /*break*/, 1];
                    case 4: return [2 /*return*/];
                }
            });
        }); };
        DrupalApi.prototype.saveNode = function (t) { return __awaiter$2(this, void 0, void 0, function () { var e, _a; return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    e = t.data.type.split("--")[0] + "/" + t.data.type.split("--")[1];
                    if (!t.data.id) return [3 /*break*/, 2];
                    return [4 /*yield*/, this.patch(e, t.data.id, t)];
                case 1:
                    _a = _b.sent();
                    return [3 /*break*/, 4];
                case 2: return [4 /*yield*/, this.post(e, t)];
                case 3:
                    _a = _b.sent();
                    _b.label = 4;
                case 4:
                    _a;
                    return [2 /*return*/];
            }
        }); }); };
        DrupalApi.prototype.deleteNode = function (t) { return __awaiter$2(this, void 0, void 0, function () { var e; return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    e = t.data.type.split("--")[0] + "/" + t.data.type.split("--")[1];
                    return [4 /*yield*/, this.delete(e, t.data.id).then(function (t) { })];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        }); }); };
        DrupalApi.prototype.getById = function (t) {
            var _this = this;
            var e;
            if (Object.keys(this.collections).forEach(function (r, n) { _this.collections[r].data && _this.collections[r].data.length && _this.collections[r].data.forEach(function (r) { r.id === t && (e = r); }); }), e)
                return { data: e };
        };
        DrupalApi.prototype.getByNodeId = function (t) { return __awaiter$2(this, void 0, void 0, function () {
            var e;
            var _this = this;
            return __generator(this, function (_a) {
                if (Object.keys(this.collections).forEach(function (r, n) { r.match("node--") && _this.collections[r].data && _this.collections[r].data.length && _this.collections[r].data.forEach(function (r) { r.attributes.nid == t && (e = r); }); }), e)
                    return [2 /*return*/, { data: e }];
                return [2 /*return*/];
            });
        }); };
        return DrupalApi;
    }(BaseApi));  var __awaiter$3 = function (t, e, r, n) { return new (r || (r = Promise))(function (i, o) { function s(t) { try {
    u(n.next(t));
}
catch (t) {
    o(t);
} } function a(t) { try {
    u(n.throw(t));
}
catch (t) {
    o(t);
} } function u(t) { t.done ? i(t.value) : new r(function (e) { e(t.value); }).then(s, a); } u((n = n.apply(t, e || [])).next()); }); };
    var Drupal = /** @class */ (function () {
        function Drupal(t, e) {
            this.cache = !1, this.defaultAuthHeader = "Basic c2VydmljZXM6OSs0djJGdm0zMi84Ymtl", this.defaultUser = "services", this.model = { entities: {}, collections: {}, views: {}, menus: {}, books: {}, siteParams: {}, nodes: {} }, this.restoreLocalModel = __chunk_3.Storage.restoreLocalModel, this.routes = {}, this.sites = {}, this.saveLocalModel = __chunk_3.Storage.saveLocalModel, this.user = {}, this.version = "drupal-8", this.config = ConfigService.instance, this.api = new BaseApi, this.api.corsMode = "cors", this.jsonApi = new DrupalApi, this.jsonApi.corsMode = "cors", e && (this.api = e), this.model.entities = this.jsonApi.entities, this.model.collections = this.jsonApi.collections, this.model.nodes = this.jsonApi.nodes, this.model.views = this.jsonApi.views, t && this.config.add(t);
        }
        Drupal.prototype.init = function (t) { this.options = t, t.version && (this.version = t.version), this.domain = t.domain, this.static = t.static || !1, this.cache = t.cache || !1, this.standalone = t.standalone || !1, this.base_path = t.base_path || "/", this.protocol = t.protocol || "http:", this.sites[this.domain] = {}, this.file_directory_path = t.file_directory_path || "sites/default/files", this.file_directory_path = this.baseUrl + this.file_directory_path, this.initApi(t); var e = []; return __chunk_2.Util.isOnline() && !this.static ? (e.push(this.getParams()), e.push(this.getLocalStorageCredentials()), e.push(this.jsonApi.getResourcesIndex(t.api.path_prefix, this.config.get("language"))), e.push(new Promise(function (t) { t(); })), Promise.all(e)) : new Promise(function (t) { t(); }); };
        Drupal.prototype.initApi = function (t) { this.baseUrl = t.api.host = this.protocol + "//" + this.domain + this.base_path, this.api.init(t.api), this.jsonApi.init(t.api), this.jsonApi.cache = this.cache, this.config.get("drupal").hasLanguages && this.config.get("language"), "undefined" != typeof window && (window.drupalSettings = window.drupalSettings || {}, window.Drupal && (window.Drupal.url && window.Drupal.url.isLocal() ? this.isLocal = !0 : this.isLocal = !1), window.Drupal = window.Drupal || {}, window.Drupal.app_server_model = this.model, window.Drupal.settings = window.Drupal.settings || {}, window.Drupal.settings.baseUrl = this.baseUrl), "drupal-8" === this.version && (__chunk_2.Util.getGlobal().jDrupal || this.initJDrupal(), jDrupal = __chunk_2.Util.getGlobal().jDrupal, jDrupal.config("sitePath", this.options.protocol + "//" + this.options.domain)), "withCredentials" in new XMLHttpRequest && (this.cors = !0); };
        Drupal.prototype.login = function (t, e) { var r = this; return new Promise(function (n) { jDrupal.userLogin(t, e).then(function () { r.user = jDrupal.currentUser(), r.api.setCredentials(jDrupal.settings, "drupal:authUser:" + r.domain), r.authenticated = !0, n(); }); }); };
        Drupal.prototype.logout = function () { var t = this; return new Promise(function (e) { t.jsonApi.logout("drupal:authUser:" + t.domain), t.authenticated = !1, console.log("logged out"), e(); }); };
        Drupal.prototype.getParams = function () { var t = this; return new Promise(function (e) { var r = "index.php/rest/params"; "drupal-7" === t.version && (r = "?q=rest/app_params.json"), t.jsonApi.get(r, { _format: "json" }).then(function (r) { t.model.siteParams = t.siteParams = r, e(r); }); }); };
        Drupal.prototype.getView = function (t, e) {
            var _this = this;
            return new Promise(function (r, n) { var i = t; _this.api.get(i.attributes.display.rest_export_1.display_options.path, e).then(function (t) { _this.model.views[i.attributes.id] = t, r(); }); });
        };
        Drupal.prototype.getAppNode = function (t) {
            var _this = this;
            return new Promise(function (e) { _this.jsonApi.getNode(t, "app").then(function (r) { var n = [], i = r.data.attributes; !0 === i.field_cache && (_this.jsonApi.cache = i.field_cache), i.field_preload_collections && n.push(_this.jsonApi.getAllCollections(!0)), Object.keys(_this.model.entities).forEach(function (e) { var r = _this.model.entities[e]; "menu--menu" == r.type && n.push(_this.menuLoad(r.attributes.id, i.cache)), "view--view" == r.type && n.push(_this.getView(r)), r.data && r.data.type.match("node--") && r.data.attributes.uuid !== t && n.push(_this.jsonApi.getNode(r.data.attributes.uuid)); }), _this.getSiteData(n).then(function () { e(); }); }); });
        };
        Drupal.prototype.getSiteData = function (t) { var e = this; return new Promise(function (r) { Promise.all(t).then(function () { e.cache && e.saveLocalModel(e.model, e.domain), r(); }); }); };
        Drupal.prototype.getLocalStorageCredentials = function () { var t = this; return new Promise(function (e, r) { var n = t.api.getCredentials("drupal:authUser:" + t.domain); if (n) {
            for (var t_8 in n)
                jDrupal.settings[t_8] = n[t_8];
            var r_1 = atob(n.auth_hash);
            t.login(r_1.split(":")[0], r_1.split(":")[1]).then(function () { e(); });
        }
        else
            e(); }); };
        Drupal.prototype.menuLoad = function (t, e) {
            if (e === void 0) { e = !1; }
            return __awaiter$3(this, void 0, void 0, function () { var r; return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        r = this;
                        return [4 /*yield*/, new Promise(function (n, i) { r.api.get("entity/menu/" + t + "/tree", { _format: "hal_json" }).then(function (i) { var o = i; if (r.model.menus[t] = o, e) {
                                var t_9 = [];
                                o.forEach(function (e) { e.link.route_parameters.node && t_9.push(e.link.route_parameters.node); }), r.jsonApi.getNodes(t_9).then(function () { n(o); });
                            }
                            else
                                n(o); }); })];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            }); });
        };
        Drupal.prototype.bookLoad = function (t, e) {
            if (e === void 0) { e = !1; }
            var r = this;
            return new Promise(function (n, i) { r.api.get("rest/book_tree/" + t, { _format: "hal_json" }).then(function (i) { r.model.books[t] = i; var o = r.parseBookTree(t); e ? r.jsonApi.getNodes(o).then(function () { n(i); }) : n(i); }); });
        };
        Drupal.prototype.parseBookTree = function (t) { var e = this.model.books[t]; var r, n, i, o, s; for (o in n = [], i = [], e)
            r = e[o].below; for (o in r) {
            var t_10 = void 0;
            for (s in r[o])
                (t_10 = r[o]).link.route_parameters = { node: r[o].link.nid }, t_10.link.href = "/node/" + t_10.link.nid, i.push(r[o].link.nid);
            n.push(t_10);
        } return this.model.menus["book_" + t] = n, i; };
        Drupal.prototype.nodeLoad = function (t) { var e = this; return new Promise(function (r, n) { e.cache && e.model.nodes[t] ? r(e.model.nodes[t]) : jDrupal.nodeLoad(t).then(function (t) { e.model.nodes[t.entity.nid[0].value] = t, e.model.entities[t.entity.uuid[0].value] = t.entity, r(t); }); }); };
        Drupal.prototype.nodesLoad = function (t) { var e = this, r = []; return new Promise(function (n) { for (var n_1 in t)
            r.push(e.nodeLoad(t[n_1])); Promise.all(r).then(function () { n(); }); }); };
        Drupal.prototype.createNode = function (t) { var e = {}; for (var r in t)
            e[r] = [{ value: t[r] }]; return e.title = [{ value: t.title }], e.type = [{ target_id: t.type }], e._links = { type: { href: jDrupal.restPath() + "rest/type/node/" + t.type } }, new jDrupal.Node(e); };
        Drupal.prototype.initJDrupal = function () { var jDrupal = {}; var a; jDrupal.init = function () { jDrupal.csrf_token = !1, jDrupal.sessid = null, jDrupal.modules = {}, jDrupal.connected = !1, jDrupal.settings = { sitePath: null, basePath: "/" }, "undefined" == typeof window && (__chunk_2.global.Buffer = __chunk_2.global.Buffer || require("buffer").Buffer, "undefined" == typeof btoa && (__chunk_2.global.btoa = function (t) { return new Buffer(t).toString("base64"); }), "undefined" == typeof atob && (__chunk_2.global.atob = function (t) { return new Buffer(t, "base64").toString(); })); }, jDrupal.init(), jDrupal.config = function (t) { var e = void 0 !== arguments[1] ? arguments[1] : null; return e ? void (jDrupal.settings[t] = e) : jDrupal.settings[t]; }, jDrupal.sitePath = function () { return jDrupal.settings.sitePath; }, jDrupal.basePath = function () { return jDrupal.settings.basePath; }, jDrupal.restPath = function () { return this.sitePath() + this.basePath(); }, jDrupal.path = function () { return this.restPath().substr(this.restPath().indexOf("://") + 3).replace("localhost", ""); }, jDrupal.isReady = function () { try {
            var t = !jDrupal.isEmpty(jDrupal.sitePath());
            return t || console.log("sitePath not setVar in jdrupal.settings.js"), t;
        }
        catch (t) {
            console.log("jDrupal.isReady - " + t);
        } }, jDrupal.isEmpty = function (t) { return null !== t && "object" == typeof t ? 0 === Object.keys(t).length : void 0 === t || null === t || "" == t; }, jDrupal.functionExists = function (name) { try {
            return "function" == eval("typeof " + name);
        }
        catch (t) {
            alert("jDrupal.functionExists - " + t);
        } }, jDrupal.inArray = function (t, e) { try {
            if (void 0 === e)
                return !1;
            if ("string" == typeof t)
                return e.indexOf(t) > -1;
            for (var r = !1, n = 0; n < e.length; n++)
                if (e[n] == t) {
                    r = !0;
                    break;
                }
            return r;
        }
        catch (t) {
            console.log("jDrupal.inArray - " + t);
        } }, jDrupal.isArray = function (t) { return "[object Array]" === Object.prototype.toString.call(t); }, jDrupal.isInt = function (t) { return "string" == typeof t && (t = parseInt(t)), "number" == typeof t && t % 1 == 0; }, jDrupal.isPromise = function (t) { return Promise.resolve(t) == t; }, jDrupal.shuffle = function (t) { for (var e = t.length - 1; e > 0; e--) {
            var r = Math.floor(Math.random() * (e + 1)), n = t[e];
            t[e] = t[r], t[r] = n;
        } return t; }, jDrupal.time = function () { var t = new Date; return Math.floor(t / 1e3); }, jDrupal.lcfirst = function (t) { return (t += "").charAt(0).toLowerCase() + t.substr(1); }, jDrupal.ucfirst = function (t) { return (t += "").charAt(0).toUpperCase() + t.substr(1); }, jDrupal.Module = function () { this.name = null; }, jDrupal.moduleExists = function (t) { try {
            return void 0 !== jDrupal.modules[t];
        }
        catch (t) {
            console.log("jDrupal.moduleExists - " + t);
        } }, jDrupal.moduleImplements = function (t) { try {
            var e = [];
            if (t)
                for (var r in jDrupal.modules)
                    jDrupal.modules.hasOwnProperty(r) && jDrupal.functionExists(r + "_" + t) && e.push(r);
            return 0 != e.length && e;
        }
        catch (t) {
            console.log("jDrupal.moduleImplements - " + t);
        } }, jDrupal.moduleInvoke = function (t, e) { if (jDrupal.moduleLoad(t)) {
            var r = t + "_" + e;
            if (jDrupal.functionExists(r)) {
                var n = window[r], i = Array.prototype.slice.call(arguments);
                return i.splice(0, 2), 0 == Object.getOwnPropertyNames(i).length ? n() : n.apply(null, i);
            }
        } }, jDrupal.moduleInvokeAll = function (t) { var e = [], r = Array.prototype.slice.call(arguments); r.splice(0, 1); var n = []; for (var i in jDrupal.modules)
            jDrupal.modules.hasOwnProperty(i) && jDrupal.functionExists(i + "_" + t) && n.push(i); if (jDrupal.isEmpty(n))
            return Promise.resolve(); for (var o = 0; o < n.length; o++)
            0 == r.length ? e.push(jDrupal.moduleInvoke(n[o], t)) : (r.unshift(n[o], t), e.push(jDrupal.moduleInvoke.apply(null, r)), r.splice(0, 2)); return Promise.all(e); }, jDrupal.moduleLoad = function (t) { try {
            return !!jDrupal.modules[t] && jDrupal.modules[t];
        }
        catch (t) {
            console.log("jDrupal.moduleLoad - " + t);
        } }, jDrupal.modulesLoad = function () { return jDrupal.modules; }, a = XMLHttpRequest.prototype.send, XMLHttpRequest.prototype.send = function (t) { var e = this, r = jDrupal.moduleInvokeAll("rest_pre_process", this, t); r ? r.then(function () { a.call(e, t); }) : a.call(this, t); }, jDrupal.token = function () { return new Promise(function (t, e) { var r = new XMLHttpRequest; r.dg = { service: "system", resource: "token" }, r.open("GET", jDrupal.restPath() + "rest/session/token"), r.onload = function () { if (200 == r.status) {
            var n = jDrupal.moduleInvokeAll("rest_post_process", r);
            n ? n.then(t(r.response)) : t(r.response);
        }
        else
            e(Error(r.statusText)); }, r.onerror = function () { e(Error("Network Error")); }, r.send(); }); }, jDrupal.connect = function () { return new Promise(function (t, e) { var r = new XMLHttpRequest; r.dg = { service: "system", resource: "connect" }, r.open("GET", jDrupal.restPath() + "jdrupal/connect?_format=json"); var n = function () { jDrupal.connected = !0; var e = JSON.parse(void 0 !== r.responseText ? r.responseText : r.response); 0 == e.uid ? (jDrupal.setCurrentUser(jDrupal.userDefaults()), t(e)) : jDrupal.userLoad(jDrupal.config("uid")).then(function (r) { jDrupal.setCurrentUser(r), t(e); }); }; r.onload = function () { if (200 == r.status) {
            var t = jDrupal.moduleInvokeAll("rest_post_process", r);
            t ? t.then(n) : n();
        }
        else
            e(Error(r.statusText)); }, r.onerror = function () { e(Error("Network Error")); }, r.send(); }); }, jDrupal.userLogin = function (t, e) { return new Promise(function (r, n) { var i = new XMLHttpRequest; i.dg = { service: "user", resource: "login" }, i.open("POST", jDrupal.restPath() + "user/login?_format=json"), i.setRequestHeader("Content-type", "application/json"), i.onload = function () { JSON.parse(i.responseText).current_user && (jDrupal.settings.uid = JSON.parse(i.responseText).current_user.uid, jDrupal.settings.user_csrf_token = JSON.parse(i.responseText).csrf_token, jDrupal.settings.user_logout_token = JSON.parse(i.responseText).logout_token, jDrupal.settings.auth_hash = btoa(t + ":" + e), jDrupal.userLoad(jDrupal.config("uid")).then(function (t) { jDrupal.setCurrentUser(t), r(t); })), 200 == i.status ? jDrupal.moduleInvokeAll("rest_post_process", i) : n(Error(i.statusText)); }, i.onerror = function () { n(Error("Network Error")); }, i.send(JSON.stringify({ name: t, pass: e })); }); }, jDrupal.userLogout = function () { return new Promise(function (t, e) { var r = new XMLHttpRequest; r.dg = { service: "user", resource: "logout" }, r.open("GET", jDrupal.restPath() + "user/logout?_format=hal+json&token=" + jDrupal.settings.user_logout_token), r.setRequestHeader("Content-type", "application/json"); var n = function () { jDrupal.setCurrentUser(jDrupal.userDefaults()), jDrupal.connect().then(t); }; r.onload = function () { if (200 == r.status || 303 == r.status) {
            var t = jDrupal.moduleInvokeAll("rest_post_process", r);
            t ? t.then(n) : n();
        }
        else
            e(Error(r.statusText)); }, r.onerror = function () { e(Error("Network Error")); }, r.send(); }); }, jDrupal.entityLoad = function (t, e) { return new (this[this.ucfirst(t)])(e).load(); }, jDrupal.commentLoad = function (t) { return this.entityLoad("comment", t); }, jDrupal.nodeLoad = function (t) { return this.entityLoad("node", t); }, jDrupal.userLoad = function (t) { return this.entityLoad("user", t); }, jDrupal.userRegister = function (t, e, r) { return new Promise(function (n, i) { jDrupal.token().then(function (o) { var s = new XMLHttpRequest; s.dg = { service: "user", resource: "register" }, s.open("POST", jDrupal.restPath() + "user/register?_format=hal_json"), s.setRequestHeader("Content-type", "application/json"), s.setRequestHeader("Authorization", "Basic " + jDrupal.settings.auth_hash), s.setRequestHeader("X-CSRF-Token", o); var a = function () { jDrupal.connect().then(n); }; s.onload = function () { if (200 == s.status) {
            var t = jDrupal.moduleInvokeAll("rest_post_process", s);
            t ? t.then(a) : a();
        }
        else
            i(Error(s.statusText)); }, s.onerror = function () { i(Error("Network Error")); }, s.send(JSON.stringify({ name: { value: t }, pass: { value: e }, mail: { value: r } })); }); }); }, jDrupal.Views = function (t) { this.path = t, this.results = null; }, jDrupal.Views.prototype.getPath = function () { return this.path; }, jDrupal.Views.prototype.getResults = function () { return this.results; }, jDrupal.Views.prototype.getView = function () { var t = this; return new Promise(function (e, r) { var n = new XMLHttpRequest; n.dg = { service: "views", resource: null }, n.open("GET", jDrupal.restPath() + t.getPath()); var i = function () { t.results = JSON.parse(n.response), e(); }; n.onload = function () { if (200 == n.status) {
            var t = jDrupal.moduleInvokeAll("rest_post_process", n);
            t ? t.then(i) : i();
        }
        else
            r(Error(n.statusText)); }, n.onerror = function () { r(Error("Network Error")); }, n.send(); }); }, jDrupal.viewsLoad = function (t) { return new Promise(function (e, r) { var n = new jDrupal.Views(t); n.getView().then(function () { e(n); }); }); }, jDrupal.Entity = function (t, e, r) { this.entity = null, this.bundle = e, this.entityID = r, this.entityKeys = {}; }, jDrupal.Entity.prototype.getVar = function (t, e) { return this.entity && void 0 !== this.entity[t] ? void 0 !== e ? this.entity[t][e] : this.entity[t] : null; }, jDrupal.Entity.prototype.setVar = function (t, e, r) { this.entity && (void 0 !== e && void 0 !== this.entity[t] ? this.entity[t][e] = r : this.entity[t] = r); }, jDrupal.Entity.prototype.getEntityKey = function (t) { return void 0 !== this.entityKeys[t] ? this.entityKeys[t] : null; }, jDrupal.Entity.prototype.getEntityType = function () { return this.entityKeys.type; }, jDrupal.Entity.prototype.getBundle = function () { var t = this.getEntityKey("bundle"); return void 0 !== this.entity[t] ? this.entity[t][0].target_id : null; }, jDrupal.Entity.prototype.id = function () { var t = this.getEntityKey("id"); return void 0 !== this.entity[t] ? this.entity[t][0].value : null; }, jDrupal.Entity.prototype.language = function () { return this.entity.langcode[0].value; }, jDrupal.Entity.prototype.isNew = function () { return !this.id(); }, jDrupal.Entity.prototype.label = function () { var t = this.getEntityKey("label"); return void 0 !== this.entity[t] ? this.entity[t][0].value : null; }, jDrupal.Entity.prototype.stringify = function () { return JSON.stringify(this.entity); }, jDrupal.Entity.prototype.preLoad = function (t) { return new Promise(function (t, e) { t(); }); }, jDrupal.Entity.prototype.load = function () { try {
            var t = this, e = t.getEntityType();
            return new Promise(function (r, n) { t.preLoad().then(function () { var i = jDrupal.restPath() + e + "/" + t.id() + "?_format=hal_json", o = new XMLHttpRequest; o.dg = { service: e, resource: "retrieve" }, o.open("GET", i), jDrupal.settings.auth_hash && o.setRequestHeader("Authorization", "Basic " + jDrupal.settings.auth_hash); var s = function () { t.entity = JSON.parse(o.response), t.postLoad(o).then(function () { r(t); }); }; o.onload = function () { if (200 == o.status) {
                var t = jDrupal.moduleInvokeAll("rest_post_process", o);
                t ? t.then(s) : s();
            }
            else
                n(Error(o.statusText)); }, o.onerror = function () { n(Error("Network Error")); }, o.send(); }); });
        }
        catch (t) {
            console.log("jDrupal.Entity.load - " + t);
        } }, jDrupal.Entity.prototype.postLoad = function (t) { return new Promise(function (t, e) { t(); }); }, jDrupal.Entity.prototype.preSave = function (t) { return new Promise(function (t, e) { t(); }); }, jDrupal.Entity.prototype.save = function () { var t = this; return new Promise(function (e, r) { t.preSave().then(function () { jDrupal.token().then(function (n) { var i = t.getEntityType(), o = null, s = null, a = null; t.isNew() ? (o = "POST", s = "create", a = "entity/" + i) : (o = "PATCH", s = "update", a = i + "/" + t.id()); var u = new XMLHttpRequest; u.dg = { service: i, resource: s }, u.open(o, jDrupal.restPath() + a + "?_format=hal_json"), u.setRequestHeader("Accept", "application/json"), u.setRequestHeader("Authorization", "Basic " + jDrupal.settings.auth_hash), u.setRequestHeader("Content-Type", "application/hal+json"), u.setRequestHeader("X-CSRF-Token", n), u.onload = function () { t.postSave(u).then(function () { if ("POST" == o && 201 == u.status || "PATCH" == o && 204 == u.status || 200 == u.status) {
            var t = jDrupal.moduleInvokeAll("rest_post_process", u);
            t ? t.then(e) : e();
        }
        else
            r(Error(u.statusText)); }); }, u.onerror = function () { r(Error("Network Error")); }, u.send(t.stringify()); }); }); }); }, jDrupal.Entity.prototype.postSave = function (t) { var e = this; return new Promise(function (r, n) { if (e.isNew() && t.getResponseHeader("Location")) {
            var i = t.getResponseHeader("Location").split("/");
            e.entity[e.getEntityKey("id")] = [{ value: i[i.length - 1] }];
        } r(); }); }, jDrupal.Entity.prototype.preDelete = function (t) { return new Promise(function (t, e) { t(); }); }, jDrupal.Entity.prototype.delete = function (t) { var e = this; return new Promise(function (t, r) { e.preDelete().then(function () { jDrupal.token().then(function (n) { var i = e.getEntityType(), o = jDrupal.restPath() + i + "/" + e.id(), s = {}; s[e.getEntityKey("bundle")] = [{ target_id: e.getBundle() }], "node" === i && (s._links = { type: { href: jDrupal.restPath() + "rest/type/node/" + e.getBundle() } }); var a = new XMLHttpRequest; a.dg = { service: i, resource: "delete" }, a.open("DELETE", o), a.setRequestHeader("Content-type", "application/hal+json"), a.setRequestHeader("Accept", "application/json"), a.setRequestHeader("Authorization", "Basic " + jDrupal.settings.auth_hash), a.setRequestHeader("X-CSRF-Token", n), a.onload = function () { e.postDelete(a).then(function () { if (204 == a.status) {
            var e = jDrupal.moduleInvokeAll("rest_post_process", a);
            e ? e.then(t) : t();
        }
        else
            r(Error(a.statusText)); }); }, a.onerror = function () { r(Error("Network Error")); }, a.send(JSON.stringify(s)); }); }); }); }, jDrupal.Entity.prototype.postDelete = function (t) { var e = this; return new Promise(function (t, r) { e.entity = null, t(); }); }, jDrupal.entityConstructorPrep = function (t, e) { if (e)
            if ("object" == typeof e)
                t.entity = e;
            else {
                var r = {};
                r[t.getEntityKey("id")] = [{ value: e }], t.entity = r;
            } }, jDrupal.Comment = function (t) { this.entityKeys.type = "comment", this.entityKeys.bundle = "comment_type", this.entityKeys.id = "cid", this.entityKeys.label = "subject", jDrupal.entityConstructorPrep(this, t); }, jDrupal.Comment.prototype = new jDrupal.Entity, jDrupal.Comment.prototype.constructor = jDrupal.Comment, jDrupal.Comment.prototype.getSubject = function () { return this.entity.subject[0].value; }, jDrupal.Comment.prototype.setSubject = function (t) { try {
            this.entity.subject[0].value = t;
        }
        catch (t) {
            console.log("jDrupal.Comment.setSubject - " + t);
        } }, jDrupal.Comment.prototype.preSave = function (t) { return new Promise(function (t, e) { t(); }); }, jDrupal.Comment.prototype.stringify = function () { try {
            if (!this.isNew()) {
                for (var t = JSON.parse(JSON.stringify(this.entity)), e = ["hostname", "changed", "cid", "thread", "uuid", "entity_id", "entity_type", "pid", "field_name", "created", "name", "mail", "homepage"], r = 0; r < e.length; r++)
                    void 0 !== t[e[r]] && delete t[e[r]];
                return JSON.stringify(t);
            }
            return JSON.stringify(this.entity);
        }
        catch (t) {
            console.log("jDrupal.Comment.stringify - " + t);
        } }, jDrupal.Node = function (t) { this.entityKeys.type = "node", this.entityKeys.bundle = "type", this.entityKeys.id = "nid", this.entityKeys.label = "title", jDrupal.entityConstructorPrep(this, t), this.entity && (this.entity.title || (this.entity.title = [{ value: "" }])); }, jDrupal.Node.prototype = new jDrupal.Entity, jDrupal.Node.prototype.constructor = jDrupal.Node, jDrupal.Node.prototype.getTitle = function () { return this.label(); }, jDrupal.Node.prototype.setTitle = function (t) { try {
            this.entity.title[0].value = t;
        }
        catch (t) {
            console.log("jDrupal.Node.setTitle - " + t);
        } }, jDrupal.Node.prototype.getType = function () { return this.getBundle(); }, jDrupal.Node.prototype.getCreatedTime = function () { return this.entity.created[0].value; }, jDrupal.Node.prototype.isPromoted = function () { return this.entity.promote[0].value; }, jDrupal.Node.prototype.isPublished = function () { return this.entity.status[0].value; }, jDrupal.Node.prototype.isSticky = function () { return this.entity.sticky[0].value; }, jDrupal.Node.prototype.preSave = function (t) { var e = this; return new Promise(function (t, r) { for (var n = ["_embedded", "changed", "revision_timestamp", "revision_uid", "revision_translation_affected"], i = 0; i < n.length; i++)
            delete e.entity[n[i]]; t(); }); }, jDrupal.User = function (t) { this.entityKeys.type = "user", this.entityKeys.bundle = "user", this.entityKeys.id = "uid", this.entityKeys.label = "name", jDrupal.entityConstructorPrep(this, t); }, jDrupal.User.prototype = new jDrupal.Entity, jDrupal.User.prototype.constructor = jDrupal.User, jDrupal.User.prototype.getAccountName = function () { return this.label(); }, jDrupal.User.prototype.getRoles = function () { for (var t = (this.entity.roles, []), e = 0; e < this.entity.roles.length; e++)
            t.push(this.entity.roles[e].target_id); return t; }, jDrupal.User.prototype.hasRole = function (t) { return jDrupal.inArray(t, this.getRoles()); }, jDrupal.User.prototype.isAnonymous = function () { return 0 == this.id(); }, jDrupal.User.prototype.isAuthenticated = function () { return !this.isAnonymous(); }, jDrupal.currentUser = function () { return jDrupal._currentUser; }, jDrupal.User.prototype.postLoad = function (t) { var e = this; return new Promise(function (t, r) { e.entity.roles || (e.entity.roles = [{ target_id: "authenticated" }]), t(); }); }, jDrupal.userDefaults = function () { return new jDrupal.User({ uid: [{ value: 0 }], roles: [{ target_id: "anonymous" }] }); }, jDrupal.setCurrentUser = function (t) { t.isAuthenticated() && !jDrupal.inArray("authenticated", t.getRoles()) && t.entity.roles.push({ target_id: "authenticated" }), jDrupal._currentUser = t; }, jDrupal.userPassword = function () { var t = 10; arguments[0] && (t = arguments[0]); for (var e = "", r = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz23456789", n = 0; n < t; n++)
            e += r.charAt(Math.floor(Math.random() * r.length)); return e; }, __chunk_2.Util.getGlobal().jDrupal = jDrupal; };
        return Drupal;
    }());  exports.ConfigService = ConfigService, exports.BaseApi = BaseApi, exports.Drupal = Drupal; });
