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 };
    }
};
import { a as Util } from "./chunk-25ecf4a4.js";
import { a as Storage } from "./chunk-7fb7349a.js";
var __awaiter = function (t, e, i, s) { return new (i || (i = Promise))(function (o, n) { function a(t) { try {
    h(s.next(t));
}
catch (t) {
    n(t);
} } function r(t) { try {
    h(s.throw(t));
}
catch (t) {
    n(t);
} } function h(t) { t.done ? o(t.value) : new i(function (e) { e(t.value); }).then(a, r); } h((s = s.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 i in e)
                t_1.headers[i] = e[i]; });
        }
        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 (i) { i === t && (e = _this.routes[i]); }), 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 (i) { return t += i + "=" + e[i] + "&"; }));
                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, i) { 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(i) })];
            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.setItem(e, JSON.stringify(t));
    };
    BaseApi.prototype.getCredentials = function (t) {
        if (t === void 0) { t = "app:authData"; }
        if ("undefined" != typeof window && 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 i in t)
        e.routes[i] = t[i]; }), e.settings.dev && e.settings.routes_dev && e.settings.routes_dev.forEach(function (t) { for (var i in t)
        e.routes[i] = t[i]; }); };
    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, i, s) { return new (i || (i = Promise))(function (o, n) { function a(t) { try {
    h(s.next(t));
}
catch (t) {
    n(t);
} } function r(t) { try {
    h(s.throw(t));
}
catch (t) {
    n(t);
} } function h(t) { t.done ? o(t.value) : new i(function (e) { e(t.value); }).then(a, r); } h((s = s.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, i) {
        if (i === void 0) { i = !1; }
        if (!this.islocked(t))
            return i && (e = Util.castToType(e)), this._config[t] = e;
        console.error('Config: property "' + t + '" can not be changed.');
    };
    Config.prototype.add = function (t, e, i) {
        if (e === void 0) { e = !1; }
        if (i === void 0) { i = !1; }
        var s = this;
        e ? this.extend(t) : Object.keys(t).forEach(function (e) { s.set(e, t[e], i); });
    };
    Config.prototype.fetch = function (t, e) {
        if (e === void 0) { e = !1; }
        return __awaiter$1(this, void 0, void 0, function () { var i, s; return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    i = this;
                    return [4 /*yield*/, fetch(t).then(function (t) { return t.json(); }).catch(function (t) { console.log("config fetch error:", t.message); })];
                case 1:
                    s = _a.sent();
                    return [2 /*return*/, (e ? this.extend(s) : Object.keys(s).forEach(function (t, e) { i.set(t, s[t]); }), s)];
            }
        }); });
    };
    Config.prototype.extend = function (t, e) {
        if (e === void 0) { e = this._config; }
        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(Storage.restoreLocalModel(t)); };
    Config.prototype.setLocalSettings = function (t, e) {
        if (e === void 0) { e = this._config; }
        "undefined" != typeof window && window.localStorage && Storage.saveLocalModel(e, t);
    };
    Config.prototype.setDefaults = function (t) { var e = this; e._defaults = t, Object.keys(t).forEach(function (i) { e.set(i, Util.copy(t[i])); }); };
    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 __awaiter$2 = function (t, e, i, s) { return new (i || (i = Promise))(function (o, n) { function a(t) { try {
    h(s.next(t));
}
catch (t) {
    n(t);
} } function r(t) { try {
    h(s.throw(t));
}
catch (t) {
    n(t);
} } function h(t) { t.done ? o(t.value) : new i(function (e) { e(t.value); }).then(a, r); } h((s = s.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, i) { 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", i.data.attributes.changed && (i.data.attributes.changed = void 0);
                return [4 /*yield*/, fetch("" + this.baseUrl + t + "/" + e, { headers: this.headers, method: "PATCH", body: JSON.stringify(i) })];
            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 i; return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    i = this;
                    return [4 /*yield*/, new Promise(function (s, o) { var n = i.baseUrl + "index.php/"; e && (n += e + "/"), n += t, fetch(n).then(function (t) { if (t.status >= 400)
                            throw new Error("JsonApi: Bad resources response from server"); return t.json(); }).then(function (t) { i.resources = t, i.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--")) && (i.collections[t] = {}); }), s(t); }); })];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        }); });
    };
    DrupalApi.prototype.getCollection = function (t, e, i) {
        if (i === void 0) { i = !1; }
        return __awaiter$2(this, void 0, void 0, function () {
            var e, s, _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:
                        s = _a[_b] = _e.sent();
                        if (!i) return [3 /*break*/, 5];
                        t_2 = [];
                        Object.keys(s.data).forEach(function (e) { t_2.push(_this.getEntityRelationships({ data: s.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(s.data).forEach(function (t, e) { _this.entities[s.data[t].attributes.uuid] = s.data[t]; }), s)];
                }
            });
        });
    };
    DrupalApi.prototype.getCollections = function (t, e, i) {
        if (i === void 0) { i = !1; }
        return __awaiter$2(this, void 0, void 0, function () {
            var s, _a, _b, _i, t_3;
            var _this = this;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        s = [];
                        t.forEach(function (t) { s.push(_this.getCollection(t, e, i)); });
                        _a = [];
                        for (_b in s)
                            _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*/, s[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 i, _a, _b, _i, t_4;
            var _this = this;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        i = [];
                        Object.keys(this.collections).forEach(function (s, o) { var n = !1; e.forEach(function (t) { t === s && (n = !0); }), n || i.push(_this.getCollection(s, {}, t)); });
                        _a = [];
                        for (_b in i)
                            _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*/, i[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 (i) { return t += i + "=" + e[i] + "&"; }));
                    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 i = []; if (t.data && t.data.relationships && Object.keys(t.data.relationships).forEach(function (e, s) { e.match("field_") && i.push(t.data.relationships[e]); }), i.length) {
                        var t_6 = [];
                        i.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 i, s;
        var _this = this;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    i = this;
                    s = "uuid";
                    "number" == typeof (t = Util.castToType(t)) && (s = "nid");
                    return [4 /*yield*/, new Promise(function (o) { var n; if (_this.cache) {
                            var e_2;
                            if (Object.keys(_this.entities).forEach(function (i) { _this.entities[i].data && _this.entities[i].data.attributes[s] == t && (e_2 = _this.entities[i]); }), e_2)
                                return void o(e_2);
                        } if (_this.views.node_index && _this.views.node_index.forEach(function (i) { i[s] == t && (e = i.type); }), e)
                            n = "node/" + e, _this.get(n + "?filter[" + s + "][value]=" + t).then(function (t) { a(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 i = e.split("--")[0] + "/" + e.split("--")[1]; _this.get(i + "?filter[" + s + "][value]=" + t).then(function (t) { a(t); }); });
                        } function a(t) { if (t.data.length) {
                            var e_4 = { data: t.data[0] };
                            i.entities[t.data[0].id] = e_4, i.collections[t.data[0].type].data || (i.collections[t.data[0].type].data = []), i.collections[t.data[0].type].data.push(e_4), i.nodes[t.data[0].attributes.nid] = e_4, e_4.data.relationships && i.getEntityRelationships(e_4).then(function () { o(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 (i, s) { _this.collections[i].data && _this.collections[i].data.length && _this.collections[i].data.forEach(function (i) { i.id === t && (e = i); }); }), 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 (i, s) { i.match("node--") && _this.collections[i].data && _this.collections[i].data.length && _this.collections[i].data.forEach(function (i) { i.attributes.nid == t && (e = i); }); }), e)
                return [2 /*return*/, { data: e }];
            return [2 /*return*/];
        });
    }); };
    return DrupalApi;
}(BaseApi));
var __awaiter$3 = function (t, e, i, s) { return new (i || (i = Promise))(function (o, n) { function a(t) { try {
    h(s.next(t));
}
catch (t) {
    n(t);
} } function r(t) { try {
    h(s.throw(t));
}
catch (t) {
    n(t);
} } function h(t) { t.done ? o(t.value) : new i(function (e) { e(t.value); }).then(a, r); } h((s = s.apply(t, e || [])).next()); }); };
var Drupal = /** @class */ (function () {
    function Drupal(t, e) {
        this.user = {}, this.saveLocalModel = Storage.saveLocalModel, this.restoreLocalModel = Storage.restoreLocalModel, this.cache = !1, this.model = { entities: {}, collections: {}, nodes: {}, views: {}, menus: {}, books: {}, siteParams: {} }, this.routes = {}, this.version = "drupal-8", this.sites = {}, this.defaultUser = "services", this.defaultAuthHeader = "Basic c2VydmljZXM6OSs0djJGdm0zMi84Ymtl", 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 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) { if (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"), window && (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, window.drupalSettings = window.drupalSettings || {}), "drupal-8" === this.version) {
        if (window && !window.jDrupal)
            return void console.error("ERROR: jDrupal is not defined in the global namespace.");
        this.jDrupal = jDrupal, jDrupal.config("sitePath", this.options.protocol + "//" + this.options.domain);
    } "withCredentials" in new XMLHttpRequest && (this.cors = !0); };
    Drupal.prototype.login = function (t, e) { var i = this; return new Promise(function (s) { jDrupal.userLogin(t, e).then(function () { i.user = jDrupal.currentUser(), i.api.setCredentials(jDrupal.settings, "drupal:authUser:" + i.domain), i.authenticated = !0, s(); }); }); };
    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 i = "index.php/rest/params"; "drupal-7" === t.version && (i = "?q=rest/app_params.json"), t.jsonApi.get(i, { _format: "json" }).then(function (i) { t.model.siteParams = t.siteParams = i, e(i); }); }); };
    Drupal.prototype.getView = function (t, e) {
        var _this = this;
        return new Promise(function (i, s) { var o = t; _this.api.get(o.attributes.display.rest_export_1.display_options.path, e).then(function (t) { _this.model.views[o.attributes.id] = t, i(); }); });
    };
    Drupal.prototype.getAppNode = function (t) {
        var _this = this;
        return new Promise(function (e) { _this.jsonApi.getNode(t, "app").then(function (i) { var s = [], o = i.data.attributes; !0 === o.field_cache && (_this.jsonApi.cache = o.field_cache), o.field_preload_collections && s.push(_this.jsonApi.getAllCollections(!0)), Object.keys(_this.model.entities).forEach(function (e) { var i = _this.model.entities[e]; "menu--menu" == i.type && s.push(_this.menuLoad(i.attributes.id, o.cache)), "view--view" == i.type && s.push(_this.getView(i)), i.data && i.data.type.match("node--") && i.data.attributes.uuid !== t && s.push(_this.jsonApi.getNode(i.data.attributes.uuid)); }), _this.getSiteData(s).then(function () { e(); }); }); });
    };
    Drupal.prototype.getSiteData = function (t) { var e = this; return new Promise(function (i) { Promise.all(t).then(function () { e.cache && e.saveLocalModel(e.model, e.domain), i(); }); }); };
    Drupal.prototype.getLocalStorageCredentials = function () { var t = this; return new Promise(function (e, i) { var s = t.api.getCredentials("drupal:authUser:" + t.domain); if (s) {
        for (var t_8 in s)
            jDrupal.settings[t_8] = s[t_8];
        var i_1 = atob(s.auth_hash);
        t.login(i_1.split(":")[0], i_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 i; return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    i = this;
                    return [4 /*yield*/, new Promise(function (s, o) { i.api.get("entity/menu/" + t + "/tree", { _format: "hal_json" }).then(function (o) { var n = o; if (i.model.menus[t] = n, e) {
                            var t_9 = [];
                            n.forEach(function (e) { e.link.route_parameters.node && t_9.push(e.link.route_parameters.node); }), i.jsonApi.getNodes(t_9).then(function () { s(n); });
                        }
                        else
                            s(n); }); })];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        }); });
    };
    Drupal.prototype.bookLoad = function (t, e) {
        if (e === void 0) { e = !1; }
        var i = this;
        return new Promise(function (s, o) { i.api.get("rest/book_tree/" + t, { _format: "hal_json" }).then(function (o) { i.model.books[t] = o; var n = i.parseBookTree(t); e ? i.jsonApi.getNodes(n).then(function () { s(o); }) : s(o); }); });
    };
    Drupal.prototype.parseBookTree = function (t) { var e = this.model.books[t]; var i, s, o, n, a; for (n in s = [], o = [], e)
        i = e[n].below; for (n in i) {
        var t_10 = void 0;
        for (a in i[n])
            (t_10 = i[n]).link.route_parameters = { node: i[n].link.nid }, t_10.link.href = "/node/" + t_10.link.nid, o.push(i[n].link.nid);
        s.push(t_10);
    } return this.model.menus["book_" + t] = s, o; };
    Drupal.prototype.nodeLoad = function (t) { var e = this; return new Promise(function (i, s) { e.cache && e.model.nodes[t] ? i(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, i(t); }); }); };
    Drupal.prototype.nodesLoad = function (t) { var e = this, i = []; return new Promise(function (s) { for (var s_1 in t)
        i.push(e.nodeLoad(t[s_1])); Promise.all(i).then(function () { s(); }); }); };
    Drupal.prototype.createNode = function (t) { var e = {}; for (var i in t)
        e[i] = [{ value: t[i] }]; 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); };
    return Drupal;
}());
export { ConfigService as a, BaseApi as b, Drupal as c };
