import { __assign, __awaiter, __generator, __read, __spread, __values } from "tslib";
import { Obj } from "@intelligentgraphics/ig.utilities";
import { IGXA } from "@intelligentgraphics/igd.core";
/**
 * Tries to perform parametrized query to get desired catalog items
 */
export var queryCatalogs = function (_a) {
    var queryDepth = _a.queryDepth, session = _a.session, context = _a.context, where = _a.where;
    return __awaiter(void 0, void 0, void 0, function () {
        var results, catalogManagerSourceIdMap, _loop_1, catalogManagerSourceIdMap_1, catalogManagerSourceIdMap_1_1, _b, catalogManager, sourceId, e_1_1;
        var e_1, _c;
        return __generator(this, function (_d) {
            switch (_d.label) {
                case 0:
                    results = [];
                    catalogManagerSourceIdMap = getCatalogManagersForQuery(session, context);
                    _loop_1 = function (catalogManager, sourceId) {
                        var rootItems, rootContext, processLevel, rootItems_1, rootItems_1_1, rootItem;
                        var e_2, _a;
                        return __generator(this, function (_b) {
                            switch (_b.label) {
                                case 0: return [4 /*yield*/, catalogManager.getFolderContent(undefined)];
                                case 1:
                                    rootItems = _b.sent();
                                    return [4 /*yield*/, Obj.loadObjectData({
                                            image: catalogManager.getImage(),
                                            caption: catalogManager.getTitle(),
                                            path: [sourceId],
                                        })];
                                case 2:
                                    rootContext = _b.sent();
                                    processLevel = function (catalogItems, context) { return __awaiter(void 0, void 0, void 0, function () {
                                        var catalogItems_1, catalogItems_1_1, catalogItem, _a, folder, folderContent, newContext, e_3_1;
                                        var e_3, _b;
                                        return __generator(this, function (_c) {
                                            switch (_c.label) {
                                                case 0:
                                                    _c.trys.push([0, 6, 7, 8]);
                                                    catalogItems_1 = __values(catalogItems), catalogItems_1_1 = catalogItems_1.next();
                                                    _c.label = 1;
                                                case 1:
                                                    if (!!catalogItems_1_1.done) return [3 /*break*/, 5];
                                                    catalogItem = catalogItems_1_1.value;
                                                    if (where(catalogItem)) {
                                                        results.push(__assign(__assign({}, catalogItem), { uuid: sourceId + "." + catalogItem.ID, catalogId: sourceId, Caption: catalogItem.Caption, Image: catalogItem.Image || context.image, path: context.path }));
                                                    }
                                                    if (!(catalogItem.Type === IGXA.EItemType.Folder ||
                                                        catalogItem.Type === IGXA.EItemType.MultiArticle)) return [3 /*break*/, 4];
                                                    return [4 /*yield*/, Obj.loadObjectData({
                                                            folderContent: catalogManager.getFolderContent(catalogItem.ID),
                                                            folder: catalogManager.getFolder(catalogItem.ID),
                                                        })];
                                                case 2:
                                                    _a = _c.sent(), folder = _a.folder, folderContent = _a.folderContent;
                                                    newContext = {
                                                        image: folder.Image || context.image,
                                                        caption: context.caption || folder.Caption,
                                                        path: __spread(context.path, [folder.ID]),
                                                    };
                                                    return [4 /*yield*/, processLevel(folderContent, newContext)];
                                                case 3:
                                                    _c.sent();
                                                    _c.label = 4;
                                                case 4:
                                                    catalogItems_1_1 = catalogItems_1.next();
                                                    return [3 /*break*/, 1];
                                                case 5: return [3 /*break*/, 8];
                                                case 6:
                                                    e_3_1 = _c.sent();
                                                    e_3 = { error: e_3_1 };
                                                    return [3 /*break*/, 8];
                                                case 7:
                                                    try {
                                                        if (catalogItems_1_1 && !catalogItems_1_1.done && (_b = catalogItems_1.return)) _b.call(catalogItems_1);
                                                    }
                                                    finally { if (e_3) throw e_3.error; }
                                                    return [7 /*endfinally*/];
                                                case 8: return [2 /*return*/];
                                            }
                                        });
                                    }); };
                                    if (!(queryDepth === "ROOT")) return [3 /*break*/, 3];
                                    try {
                                        for (rootItems_1 = (e_2 = void 0, __values(rootItems)), rootItems_1_1 = rootItems_1.next(); !rootItems_1_1.done; rootItems_1_1 = rootItems_1.next()) {
                                            rootItem = rootItems_1_1.value;
                                            if (!where(rootItem)) {
                                                continue;
                                            }
                                            results.push(__assign(__assign(__assign({}, rootItem), rootContext), { catalogId: sourceId, uuid: sourceId + "." + rootItem.ID }));
                                        }
                                    }
                                    catch (e_2_1) { e_2 = { error: e_2_1 }; }
                                    finally {
                                        try {
                                            if (rootItems_1_1 && !rootItems_1_1.done && (_a = rootItems_1.return)) _a.call(rootItems_1);
                                        }
                                        finally { if (e_2) throw e_2.error; }
                                    }
                                    return [3 /*break*/, 5];
                                case 3: return [4 /*yield*/, processLevel(rootItems, rootContext)];
                                case 4:
                                    _b.sent();
                                    _b.label = 5;
                                case 5: return [2 /*return*/];
                            }
                        });
                    };
                    _d.label = 1;
                case 1:
                    _d.trys.push([1, 6, 7, 8]);
                    catalogManagerSourceIdMap_1 = __values(catalogManagerSourceIdMap), catalogManagerSourceIdMap_1_1 = catalogManagerSourceIdMap_1.next();
                    _d.label = 2;
                case 2:
                    if (!!catalogManagerSourceIdMap_1_1.done) return [3 /*break*/, 5];
                    _b = catalogManagerSourceIdMap_1_1.value, catalogManager = _b.catalogManager, sourceId = _b.sourceId;
                    return [5 /*yield**/, _loop_1(catalogManager, sourceId)];
                case 3:
                    _d.sent();
                    _d.label = 4;
                case 4:
                    catalogManagerSourceIdMap_1_1 = catalogManagerSourceIdMap_1.next();
                    return [3 /*break*/, 2];
                case 5: return [3 /*break*/, 8];
                case 6:
                    e_1_1 = _d.sent();
                    e_1 = { error: e_1_1 };
                    return [3 /*break*/, 8];
                case 7:
                    try {
                        if (catalogManagerSourceIdMap_1_1 && !catalogManagerSourceIdMap_1_1.done && (_c = catalogManagerSourceIdMap_1.return)) _c.call(catalogManagerSourceIdMap_1);
                    }
                    finally { if (e_1) throw e_1.error; }
                    return [7 /*endfinally*/];
                case 8: return [2 /*return*/, results];
            }
        });
    });
};
/**
 * Tries to get catalog manager(s) for query
 * @param session Active session
 * @param context Defines if query should be performed over all or specific catalog managers
 */
