import { IGXA } from "@intelligentgraphics/igd.core";
import { isArticleLike, isFolderLike } from "./Util";
export class CatalogCache {
    constructor() {
        this.rootItem = {
            ID: CatalogCache.rootItemName,
            SubItems: [],
            Type: IGXA.EItemType.Folder,
            Caption: [],
        };
        this.allItems = {
            [CatalogCache.rootItemName]: this.rootItem,
        };
        this.itemPaths = {};
    }
    static fixId(input) {
        return input === undefined ? CatalogCache.rootItemName : input;
    }
    add(path, item, recursive = true) {
        this.allItems[item.ID] = item;
        this.itemPaths[item.ID] = path;
        if (!isFolderLike(item)) {
            return;
        }
        if (item.SubItems && recursive) {
            item.SubItems.forEach(subItem => this.add([...path, item.ID], subItem, recursive));
        }
        return;
    }
    addSubItems(path, items) {
        const tItem = this.allItems[path[path.length - 1]];
        if (!isFolderLike(tItem)) {
            return;
        }
        tItem.SubItems = items;
        items.forEach(item => this.add(path, item, true));
    }
    /**
     * Add root items
     *
     * @param {IGXA.TAnyItem[]} items
     * @param {boolean} [recursive=true]
     * @memberof CatalogCache
     */
    setRootItems(items, recursive = true) {
        items.forEach(item => this.add([], item, recursive));
        this.rootItem.SubItems = items;
    }
    parseIGXA(igxa) {
        this.setRootItems(igxa.Items, true);
    }
    getItemById(id) {
        return this.allItems[CatalogCache.fixId(id)];
    }
    getItemPath(id) {
        return this.itemPaths[CatalogCache.fixId(id)];
    }
    findAllArticlesFor(searchTerm) {
        const tTerm = searchTerm.toUpperCase();
        return Object.getOwnPropertyNames(this.allItems)
            .filter(id => {
            const tItem = this.allItems[id];
            if (!isArticleLike(tItem)) {
                return false;
            }
            return CatalogCache.searchFields.some(field => {
                const tText = tItem[field];
                const tNormalizedText = this.normalizeText(tText);
                return tNormalizedText.some(text => text.toUpperCase().indexOf(tTerm) > -1);
            });
        })
            .map(id => this.allItems[id]);
    }
    normalizeText(text) {
        if (Array.isArray(text)) {
            return text;
        }
        if (typeof text === "object") {
            const tNormalizedText = [];
            Object.getOwnPropertyNames(text).forEach(prop => {
                if (typeof text[prop] === "object") {
                    tNormalizedText.push(...Text[prop]);
                }
                else {
                    tNormalizedText.push(text[prop]);
                }
            });
            return tNormalizedText;
        }
        return [text];
    }
}
CatalogCache.rootItemName = "__root__";
CatalogCache.searchFields = [
    "Description",
    "Caption",
];
