"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.CdmTraitCollection = void 0;
const util_1 = require("util");
const internal_1 = require("../internal");
/**
 * CdmCollection customized for CdmTraitReferenceBase.
 */
class CdmTraitCollection extends internal_1.CdmCollection {
    /**
     * Constructs a CdmTraitCollection by using the parent constructor and TraitRef as the default type.
     * @param ctx The context.
     * @param owner The object this collection is a member of.
     */
    constructor(ctx, owner) {
        super(ctx, owner, internal_1.cdmObjectType.traitRef);
    }
    insert(index, traitReference) {
        this.clearCache();
        super.insert(index, traitReference);
    }
    push(trait, simpleRefOrArgs = undefined) {
        this.clearCache();
        if (simpleRefOrArgs === undefined || (simpleRefOrArgs !== undefined && typeof simpleRefOrArgs === 'boolean')) {
            let simpleRef = simpleRefOrArgs !== undefined ? simpleRefOrArgs : false;
            if (typeof trait === 'string') {
                return super.push(trait, simpleRef);
            }
            else if (internal_1.isCdmTraitDefinition(trait)) {
                return super.push(new internal_1.CdmTraitReference(this.ctx, trait, simpleRef, false));
            }
            else if (internal_1.isCdmTraitGroupDefinition(trait)) {
                return super.push(new internal_1.CdmTraitGroupReference(this.ctx, trait, simpleRef));
            }
            else {
                return super.push(trait);
            }
        }
        else {
            if (typeof trait === 'string') {
                const traitRef = super.push(trait);
                if (traitRef === undefined || simpleRefOrArgs === undefined) {
                    return traitRef;
                }
                for (const tupleList of simpleRefOrArgs) {
                    traitRef.arguments.push(tupleList[0], tupleList[1]);
                }
                return traitRef;
            }
            return undefined;
        }
    }
    remove(trait, onlyFromProperty) {
        const index = this.indexOf(trait, onlyFromProperty);
        if (index !== -1) {
            this.clearCache();
            this.propagateInDocument(this.allItems[index], undefined);
            this.allItems.splice(index, 1);
            return true;
        }
        return false;
    }
    concat(traitList) {
        for (const trait of traitList) {
            this.push(trait);
        }
    }
    removeAt(index) {
        this.clearCache();
        super.removeAt(index);
    }
    /**
     * Returns a new collection consisting of only the trait reference objects present in this collection.
     * @returns New collection of found trait reference objects
     */
    toTraitRefs() {
        let traitCollection = new internal_1.CdmCollection(this.ctx, this.owner, internal_1.cdmObjectType.traitRef);
        traitCollection.concat(this.allItems.filter(x => x instanceof internal_1.CdmTraitReference));
        return traitCollection;
    }
    /**
     * Returns a new collection consisting of only the trait group reference objects present in this collection.
     * @returns New collection of found trait group reference objects
     */
    toTraitGroupRefs() {
        let traitGroupCollection = new internal_1.CdmCollection(this.ctx, this.owner, internal_1.cdmObjectType.traitGroupRef);
        traitGroupCollection.concat(this.allItems.filter(x => x instanceof internal_1.CdmTraitGroupReference));
        return traitGroupCollection;
    }
    indexOf(obj, onlyFromProperty = false) {
        if (!util_1.isString(obj)) {
            return this.indexOf(obj.fetchObjectDefinitionName(), onlyFromProperty);
        }
        let indexOfTraitNotFromProperty = -1;
        for (let index = 0; index < this.length; index++) {
            if (this.corresponds(this.allItems[index], obj)) {
                if (this.allItems[index] instanceof internal_1.CdmTraitGroupReference || this.allItems[index].isFromProperty) {
                    return index;
                }
                indexOfTraitNotFromProperty = index;
            }
        }
        if (!onlyFromProperty) {
            return indexOfTraitNotFromProperty;
        }
        return -1;
    }
    clear() {
        this.clearCache();
        super.clear();
    }
    clearCache() {
        const ownerAsCdmObjectBase = this.owner;
        if (ownerAsCdmObjectBase) {
            ownerAsCdmObjectBase.clearTraitCache();
        }
    }
    corresponds(obj, traitName) {
        return obj.fetchObjectDefinitionName() === traitName;
    }
}
exports.CdmTraitCollection = CdmTraitCollection;
//# sourceMappingURL=CdmTraitCollection.js.map