"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.ResolvedAttribute = void 0;
const internal_1 = require("../internal");
/**
     * @internal
     */
class ResolvedAttribute {
    constructor(resOpt, target, defaultName, attCtx) {
        this.insertOrder = 0;
        // let bodyCode = () =>
        {
            this.target = target;
            this.resolvedTraits = new internal_1.ResolvedTraitSet(resOpt);
            this.resolvedName = defaultName;
            this.previousResolvedName = defaultName;
            this.attCtx = attCtx;
            // if the target is a resolved attribute set, then we are wrapping it. update the lineage of this new ra to point at all members of the set
            if (target instanceof internal_1.ResolvedAttributeSet && attCtx) {
                const rasSub = target;
                if (rasSub.set && rasSub.set.length > 0) {
                    for (const raSub of rasSub.set) {
                        if (raSub.attCtx) {
                            attCtx.addLineage(raSub.attCtx);
                        }
                    }
                }
            }
        }
        // return p.measure(bodyCode);
    }
    get isPrimaryKey() {
        return this.traitToPropertyMap
            .fetchPropertyValue('isPrimaryKey');
    }
    get isReadOnly() {
        return this.traitToPropertyMap
            .fetchPropertyValue('isReadOnly');
    }
    get isNullable() {
        return this.traitToPropertyMap
            .fetchPropertyValue('isNullable');
    }
    get dataFormat() {
        return this.traitToPropertyMap
            .fetchPropertyValue('dataFormat');
    }
    get sourceName() {
        return this.traitToPropertyMap
            .fetchPropertyValue('sourceName');
    }
    get sourceOrdering() {
        return this.traitToPropertyMap
            .fetchPropertyValue('sourceOrdering');
    }
    get displayName() {
        return this.traitToPropertyMap
            .fetchPropertyValue('displayName');
    }
    get description() {
        return this.traitToPropertyMap
            .fetchPropertyValue('description');
    }
    get maximumValue() {
        return this.traitToPropertyMap
            .fetchPropertyValue('maximumValue');
    }
    get minimumValue() {
        return this.traitToPropertyMap
            .fetchPropertyValue('minimumValue');
    }
    get maximumLength() {
        return this.traitToPropertyMap
            .fetchPropertyValue('maximumLength');
    }
    get valueConstrainedToList() {
        return this.traitToPropertyMap
            .fetchPropertyValue('valueConstrainedToList');
    }
    get defaultValue() {
        return this.traitToPropertyMap
            .fetchPropertyValue('defaultValue');
    }
    get creationSequence() {
        return this.insertOrder;
    }
    get traitToPropertyMap() {
        if (this.t2pm) {
            return this.t2pm;
        }
        this.t2pm = new internal_1.traitToPropertyMap(this.target);
        return this.t2pm;
    }
    set resolvedName(newVal) {
        this._resolvedName = newVal;
        if (this.previousResolvedName === undefined) {
            this.previousResolvedName = newVal;
        }
    }
    get resolvedName() {
        return this._resolvedName;
    }
    set target(newVal) {
        if (newVal !== undefined) {
            if (newVal instanceof internal_1.CdmAttribute) {
                this.resolvedAttributeCount = newVal.attributeCount;
            }
            else if (newVal instanceof internal_1.ResolvedAttributeSet) {
                this.resolvedAttributeCount = newVal.resolvedAttributeCount;
            }
        }
        this._target = newVal;
    }
    get target() {
        return this._target;
    }
    copy() {
        // let bodyCode = () =>
        {
            const resOpt = this.resolvedTraits.resOpt; // use the options from the traits
            const copy = new ResolvedAttribute(resOpt, this.target, this._resolvedName, undefined);
            copy.previousResolvedName = this.previousResolvedName;
            copy.resolvedName = this.resolvedName;
            copy.resolvedTraits = this.resolvedTraits.shallowCopy();
            copy.insertOrder = this.insertOrder;
            copy.arc = this.arc;
            copy.attCtx = this.attCtx; // set here instead of constructor to avoid setting lineage for this copy
            copy.owner = this.owner;
            if (copy.target instanceof internal_1.ResolvedAttributeSet) {
                // deep copy when set contains sets. this copies the resolved att set and the context, etc.
                copy.target = copy.target.copy();
            }
            if (this.applierState) {
                copy.applierState = {};
                Object.assign(copy.applierState, this.applierState);
            }
            return copy;
        }
        // return p.measure(bodyCode);
    }
    spew(resOpt, to, indent, nameSort) {
        // let bodyCode = () =>
        {
            to.spewLine(`${indent}[${this._resolvedName}]`);
            this.resolvedTraits.spew(resOpt, to, `${indent}-`, nameSort);
        }
        // return p.measure(bodyCode);
    }
    completeContext(resOpt) {
        if (this.attCtx) {
            if (this.attCtx.name === undefined) {
                this.attCtx.name = this._resolvedName;
                this.attCtx.atCorpusPath = `${this.attCtx.parent.fetchObjectDefinition(resOpt).atCorpusPath}/${this._resolvedName}`;
            }
            if (!this.attCtx.definition && this.target instanceof internal_1.CdmAttribute)
                this.attCtx.definition = this.target.createPortableReference(resOpt);
        }
    }
}
exports.ResolvedAttribute = ResolvedAttribute;
//# sourceMappingURL=ResolvedAttribute.js.map