"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.CdmCollection = void 0;
class CdmCollection {
    constructor(ctx, owner, defaultType) {
        this.ctx = ctx;
        this.owner = owner;
        this.allItems = [];
        this.defaultType = defaultType;
    }
    get owner() {
        return this._owner;
    }
    set owner(value) {
        this._owner = value;
    }
    get length() {
        return this.allItems.length;
    }
    /**
     * Add an element to CdmCollection.
     * @param obj If this parameter is a string, a new object with this name will be created and added to the collection.
     * This parameter can also be an element to be added to the cdmCollection or a list of elements to be added to the collection.
     * @param simpleRef Parameter only used if function creates a new elementn,
     * and is only used for certain types by the constructor of the object to be added.
     * @returns If an item was created, it returns the newly created item. Otherwise doesn't return anything.
     */
    push(obj, simpleRef = false) {
        if (typeof obj === 'string' || obj === undefined) {
            const newObj = this.ctx.corpus.MakeObject(this.defaultType, obj, simpleRef);
            return this.push(newObj);
        }
        else {
            this.makeDocumentDirty();
            const currObject = obj;
            currObject.owner = this.owner;
            this.propagateInDocument(currObject, this.owner.inDocument);
            this.allItems.push(currObject);
            return currObject;
        }
    }
    insert(index, item) {
        item.owner = this.owner;
        this.propagateInDocument(item, this.owner.inDocument);
        this.makeDocumentDirty();
        this.allItems.splice(index, 0, item);
    }
    concat(list) {
        for (const elem of list) {
            this.push(elem);
        }
    }
    remove(currObject) {
        const index = this.allItems.indexOf(currObject);
        if (index > -1) {
            this.removeAt(index);
            return true;
        }
        return false;
    }
    removeAt(index) {
        if (index >= 0 && index < this.length) {
            this.allItems[index].owner = undefined;
            this.propagateInDocument(this.allItems[index], undefined);
            this.makeDocumentDirty();
            this.allItems.splice(index, 1);
        }
    }
    item(name) {
        return this.allItems.find((x) => x.fetchObjectDefinitionName() === name);
    }
    visitArray(path, preChildren, postChildren) {
        let result = false;
        if (this.allItems) {
            const lItem = this.allItems.length;
            for (let iItem = 0; iItem < lItem; iItem++) {
                const element = this.allItems[iItem];
                if (element) {
                    if (element.visit(path, preChildren, postChildren)) {
                        result = true;
                        break;
                    }
                }
            }
        }
        return result;
    }
    clear() {
        for (const item of this.allItems) {
            item.owner = undefined;
            this.propagateInDocument(item, undefined);
        }
        this.makeDocumentDirty();
        this.allItems = [];
    }
    [Symbol.iterator]() {
        return this.allItems[Symbol.iterator]();
    }
    makeDocumentDirty() {
        if (!this.ctx.corpus.isCurrentlyResolving) {
            const document = this.owner && this.owner.inDocument ? this.owner.inDocument : this.owner;
            if (document) {
                document.isDirty = true;
                document.needsIndexing = true;
            }
        }
    }
    propagateInDocument(cdmObject, document) {
        if (!this.ctx.corpus.isCurrentlyResolving) {
            cdmObject.visit('', (iObject, path) => {
                // If object's document is already the same as the one we're trying to set
                // then we're assuming that every sub-object is also set to it, so bail out.
                if (iObject.inDocument === document) {
                    return true;
                }
                iObject.inDocument = document;
                return false;
            }, undefined);
        }
    }
}
exports.CdmCollection = CdmCollection;
//# sourceMappingURL=CdmCollection.js.map