import { __awaiter, __generator } from "tslib";
import Logger from "../Logger";
import { CatalogCache } from "./Cache";
import { getItemId, isArticleLike, isFolderLike, isSetLike } from "./Util";
var CatalogManager = /** @class */ (function () {
    function CatalogManager(baseSettings) {
        if (baseSettings === void 0) { baseSettings = {}; }
        this.baseSettings = baseSettings;
        this.isArticleLike = isArticleLike;
        this.isFolderLike = isFolderLike;
        this.getItemId = getItemId;
        this.cache = new CatalogCache();
    }
    /**
     * Asynchronous catalog manager creation method.
     *
     * Replaces onInitalizedCallback of Settings
     *
     * @static
     * @param {object} settings
     * @returns {Promise<CatalogManager>}
     * @memberof CatalogManager
     */
    CatalogManager.Create = function (settings) {
        return Promise.reject("Called create method on abstract CatalogManager class");
    };
    /**
     * Search for item with supplied id
     *
     * @param {string} id
     * @returns
     * @memberof CatalogManager
     */
    CatalogManager.prototype.getItem = function (id) {
        return Promise.resolve(this.cache.getItemById(id));
    };
    /**
     * Retrieves the folder item.
     * @param folderID internal ID of the folder.
     * @returns the folder item as Promise
     */
    CatalogManager.prototype.getFolder = function (folderID) {
        var tFolder = this.cache.getItemById(folderID);
        if (!this.isFolderLike(tFolder)) {
            return Promise.resolve(undefined);
        }
        return Promise.resolve(tFolder);
    };
    CatalogManager.prototype.getArticle = function (articleId) {
        var tArticle = this.cache.getItemById(articleId);
        if (!isArticleLike(tArticle)) {
            return Promise.resolve(undefined);
        }
        return Promise.resolve(tArticle);
    };
    CatalogManager.prototype.getSet = function (setID) {
        var tSet = this.cache.getItemById(setID);
        if (!isSetLike(tSet)) {
            return Promise.resolve(undefined);
        }
        return Promise.resolve(tSet);
    };
    /**
     * Retrieves the content of a folder.
     * @param folderID ID of the folder. undefined should retrieve the root folder.
     * @returns {Promise<IGXA.TAnyItem[]>}
     * @memberof ICatalogManager
     */
    CatalogManager.prototype.getFolderContent = function (folderID) {
        var tFolder = this.cache.getItemById(folderID);
        if (!isFolderLike(tFolder)) {
            return Promise.resolve([]);
        }
        return Promise.resolve(tFolder.SubItems);
    };
    /**
     * Searches an article and reports the first folder the article is found in.
     *
     * Resolves with folderID if found. Rejects if none is found
     *
     * @param {string} baseArticleID ID of article that should be searched.
     * @returns {Promise<string>}
     * @memberof ICatalogManager
     */
    CatalogManager.prototype.getFolderOfArticle = function (baseArticleID) {
        var tPath = this.cache.getItemPath(baseArticleID);
        if (!tPath) {
            Logger.warn("No folder found for: " + baseArticleID);
            return Promise.resolve(undefined);
        }
        return Promise.resolve(tPath[tPath.length - 1]);
    };
    /**
     * Retrieves folder path to Item.
     * Doesn't include item id in array, unless it is a folderId.
     *
     * Returns undefined if no path is found
     * @param {string} id Article id
     * @returns {Promise<string[]>} Path to item
     * @memberof ICatalogManager
     */
    CatalogManager.prototype.getItemPath = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            var tResult;
            return __generator(this, function (_a) {
                tResult = this.cache.getItemPath(id);
                if (!tResult) {
                    Logger.warn("Unknown item: " + id);
                    return [2 /*return*/, undefined];
                }
                return [2 /*return*/, tResult];
            });
        });
    };
    /**
     * Retrieves the parent folder item.
     * @param folderID internal ID of the folder.
     * @returns the parent folder item as Promise
     */
    CatalogManager.prototype.getParentFolder = function (folderId) {
        var tFoundPath = this.cache.getItemPath(folderId);
        if (!tFoundPath) {
            // no path found
            return Promise.resolve(undefined);
        }
        var tParentFolder = tFoundPath[tFoundPath.indexOf(folderId) - 1];
        return this.getFolder(tParentFolder);
    };
    /**
     * Searches the catalog.
     *
     * @param {string} searchTerm  text that should be searched.
     * @returns {Promise<IGXA.TAnyItem[]>}
     * @memberof ICatalogManager
     */
    CatalogManager.prototype.searchCatalog = function (searchTerm) {
        var tFoundItems = this.cache.findAllArticlesFor(searchTerm);
        return Promise.resolve(tFoundItems);
    };
    /**
     *  Retrieves the catalog title.
     *
     * @returns {Promise<string>}
     * @memberof CatalogManager
     */
    CatalogManager.prototype.getTitle = function () {
        return Promise.resolve(this._title);
    };
    /**
     * Retrieves the catalog image.
     *
     * @returns {Promise<string>}
     * @memberof CatalogManager
     */
    CatalogManager.prototype.getImage = function () {
        return Promise.resolve(this._image);
    };
    /**
     * Retrieves the addables of an item.
     *
     * @param {string} baseArticleID number of the base article for which addables should be searched.
     * @returns {Promise<IGXA.IArticleItem[]>}
     * @memberof CatalogManager
     */
    CatalogManager.prototype.getAddables = function (baseArticleID) {
        return Promise.resolve([]);
    };
    CatalogManager.prototype.detectUnitializedFolder = function (folder) {
        return folder.SubItems === undefined;
    };
    return CatalogManager;
}());
export { CatalogManager };
export { CatalogCache };
