"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.removeTraitRef = exports.fetchTraitReferenceIndex = exports.fetchTraitRefName = exports.addTraitRef = void 0;
const internal_1 = require("../internal");
function addTraitRef(ctx, collection, traitDefOrRef, implicitRef) {
    // let bodyCode = () =>
    {
        if (traitDefOrRef) {
            let tRef;
            if (traitDefOrRef.getObjectType && traitDefOrRef.getObjectType() === internal_1.cdmObjectType.traitRef) {
                // already a ref, just store it
                tRef = traitDefOrRef;
            }
            else {
                if (typeof (traitDefOrRef) === 'string') {
                    // all we got is a string, so make a trait ref out of it
                    tRef = new internal_1.CdmTraitReference(ctx, traitDefOrRef, implicitRef, false);
                }
                else {
                    // must be a trait def, so make a ref
                    tRef = new internal_1.CdmTraitReference(ctx, traitDefOrRef, false, false);
                }
            }
            collection.push(tRef);
            return tRef;
        }
    }
    // return p.measure(bodyCode);
}
exports.addTraitRef = addTraitRef;
function fetchTraitRefName(traitRefOrDef) {
    // let bodyCode = () =>
    {
        // lots of things this could be on an unresolved object model, so try them
        if (typeof traitRefOrDef === 'string') {
            return traitRefOrDef;
        }
        if (traitRefOrDef.trait) { // is this a resolved trait?
            return traitRefOrDef.traitName;
        }
        const ot = traitRefOrDef.getObjectType();
        if (ot === internal_1.cdmObjectType.traitDef) {
            return traitRefOrDef.getName();
        }
        if (ot === internal_1.cdmObjectType.traitRef) {
            return traitRefOrDef.fetchObjectDefinitionName();
        }
        return undefined;
    }
    // return p.measure(bodyCode);
}
exports.fetchTraitRefName = fetchTraitRefName;
function fetchTraitReferenceIndex(collection, traitDef, onlyFromProperty = false) {
    // let bodyCode = () =>
    {
        if (!collection) {
            return -1;
        }
        const traitName = fetchTraitRefName(traitDef);
        let traitIndex = -1;
        for (let index = 0; index < collection.length; index++) {
            const t = collection.allItems[index];
            if (fetchTraitRefName(t) === traitName) {
                // found a trait that satisfies the name, but continue checking for a trait from property.
                traitIndex = index;
                if (t instanceof internal_1.CdmTraitGroupReference || ('isFromProperty' in t ? t.isFromProperty : true)) {
                    return index;
                }
            }
        }
        // if it got here it means that the trait from property wasn't found.
        return onlyFromProperty ? -1 : traitIndex;
    }
    // return p.measure(bodyCode);
}
exports.fetchTraitReferenceIndex = fetchTraitReferenceIndex;
function removeTraitRef(collection, traitDef, onlyFromProperty) {
    // let bodyCode = () =>
    {
        const index = fetchTraitReferenceIndex(collection, traitDef, onlyFromProperty);
        if (index >= 0) {
            collection.allItems.splice(index, 1);
        }
    }
    // return p.measure(bodyCode);
}
exports.removeTraitRef = removeTraitRef;
//# sourceMappingURL=TraitRefUtil.js.map