export const CURRENT_VERSION = 1;
export 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 || (EItemType = {}));
export 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 || (EItemVisibility = {}));
export const extractLocalizedText = (text, language = "any") => {
    if (Array.isArray(text)) {
        return text;
    }
    if (typeof text === "object") {
        const localizedText = text[language] || text.any;
        if (!localizedText) {
            return [];
        }
        return extractLocalizedText(localizedText, language);
    }
    return [text];
};
export var TagGroupSortMode;
(function (TagGroupSortMode) {
    TagGroupSortMode["none"] = "none";
    TagGroupSortMode["alphabetically"] = "alphabetically";
    TagGroupSortMode["byIndex"] = "byIndex";
    TagGroupSortMode["byIndexOrAlphabetically"] = "byIndexOrAlphabetically";
})(TagGroupSortMode || (TagGroupSortMode = {}));
const sortBySortIndex = (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;
};
const sortAlphabetically = (language) => (a, b) => {
    // sort rest elements to the end
    if (a.Rest) {
        return 1;
    }
    if (b.Rest) {
        return -1;
    }
    const aCaption = a.Caption !== undefined
        ? extractLocalizedText(a.Caption, language).join("ZZZ")
        : a.Identifier;
    const bCaption = b.Caption !== undefined
        ? extractLocalizedText(b.Caption, language).join("ZZZ")
        : b.Identifier;
    return aCaption.localeCompare(bCaption);
};
export const createTagGroups = (items, sortMode = TagGroupSortMode.byIndex, language) => {
    const groups = new Map();
    for (const item of items) {
        if (item.Tags === undefined || item.Tags.length === 0) {
            let 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;
        }
        for (const tag of item.Tags) {
            let 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);
        }
    }
    const 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);
};
