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