import { __assign, __values } from "tslib";
import { Obj, } from "@intelligentgraphics/ig.utilities";
import { IGXB } from "@intelligentgraphics/igd.core";
import BaseLogger from "../Logger";
var Logger = BaseLogger.createSubLogger("IGXBManager");
/**
 * IGXB Manager class for BOM processing.
 * This is still subject to change and far from complete, use at your own risk.
 */
var IGXBManager = /** @class */ (function () {
    /**
     * Creates an instance of IGXBManager.
     *
     * DO NOT USE THIS DIRECTLY UNLESS YOU KNOW WHAT YOU'RE DOING.
     *
     * Use IGXBManager.Create instead
     *
     * @param {IIGXBManagerSettings} settings
     * @memberof IGXBManager
     */
    function IGXBManager(settings) {
        if (settings.autoGrouping !== true) {
            settings.autoGrouping = false;
        }
        this._settings = settings;
        this.data = settings.data;
    }
    IGXBManager.Create = function (settings) {
        var tManager = new IGXBManager(settings);
        return tManager;
    };
    // private helpers
    IGXBManager.getUUID = function () {
        return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
            var r = (Math.random() * 16) | 0;
            var v = c === "x" ? r : (r & 0x3) | 0x8;
            return v.toString(16);
        });
    };
    IGXBManager.prototype.getAllArticles = function () {
        return this.findByConditions({ ItemType: IGXB.EItemType.Article }, { searchMode: "all" });
    };
    IGXBManager.prototype.removeHeaderCalculation = function (onlyIndex) {
        if (onlyIndex === undefined) {
            this.data.HeaderCalculation = undefined;
        }
        else {
            if (this.data.HeaderCalculation &&
                this.data.HeaderCalculation.length > onlyIndex) {
                this.data.HeaderCalculation.splice(onlyIndex, 1);
            }
        }
    };
    IGXBManager.prototype.addHeaderCalculation = function (entry, beforeIndex) {
        if (!this.data.HeaderCalculation) {
            this.data.HeaderCalculation = [];
        }
        if (beforeIndex === undefined ||
            beforeIndex >= this.data.HeaderCalculation.length) {
            this.data.HeaderCalculation.push(entry);
        }
        else {
            this.data.HeaderCalculation.splice(beforeIndex, 0, entry);
        }
    };
    /**
     * Removes an item.
     *  @param itemId ID of the item. It must not have sub-items.
     *  @returns true when the item has been removed.
     */
    IGXBManager.prototype.removeItem = function (itemId, force) {
        if (force === void 0) { force = false; }
        Logger.info("IGXBManager.removeItem()", itemId);
        var parent = this.getParentItem(itemId);
        var parentSet = parent === undefined ? this.data.Items : this.getSubItems(parent);
        var itemIndex = parentSet.findIndex(function (child) { return child.UID === itemId; });
        var item = parentSet[itemIndex];
        if (!item || itemIndex < 0) {
            return false;
        }
        var itemSubItems = this.getSubItems(item);
        if (!force && itemSubItems.length) {
            return false;
        }
        parentSet.splice(itemIndex, 1);
        if (this.Settings.autoGrouping &&
            parent &&
            parent.ItemType === IGXB.EItemType.Group &&
            !parentSet.length) {
            this.removeItem(parent.UID);
        }
        return true;
    };
    IGXBManager.prototype.addSetItem = function (data, inParentId, beforeId) {
        if (this.Settings.autoGrouping && !inParentId) {
            inParentId = this.findOrCreateAutoGroup(data.Source, inParentId);
        }
        var setItem = __assign(__assign({}, data), { UID: IGXBManager.getUUID(), ItemType: IGXB.EItemType.Set, Items: [] });
        if (inParentId) {
            this.insertItemIntoParent(setItem, inParentId, beforeId);
        }
        else {
            this.insertItemIntoSet(undefined, setItem, beforeId);
        }
        return setItem.UID;
    };
    /** Adds item group at root-level
     * @param caption Optional caption for the group
     * @param beforeId Optional UID of the item of which the group should
     * be inserted before. If not specified, the group will be
     * inserted as last item.
     * @returns UID of the new group item.
     */
    IGXBManager.prototype.addItemGroup = function (caption, beforeId, groupIdentifier) {
        Logger.info("IGXBManager.addItemGroup()", caption, beforeId);
        var tNewItem = {
            UID: IGXBManager.getUUID(),
            ItemType: IGXB.EItemType.Group,
            Caption: caption,
            Items: new Array(),
            Identifier: groupIdentifier,
        };
        var tBeforeIdx;
        if (beforeId !== undefined) {
            tBeforeIdx = this.findItemIndex(beforeId, this.data.Items);
            if (tBeforeIdx === -1) {
                Logger.error("IGXBManager.addItemGroup(): Invalid beforeId", beforeId);
            }
        }
        if (tBeforeIdx !== undefined) {
            this.data.Items.splice(tBeforeIdx, 0, tNewItem);
        }
        else {
            this.data.Items.push(tNewItem);
        }
        return tNewItem.UID;
    };
    /** Adds a text item.
     * @param text Item text
     * @param important Flag this text as important for an emphasized
     *        representation
     * @param inParentId UID of a GroupItem or ArticleItem to place this
     *        item into as sub-position.
     * @param beforeId optional UID of the item of which this item should
     *        be inserted before. If not specified, the item will be
     *        inserted as last item at root level or (if specified) in the
     *        parent's set of sub-items.
     * @returns UID of the new text item.
     */
    IGXBManager.prototype.addTextItem = function (text, important, inParentId, beforeId) {
        Logger.info("IGXBManager.addTextItem()", text, {
            important: important,
            inParentId: inParentId,
            beforeId: beforeId,
        });
        var tNewItem = {
            UID: IGXBManager.getUUID(),
            ItemType: IGXB.EItemType.Text,
            Text: text,
            Important: important,
        };
        var tParentItem;
        if (inParentId) {
            tParentItem = this.findItem(inParentId);
            if (!tParentItem ||
                (tParentItem.ItemType !== IGXB.EItemType.Group &&
                    tParentItem.ItemType !== IGXB.EItemType.Article)) {
                Logger.error("IGXBManager.addTextItem: Invalid BOM Parent Id " +
                    inParentId);
                throw new Error("Invalid BOM Parent Id " + inParentId);
            }
        }
        this.insertItemIntoSet(tParentItem, tNewItem, beforeId);
        return tNewItem.UID;
    };
    // public addSetItem(
    // )
    /** Adds a article item.
     * @param spec ArticleItem specification.
     * @param inParentId UID of a GroupItem or ArticleItem to place this
     * item into as sub-position.
     * @param beforeId optional UID of the item of which this item should
     * be inserted before. If not specified, the item will be
     * inserted as last item at root level or (if specified) in the
     * parent's set of sub-items.
     * @returns UID of the new item.
     */
    IGXBManager.prototype.addArticleItem = function (article, inParentId, beforeId) {
        Logger.info("IGXBManager.addArticleItem()", article, {
            inParentId: inParentId,
            beforeId: beforeId,
        });
        if (this.Settings.autoGrouping && !inParentId && article.IsCommercial) {
            inParentId = this.findOrCreateAutoGroup(article.Source, article.ManufacturerName || article.ManufacturerId, article.ProgramName || article.ProgramId);
        }
        var tNewItem = __assign(__assign({}, article), { UID: IGXBManager.getUUID(), ItemType: IGXB.EItemType.Article, SubItems: [] });
        if (inParentId) {
            this.insertItemIntoParent(tNewItem, inParentId, beforeId);
        }
        else {
            this.insertItemIntoSet(undefined, tNewItem, beforeId);
        }
        return tNewItem.UID;
    };
    IGXBManager.prototype.addImpressionItem = function (impression, inParentId, beforeId) {
        var newItem = __assign(__assign({}, impression), { UID: IGXBManager.getUUID(), ItemType: IGXB.EItemType.Impression });
        var tParentItem;
        if (inParentId) {
            tParentItem = this.findItem(inParentId);
            if (!tParentItem ||
                (tParentItem.ItemType !== IGXB.EItemType.Group &&
                    tParentItem.ItemType !== IGXB.EItemType.Article)) {
                Logger.error("Invalid BOM Parent ID " + inParentId);
                throw new Error("Invalid BOM Parent ID " + inParentId);
            }
        }
        this.insertItemIntoSet(tParentItem, newItem, beforeId);
        return newItem.UID;
    };
    IGXBManager.prototype.moveItem = function (itemId, inParentId, beforeId) {
        var currentParent = this.getParentItem(itemId);
        var currentSet = currentParent
            ? this.getSubItems(currentParent)
            : this.data.Items;
        var currentIndex = currentSet.findIndex(function (entry) { return entry.UID === itemId; });
        var item = currentSet[currentIndex];
        currentSet.splice(currentIndex, 1);
        var newParent = inParentId === undefined ? undefined : this.getItem(inParentId);
        return this.insertItemIntoSet(newParent, item, beforeId);
    };
    IGXBManager.prototype.copyItem = function (input, inParentId, beforeId) {
        var _this = this;
        var tR;
        var tOldItem = typeof input === "string" ? this.getItem(input) : input;
        var tNewItem;
        var tParentItem;
        var tBeforeItem;
        var regenUIDs = function (itemSet) {
            var e_1, _a;
            try {
                for (var itemSet_1 = __values(itemSet), itemSet_1_1 = itemSet_1.next(); !itemSet_1_1.done; itemSet_1_1 = itemSet_1.next()) {
                    var item = itemSet_1_1.value;
                    item.UID = IGXBManager.getUUID();
                    regenUIDs(_this.getSubItems(item));
                }
            }
            catch (e_1_1) { e_1 = { error: e_1_1 }; }
            finally {
                try {
                    if (itemSet_1_1 && !itemSet_1_1.done && (_a = itemSet_1.return)) _a.call(itemSet_1);
                }
                finally { if (e_1) throw e_1.error; }
            }
        };
        if (!tOldItem) {
            Logger.error("IGXBManager.copyItem() failed", {
                item: tNewItem,
                toParent: tParentItem,
                beforeItem: tBeforeItem,
            });
            return undefined;
        }
        if (tOldItem.ItemType === IGXB.EItemType.Article ||
            tOldItem.ItemType === IGXB.EItemType.Impression) {
            // add item to the correct group
            var groupIdentifier = tOldItem.ManufacturerId + "." + tOldItem.ProgramId;
            var existingGroup = this.findByConditions({
                ItemType: IGXB.EItemType.Group,
                Identifier: groupIdentifier,
            });
            if (existingGroup) {
                inParentId = existingGroup.UID;
            }
            else {
                // create new group
                var tName = tOldItem.ManufacturerName || "";
                if (tName.length === 0) {
                    tName = tOldItem.ManufacturerId;
                }
                var tProg = tOldItem.ProgramName || "";
                if (tProg.length === 0) {
                    tProg = tOldItem.ProgramId;
                }
                inParentId = this.addItemGroup(tName + " " + tProg, undefined, groupIdentifier);
            }
        }
        tParentItem =
            inParentId === undefined ? undefined : this.getItem(inParentId);
        if (tParentItem) {
            if (tParentItem.ItemType !== IGXB.EItemType.Article &&
                tParentItem.ItemType !== IGXB.EItemType.Group) {
                tParentItem = undefined;
            }
        }
        tBeforeItem =
            beforeId === undefined ? undefined : this.getItem(beforeId);
        if (tBeforeItem === tOldItem) {
            tBeforeItem = undefined;
        }
        Logger.info("IGXBManager.copyItem()", {
            item: tOldItem,
            toParent: tParentItem,
            beforeItem: tBeforeItem,
        });
        var tNewParentSet = this.data.Items;
        if (tParentItem) {
            if (tParentItem.ItemType === IGXB.EItemType.Article) {
                tNewParentSet = tParentItem.SubItems;
            }
            else if (tParentItem.ItemType === IGXB.EItemType.Group) {
                tNewParentSet = tParentItem.Items;
            }
        }
        if (tNewParentSet) {
            tNewItem = Obj.deepClone(tOldItem);
            regenUIDs([tNewItem]);
            var tBeforeIdx = void 0;
            if (tBeforeItem) {
                tBeforeIdx = this.findItemIndex(beforeId, tNewParentSet);
            }
            if (tBeforeIdx !== undefined) {
                tNewParentSet.splice(tBeforeIdx, 0, tNewItem);
            }
            else {
                tNewParentSet.push(tNewItem);
            }
            tR = tNewItem.UID;
        }
        else {
            Logger.error("IGXBManager.copyItem() could find parent set", {
                item: tOldItem,
                toParent: tParentItem,
                beforeItem: tBeforeItem,
            });
        }
        return tR;
    };
    /** Returns the BOM position string of an item.
     *  @param itemId ID of the item.
     *  @returns The position string, like "1" or "3.2.1" or an empty
     * string, if this item is not an ArticleItem.
     */
    IGXBManager.prototype.getPositionString = function (itemId) {
        var e_2, _a, e_3, _b;
        var _this = this;
        var articles = [];
        try {
            // collect all articles, which are either located in the root level or under a root level group
            for (var _c = __values(this.data.Items), _d = _c.next(); !_d.done; _d = _c.next()) {
                var item = _d.value;
                if (item.ItemType === IGXB.EItemType.Text) {
                    continue;
                }
                if (item.ItemType === IGXB.EItemType.Group) {
                    try {
                        for (var _e = (e_3 = void 0, __values(item.Items)), _f = _e.next(); !_f.done; _f = _e.next()) {
                            var subItem = _f.value;
                            if (subItem.ItemType === IGXB.EItemType.Article) {
                                articles.push(subItem);
                            }
                        }
                    }
                    catch (e_3_1) { e_3 = { error: e_3_1 }; }
                    finally {
                        try {
                            if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
                        }
                        finally { if (e_3) throw e_3.error; }
                    }
                }
                if (item.ItemType === IGXB.EItemType.Set) {
                    articles.push(item);
                }
                if (item.ItemType === IGXB.EItemType.Article) {
                    articles.push(item);
                }
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
            }
            finally { if (e_2) throw e_2.error; }
        }
        var parts = [];
        var buildParts = function (items) {
            // keep track of skipped textItems so the count of these can later be used to create and article only index
            var skipped = 0;
            for (var index = 0; index < items.length; index++) {
                var item = items[index];
                if (item.ItemType === IGXB.EItemType.Text || item.Hidden) {
                    skipped++;
                    continue;
                }
                if (item.UID === itemId) {
                    // found the required item. return true to let parent know, that searching is done
                    parts.push(index - skipped + 1);
                    return true;
                }
                var subItems = _this.getSubItems(item);
                var result = buildParts(subItems);
                if (result) {
                    parts.push(index - skipped + 1);
                    return true;
                }
            }
        };
        buildParts(articles);
        // indicies need to be reversed, since they get added depth-first
        return parts.reverse().join(".");
    };
    /**
     * Returns the accumulated price for one position, including subitems and additional prices
     */
    // public getItemTotal(itemId: string): IGXB.IPrice {
    // 	const price: IGXB.IPrice = {
    // 		Currency: undefined,
    // 		Value: 0,
    // 	};
    // 	const item = this.findItem(itemId);
    // 	if (!item) {
    // 		return price;
    // 	}
    // 	if (item.ItemType === IGXB.EItemType.Article && item.IsCommercial) {
    // 		price.Currency = price.Currency || item.Currency;
    // 		if (typeof item.SinglePrice === "number") {
    // 			price.Value +=
    // 				item.SinglePrice *
    // 				(item.Quantity === undefined ? 1 : item.Quantity);
    // 		} else {
    // 			Logger.warn("Commercial article has no price", item);
    // 		}
    // 	}
    // 	price.Value += this.getSubTotal(this.getSubItems(item)).amount;
    // 	return price;
    // }
    IGXBManager.prototype.getSubTotal = function (items) {
        var e_4, _a;
        var subTotal = 0;
        var currency;
        try {
            for (var items_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.ItemType === IGXB.EItemType.Set) {
                    if (typeof item.SinglePrice === "number") {
                        currency = item.Currency;
                        subTotal += item.SinglePrice;
                    }
                    continue;
                }
                if (item.ItemType === IGXB.EItemType.Article && item.IsCommercial) {
                    if (typeof item.SinglePrice === "number") {
                        currency = item.Currency;
                        subTotal +=
                            item.SinglePrice *
                                (item.Quantity === undefined ? 1 : item.Quantity);
                    }
                    else {
                        Logger.warn("Commercial article has no price", item);
                    }
                }
                var _b = this.getSubTotal(this.getSubItems(item)), subCurrency = _b.currency, amount = _b.amount;
                if (currency === undefined) {
                    currency = subCurrency;
                }
                subTotal += amount;
            }
        }
        catch (e_4_1) { e_4 = { error: e_4_1 }; }
        finally {
            try {
                if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
            }
            finally { if (e_4) throw e_4.error; }
        }
        return { amount: subTotal, currency: currency };
    };
    IGXBManager.prototype.getParentSet = function (item) {
        var parent = this.getParentItem(item);
        if (!parent) {
            return this.Settings.data.Items;
        }
        return this.getSubItems(parent);
    };
    IGXBManager.prototype.getParentItem = function (item) {
        var id = typeof item === "string" ? item : item.UID;
        return this.findByConditions({ UID: id }, { searchMode: "parent" });
    };
    /**
     * Finds the parent ItemGroup of an item.
     * @param item: The item whose parent should be returned.
     * @returns: The parent ItemGroup or undefined, if the item has no parent group
     */
    IGXBManager.prototype.getParentGroupItem = function (item) {
        var id = typeof item === "string" ? item : item.UID;
        return this.findByConditions({ UID: id }, { searchMode: "parent" });
    };
    /**
     * Finds the parent ArticleItem of an item.
     * @param item: The item whose parent should be returned.
     * @returns: The parent ArticleItem or undefined, if the item has no parent Article Item
     */
    IGXBManager.prototype.getParentArticleItem = function (item) {
        var id = typeof item === "string" ? item : item.UID;
        return this.findByConditions({ UID: id }, { searchMode: "parent" });
    };
    IGXBManager.prototype.getItem = function (itemID) {
        return this.findItem(itemID);
    };
    /** Returns the IGXB article item with the given commercial ID */
    IGXBManager.prototype.getItemByComId = function (comId) {
        var found = this.findByConditions({
            ComId: comId,
        });
        if (!found ||
            (found.ItemType !== IGXB.EItemType.Article &&
                found.ItemType !== IGXB.EItemType.Impression &&
                found.ItemType !== IGXB.EItemType.Set &&
                found.ItemType !== IGXB.EItemType.HeadData)) {
            return undefined;
        }
        return found;
    };
    /** returns a flat list of IGXB article items that match the given source */
    IGXBManager.prototype.getItemsBySource = function (source) {
        return this.findByConditions({ Source: source, ItemType: IGXB.EItemType.Article }, { searchMode: "all" });
    };
    Object.defineProperty(IGXBManager.prototype, "IGXB", {
        get: function () {
            return this.data;
        },
        set: function (newIGXB) {
            // TODO: consistency-check, maybe remove gfx-ids, ...
            this.data = newIGXB;
        },
        enumerable: true,
        configurable: true
    });
    /** Creates a new BOM structure
     * @param priceType: IGXB price type (None, Net, Gross)
     * @param vat: VAT in percent must be given when price type is Gross
     */
    IGXBManager.createNewIGXB = function (priceType, vat) {
        if (priceType === IGXB.EPriceType.Gross && vat === undefined) {
            throw new Error("VAT must be given when priceType is Gross");
        }
        return {
            FormatVersion: "" + IGXB.CurrentMajorVersion + "." + IGXB.CurrentMinorVersion,
            PriceType: priceType,
            VAT: vat,
            Items: [],
            ProjectData: {
                Id: this.getUUID(),
                Title: "A new Project",
                Creation: new Date().toUTCString(),
            },
            CustomData: {},
        };
    };
    Object.defineProperty(IGXBManager.prototype, "Settings", {
        get: function () {
            return this._settings;
        },
        enumerable: true,
        configurable: true
    });
    IGXBManager.prototype.findItem = function (uid, items, searchMode) {
        if (searchMode === void 0) { searchMode = "item"; }
        if (!uid) {
            return undefined;
        }
        return this.findByConditions({ UID: uid }, { itemSet: items, searchMode: searchMode });
    };
    IGXBManager.prototype.findByConditions = function (conditions, options) {
        var _this = this;
        if (options === void 0) { options = {}; }
        var searchMode = options.searchMode || "item";
        var itemSet = options.itemSet || this.data.Items;
        if (!conditions) {
            return searchMode === "all" ? [] : undefined;
        }
        var matches;
        if (typeof conditions === "object") {
            var conditionProps_1 = Object.getOwnPropertyNames(conditions);
            if (!conditionProps_1.length) {
                return searchMode === "all" ? [] : undefined;
            }
            matches = function (item) {
                return conditionProps_1.every(function (prop) { return item[prop] === conditions[prop]; });
            };
        }
        else {
            matches = conditions;
        }
        var foundItems = [];
        var recurse = function (entries) {
            var e_5, _a;
            if (!entries) {
                return undefined;
            }
            try {
                for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) {
                    var item = entries_1_1.value;
                    if (matches(item)) {
                        if (searchMode === "all") {
                            foundItems.push(item);
                            recurse(_this.getSubItems(item));
                            continue;
                        }
                        return searchMode === "item" ? item : true;
                    }
                    if (item.ItemType === IGXB.EItemType.Text) {
                        continue;
                    }
                    var subItems = _this.getSubItems(item);
                    var res = recurse(subItems);
                    if (res === true) {
                        return item;
                    }
                    if (res) {
                        return res;
                    }
                }
            }
            catch (e_5_1) { e_5 = { error: e_5_1 }; }
            finally {
                try {
                    if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1);
                }
                finally { if (e_5) throw e_5.error; }
            }
            return undefined;
        };
        var result = recurse(itemSet);
        if (result === true) {
            return undefined;
        }
        if (searchMode === "all") {
            return foundItems;
        }
        return result;
    };
    IGXBManager.prototype.getSubItems = function (item) {
        if (item === undefined) {
            return this.data.Items;
        }
        if (item.ItemType === IGXB.EItemType.Article) {
            return item.SubItems || [];
        }
        if (item.ItemType === IGXB.EItemType.Group ||
            item.ItemType === IGXB.EItemType.Set) {
            return item.Items;
        }
        return [];
    };
    /** Finds the index of an item in a given item set.
     *  @param uid: ID of an item (any type)
     *  @param items: list of items to be searched
     *  @returns: zero-based index of the item in the set or undefined if
     * the item is not part of the set.
     */
    IGXBManager.prototype.findItemIndex = function (uid, items) {
        return items.findIndex(function (item) { return item.UID === uid; });
    };
    IGXBManager.prototype.insertItemIntoParent = function (item, parentId, beforeId) {
        var tParentItem = this.findItem(parentId);
        if (!tParentItem ||
            (tParentItem.ItemType !== IGXB.EItemType.Group &&
                tParentItem.ItemType !== IGXB.EItemType.Article &&
                tParentItem.ItemType !== IGXB.EItemType.Set)) {
            Logger.error("Invalid BOM Parent ID " + parentId);
            throw new Error("Invalid BOM Parent ID " + parentId);
        }
        if (!this.insertItemIntoSet(tParentItem, item, beforeId)) {
            throw new Error("Could not insert item into set");
        }
        return true;
    };
    IGXBManager.prototype.insertItemIntoSet = function (newParent, item, beforeId) {
        var newSet;
        if (newParent === undefined) {
            newSet = this.data.Items;
        }
        else if (newParent.ItemType === IGXB.EItemType.Article) {
            newParent.SubItems = newParent.SubItems || [];
            newSet = newParent.SubItems;
        }
        else if (newParent.ItemType === IGXB.EItemType.Group ||
            newParent.ItemType === IGXB.EItemType.Set) {
            newParent.Items = newParent.Items || [];
            newSet = newParent.Items;
        }
        else {
            return false;
        }
        var insertIndex = newSet.findIndex(function (entry) { return entry.UID === beforeId; });
        if (insertIndex < 0) {
            newSet.push(item);
            return true;
        }
        var oldLength = newSet.length;
        newSet.splice(insertIndex, 0, item);
        if (newSet.length <= oldLength) {
            return false;
        }
        return true;
    };
    IGXBManager.prototype.findOrCreateAutoGroup = function (source, manufacturerName, programName) {
        if (manufacturerName === void 0) { manufacturerName = ""; }
        if (programName === void 0) { programName = ""; }
        var existingGroup = this.findByConditions({
            ItemType: IGXB.EItemType.Group,
            Identifier: source,
        });
        if (existingGroup) {
            return existingGroup.UID;
        }
        return this.addItemGroup(manufacturerName + " " + programName, undefined, source);
    };
    return IGXBManager;
}());
export { IGXBManager };
