"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.DepthInfo = void 0;
class DepthInfo {
    constructor() {
        this.reset();
    }
    /**
     * Resets the instance to its initial values.
     * @internal
     */
    reset() {
        this.currentDepth = 0;
        this.maxDepth = undefined;
        this.maxDepthExceeded = false;
    }
    /**
     * Creates a copy of this depth info instance.
     * @internal
     */
    copy() {
        const copy = new DepthInfo();
        copy.currentDepth = this.currentDepth;
        copy.maxDepth = this.maxDepth;
        copy.maxDepthExceeded = this.maxDepthExceeded;
        return copy;
    }
    /**
     * Updates this depth info to the next level.
     * @internal
     */
    updateToNextLevel(resOpt, isPolymorphic, arc = undefined) {
        var _a;
        let directives = resOpt.directives;
        let isByRef = false;
        this.maxDepth = resOpt.maxDepth;
        // if using resolution guidance
        if (arc !== undefined) {
            if (arc.resOpt !== undefined) {
                directives = arc.resOpt.directives;
                if (isPolymorphic === undefined) {
                    isPolymorphic = (directives === null || directives === void 0 ? void 0 : directives.has('selectOne')) === true;
                }
            }
            if (((_a = arc.resGuide) === null || _a === void 0 ? void 0 : _a.entityByReference) !== undefined) {
                if (arc.resGuide.entityByReference.referenceOnlyAfterDepth !== undefined) {
                    this.maxDepth = arc.resGuide.entityByReference.referenceOnlyAfterDepth;
                }
                if (arc.resGuide.entityByReference.allowReference === true) {
                    isByRef = (directives === null || directives === void 0 ? void 0 : directives.has('referenceOnly')) === true;
                }
            }
        }
        if (directives !== undefined) {
            if (directives.has('noMaxDepth')) {
                // no max? really? what if we loop forever? if you need more than 32 nested entities, then you should buy a different metadata description system
                this.maxDepth = DepthInfo.maxDepthLimit;
            }
        }
        // if this is a polymorphic, then skip counting this entity in the depth, else count it
        // if it's already by reference, we won't go one more level down so don't increase current depth
        if (isPolymorphic !== true && !isByRef) {
            this.currentDepth++;
            if (this.currentDepth > this.maxDepth) {
                this.maxDepthExceeded = true;
            }
        }
    }
}
exports.DepthInfo = DepthInfo;
/**
 * The max depth set if the user specified to not use max depth
 */
DepthInfo.maxDepthLimit = 32;
//# sourceMappingURL=depthInfo.js.map