var getCatalogManagersForQuery = function (session, context) {
    var e_4, _a;
    if (context === "ALL_CATALOGS") {
        // get all registered catalog managers
        var sourceIds = session.getRegisteredSourceIds();
        var results = [];
        try {
            for (var sourceIds_1 = __values(sourceIds), sourceIds_1_1 = sourceIds_1.next(); !sourceIds_1_1.done; sourceIds_1_1 = sourceIds_1.next()) {
                var sourceId = sourceIds_1_1.value;
                var manager = session.getCatalogManagerForSourceId(sourceId);
                if (manager !== undefined) {
                    results.push({
                        catalogManager: manager,
                        sourceId: sourceId,
                    });
                }
            }
        }
        catch (e_4_1) { e_4 = { error: e_4_1 }; }
        finally {
            try {
                if (sourceIds_1_1 && !sourceIds_1_1.done && (_a = sourceIds_1.return)) _a.call(sourceIds_1);
            }
            finally { if (e_4) throw e_4.error; }
        }
        return results;
    }
    if (context.type === "comId") {
        var sourceId = session.getArticleSource(context.comId);
        var catalogManager_1 = session.getCatalogManagerForComId(context.comId);
        if (catalogManager_1 !== undefined) {
            return [
                {
                    catalogManager: catalogManager_1,
                    sourceId: sourceId,
                },
            ];
        }
        throw new Error("The source of article \"" + context.comId + "\" doesn't have a registered catalog manager and therefore can not be queried.");
    }
    var catalogManager = session.getCatalogManagerForSourceId(context.sourceId);
    if (catalogManager !== undefined) {
        return [
            {
                catalogManager: catalogManager,
                sourceId: context.sourceId,
            },
        ];
    }
    throw new Error("Source \"" + context.sourceId + "\" doesn't have a registered catalog manager and therefore can not be queried.");
};
