"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.CdmAttribute = void 0;
const internal_1 = require("../internal");
class CdmAttribute extends internal_1.CdmObjectDefinitionBase {
    constructor(ctx, name) {
        super(ctx);
        // let bodyCode = () =>
        {
            this.name = name;
            this.appliedTraits = new internal_1.CdmTraitCollection(this.ctx, this);
            this.attributeCount = 0;
        }
        // return p.measure(bodyCode);
    }
    /**
     * @internal
     */
    copyAtt(resOpt, copy) {
        // let bodyCode = () =>
        {
            copy.purpose = this.purpose ? this.purpose.copy(resOpt) : undefined;
            copy.resolutionGuidance = this.resolutionGuidance ? this.resolutionGuidance.copy(resOpt) : undefined;
            copy.appliedTraits.clear();
            for (const trait of this.appliedTraits) {
                copy.appliedTraits.push(trait.copy(resOpt));
            }
            this.copyDef(resOpt, copy);
            return copy;
        }
        // return p.measure(bodyCode);
    }
    getName() {
        // let bodyCode = () =>
        {
            return this.name;
        }
        // return p.measure(bodyCode);
    }
    /**
     * @internal
     */
    setObjectDef(def) {
        // let bodyCode = () =>
        {
            throw Error('not a ref');
        }
        // return p.measure(bodyCode);
    }
    /**
     * @internal
     */
    visitAtt(pathFrom, preChildren, postChildren) {
        // let bodyCode = () =>
        {
            if (this.purpose) {
                this.purpose.owner = this;
                if (this.purpose.visit(`${pathFrom}/purpose/`, preChildren, postChildren)) {
                    return true;
                }
            }
            if (this.appliedTraits) {
                if (this.appliedTraits.visitArray(`${pathFrom}/appliedTraits/`, preChildren, postChildren)) {
                    return true;
                }
            }
            if (this.resolutionGuidance) {
                this.resolutionGuidance.owner = this;
                if (this.resolutionGuidance.visit(`${pathFrom}/resolutionGuidance/`, preChildren, postChildren)) {
                    return true;
                }
            }
            if (this.visitDef(pathFrom, preChildren, postChildren)) {
                return true;
            }
            return false;
        }
        // return p.measure(bodyCode);
    }
    /**
     * @internal
     */
    addResolvedTraitsApplied(rtsb, resOpt) {
        // let bodyCode = () =>
        {
            this.appliedTraits.allItems.forEach(trait => rtsb.mergeTraits(trait.fetchResolvedTraits(resOpt)));
            // any applied on use
            return rtsb.rts;
        }
        // return p.measure(bodyCode);
    }
    removeTraitDef(resOpt, def) {
        // let bodyCode = () =>
        {
            this.clearTraitCache();
            const traitName = def.getName();
            if (this.appliedTraits) {
                let iRemove = 0;
                for (iRemove = 0; iRemove < this.appliedTraits.length; iRemove++) {
                    const tr = this.appliedTraits[iRemove];
                    if (tr.fetchObjectDefinitionName() === traitName) {
                        break;
                    }
                }
                if (iRemove < this.appliedTraits.length) {
                    this.appliedTraits.allItems.splice(iRemove, 1);
                    return;
                }
            }
        }
        // return p.measure(bodyCode);
    }
}
exports.CdmAttribute = CdmAttribute;
//# sourceMappingURL=CdmAttribute.js.map