import { Obj } from "@intelligentgraphics/ig.utilities";
import { IGXA } from "@intelligentgraphics/igd.core";
/**
 * Tries to perform parametrized query to get desired catalog items
 */
export const queryCatalogs = async ({ queryDepth, session, context, where, }) => {
    const results = [];
    const catalogManagerSourceIdMap = getCatalogManagersForQuery(session, context);
    for (const { catalogManager, sourceId } of catalogManagerSourceIdMap) {
        const rootItems = await catalogManager.getFolderContent(undefined);
        const rootContext = await Obj.loadObjectData({
            image: catalogManager.getImage(),
            caption: catalogManager.getTitle(),
            path: [sourceId],
        });
        const processLevel = async (catalogItems, context) => {
            for (const catalogItem of catalogItems) {
                if (where(catalogItem)) {
                    results.push({
                        ...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) {
                    const { folder, folderContent } = await Obj.loadObjectData({
                        folderContent: catalogManager.getFolderContent(catalogItem.ID),
                        folder: catalogManager.getFolder(catalogItem.ID),
                    });
                    const newContext = {
                        image: folder.Image || context.image,
                        caption: context.caption || folder.Caption,
                        path: [...context.path, folder.ID],
                    };
                    await processLevel(folderContent, newContext);
                }
            }
        };
        if (queryDepth === "ROOT") {
            for (const rootItem of rootItems) {
                if (!where(rootItem)) {
                    continue;
                }
                results.push({
                    ...rootItem,
                    ...rootContext,
                    catalogId: sourceId,
                    uuid: `${sourceId}.${rootItem.ID}`,
                });
            }
        }
        else {
            await processLevel(rootItems, rootContext);
        }
    }
    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
 */
const getCatalogManagersForQuery = (session, context) => {
    if (context === "ALL_CATALOGS") {
        // get all registered catalog managers
        const sourceIds = session.getRegisteredSourceIds();
        const results = [];
        for (const sourceId of sourceIds) {
            const manager = session.getCatalogManagerForSourceId(sourceId);
            if (manager !== undefined) {
                results.push({
                    catalogManager: manager,
                    sourceId,
                });
            }
        }
        return results;
    }
    if (context.type === "comId") {
        const sourceId = session.getArticleSource(context.comId);
        const catalogManager = session.getCatalogManagerForComId(context.comId);
        if (catalogManager !== undefined) {
            return [
                {
                    catalogManager,
                    sourceId: sourceId,
                },
            ];
        }
        throw new Error(`The source of article "${context.comId}" doesn't have a registered catalog manager and therefore can not be queried.`);
    }
    const catalogManager = session.getCatalogManagerForSourceId(context.sourceId);
    if (catalogManager !== undefined) {
        return [
            {
                catalogManager,
                sourceId: context.sourceId,
            },
        ];
    }
    throw new Error(`Source "${context.sourceId}" doesn't have a registered catalog manager and therefore can not be queried.`);
};
