"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
exports.CURRENT_VERSION = 1;
var EItemType;
(function (EItemType) {
    EItemType[EItemType["Folder"] = 0] = "Folder";
    EItemType[EItemType["ArticleGfx"] = 1] = "ArticleGfx";
    EItemType[EItemType["ArticleCom"] = 2] = "ArticleCom";
    EItemType[EItemType["MultiArticle"] = 3] = "MultiArticle";
    EItemType[EItemType["Impression"] = 4] = "Impression";
    EItemType[EItemType["Set"] = 5] = "Set";
    EItemType[EItemType["Catalog"] = 6] = "Catalog";
    EItemType[EItemType["HeadData"] = 7] = "HeadData";
    EItemType[EItemType["Wizard"] = 8] = "Wizard";
})(EItemType = exports.EItemType || (exports.EItemType = {}));
var EItemVisibility;
(function (EItemVisibility) {
    EItemVisibility[EItemVisibility["Always"] = 0] = "Always";
    EItemVisibility[EItemVisibility["OnlySearch"] = 1] = "OnlySearch";
    EItemVisibility[EItemVisibility["OnlyAddables"] = 2] = "OnlyAddables";
    EItemVisibility[EItemVisibility["OnlyMultiArticles"] = 3] = "OnlyMultiArticles";
    EItemVisibility[EItemVisibility["None"] = 4] = "None";
})(EItemVisibility = exports.EItemVisibility || (exports.EItemVisibility = {}));
exports.extractLocalizedText = function (text, language) {
    if (language === void 0) { language = "any"; }
    if (Array.isArray(text)) {
        return text;
    }
    if (typeof text === "object") {
        var localizedText = text[language] || text.any;
        if (!localizedText) {
            return [];
        }
        return exports.extractLocalizedText(localizedText, language);
    }
    return [text];
};
var TagGroupSortMode;
(function (TagGroupSortMode) {
    TagGroupSortMode["none"] = "none";
    TagGroupSortMode["alphabetically"] = "alphabetically";
    TagGroupSortMode["byIndex"] = "byIndex";
    TagGroupSortMode["byIndexOrAlphabetically"] = "byIndexOrAlphabetically";
})(TagGroupSortMode = exports.TagGroupSortMode || (exports.TagGroupSortMode = {}));
var sortBySortIndex = function (a, b) {
    // sort rest elements to the end
    if (a.Rest) {
        return 1;
    }
    if (b.Rest) {
        return -1;
    }
    // sort elements with a sort index above those without one
    if (a.SortIndex === undefined) {
        if (b.SortIndex === undefined) {
            return 0;
        }
        return 1;
    }
    if (b.SortIndex === undefined) {
        return -1;
    }
    return a.SortIndex - b.SortIndex;
};
var sortAlphabetically = function (language) { return function (a, b) {
    // sort rest elements to the end
    if (a.Rest) {
        return 1;
    }
    if (b.Rest) {
        return -1;
    }
    var aCaption = a.Caption !== undefined
        ? exports.extractLocalizedText(a.Caption, language).join("ZZZ")
        : a.Identifier;
    var bCaption = b.Caption !== undefined
        ? exports.extractLocalizedText(b.Caption, language).join("ZZZ")
        : b.Identifier;
    return aCaption.localeCompare(bCaption);
}; };
exports.createTagGroups = function (items, sortMode, language) {
    var e_1, _a, e_2, _b;
    if (sortMode === void 0) { sortMode = TagGroupSortMode.byIndex; }
    var groups = new Map();
    try {
        for (var items_1 = tslib_1.__values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
            var item = items_1_1.value;
            if (item.Tags === undefined || item.Tags.length === 0) {
                var untaggedGroup = groups.get("UNTAGGED");
                if (untaggedGroup === undefined) {
                    untaggedGroup = {
                        Identifier: "UNTAGGED",
                        Caption: "Untagged",
                        Rest: true,
                        Items: [],
                        SortIndex: undefined,
                    };
                    groups.set("UNTAGGED", untaggedGroup);
                }
                untaggedGroup.Items.push(item);
                continue;
            }
            try {
                for (var _c = (e_2 = void 0, tslib_1.__values(item.Tags)), _d = _c.next(); !_d.done; _d = _c.next()) {
                    var tag = _d.value;
                    var group = groups.get(tag.Identifier);
                    if (group === undefined) {
                        group = {
                            Identifier: tag.Identifier,
                            Caption: tag.Caption,
                            SortIndex: tag.SortIndex,
                            Items: [],
                            Rest: false,
                        };
                        groups.set(tag.Identifier, group);
                    }
                    group.Items.push(item);
                }
            }
            catch (e_2_1) { e_2 = { error: e_2_1 }; }
            finally {
                try {
                    if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
                }
                finally { if (e_2) throw e_2.error; }
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    var entries = Array.from(groups.values());
    if (entries.length === 0 || sortMode === TagGroupSortMode.none) {
        return entries;
    }
    if (sortMode === TagGroupSortMode.alphabetically) {
        return entries.sort(sortAlphabetically(language));
    }
    if (sortMode === TagGroupSortMode.byIndexOrAlphabetically) {
        if (entries[0].SortIndex === undefined) {
            return entries.sort(sortAlphabetically(language));
        }
    }
    return entries.sort(sortBySortIndex);
};
