"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.CdmOperationExcludeAttributes = void 0;
const internal_1 = require("../../internal");
/**
 * Class to handle ExcludeAttributes operations
 */
class CdmOperationExcludeAttributes extends internal_1.CdmOperationBase {
    constructor(ctx) {
        super(ctx);
        this.TAG = CdmOperationExcludeAttributes.name;
        this.objectType = internal_1.cdmObjectType.operationExcludeAttributesDef;
        this.type = internal_1.cdmOperationType.excludeAttributes;
    }
    /**
     * @inheritdoc
     */
    copy(resOpt, host) {
        if (!resOpt) {
            resOpt = new internal_1.resolveOptions(this, this.ctx.corpus.defaultResolutionDirectives);
        }
        const copy = !host ? new CdmOperationExcludeAttributes(this.ctx) : host;
        copy.excludeAttributes = this.excludeAttributes ? this.excludeAttributes.slice() : undefined;
        this.copyProj(resOpt, copy);
        return copy;
    }
    /**
     * @inheritdoc
     */
    getName() {
        return 'operationExcludeAttributes';
    }
    /**
     * @inheritdoc
     */
    getObjectType() {
        return internal_1.cdmObjectType.operationExcludeAttributesDef;
    }
    /**
     * @inheritdoc
     */
    validate() {
        const missingFields = [];
        if (!this.excludeAttributes) {
            missingFields.push('excludeAttributes');
        }
        if (missingFields.length > 0) {
            internal_1.Logger.error(this.ctx, this.TAG, this.validate.name, this.atCorpusPath, internal_1.cdmLogCode.ErrValdnIntegrityCheckFailure, this.atCorpusPath, missingFields.map((s) => `'${s}'`).join(', '));
            return false;
        }
        return true;
    }
    /**
     * @inheritdoc
     */
    visit(pathFrom, preChildren, postChildren) {
        const path = this.fetchDeclaredPath(pathFrom);
        if (preChildren && preChildren(this, path)) {
            return false;
        }
        if (postChildren && postChildren(this, path)) {
            return true;
        }
        return false;
    }
    /**
     * @inheritdoc
     * @internal
     */
    appendProjectionAttributeState(projCtx, projOutputSet, attrCtx) {
        // Create a new attribute context for the operation
        const attrCtxOpExcludeAttrsParam = {
            under: attrCtx,
            type: internal_1.cdmAttributeContextType.operationExcludeAttributes,
            name: `operation/index${this.index}/operationExcludeAttributes`
        };
        const attrCtxOpExcludeAttrs = internal_1.CdmAttributeContext.createChildUnder(projCtx.projectionDirective.resOpt, attrCtxOpExcludeAttrsParam);
        // Get the top-level attribute names of the attributes to exclude
        // We use the top-level names because the exclude list may contain a previous name our current resolved attributes had
        const topLevelExcludeAttributeNames = internal_1.ProjectionResolutionCommonUtil.getTopList(projCtx, this.excludeAttributes);
        // Initialize a projection attribute context tree builder with the created attribute context for the operation
        const attrCtxTreeBuilder = new internal_1.ProjectionAttributeContextTreeBuilder(attrCtxOpExcludeAttrs);
        // Iterate through all the projection attribute states generated from the source's resolved attributes
        // Each projection attribute state contains a resolved attribute that it is corresponding to
        for (const currentPAS of projCtx.currentAttributeStateSet.states) {
            // Check if the current projection attribute state's resolved attribute is in the list of attributes to exclude
            // If this attribute is not in the exclude list, then we are including it in the output
            if (!topLevelExcludeAttributeNames.has(currentPAS.currentResolvedAttribute.resolvedName)) {
                // Create the attribute context parameters and just store it in the builder for now
                // We will create the attribute contexts at the end
                attrCtxTreeBuilder.createAndStoreAttributeContextParameters(undefined, currentPAS, currentPAS.currentResolvedAttribute, internal_1.cdmAttributeContextType.attributeDefinition, currentPAS.currentResolvedAttribute.attCtx, // lineage is the included attribute
                undefined // don't know who will point here
                );
                // Create a projection attribute state for the included attribute by creating a copy of the current state
                // Copy() sets the current state as the previous state for the new one
                // We only create projection attribute states for attributes that are not in the exclude list
                const newPAS = currentPAS.copy();
                projOutputSet.add(newPAS);
            }
            else {
                // The current projection attribute state's resolved attribute is in the exclude list
                // Get the attribute name the way it appears in the exclude list
                const excludeAttributeName = topLevelExcludeAttributeNames.get(currentPAS.currentResolvedAttribute.resolvedName);
                // Create the attribute context parameters and just store it in the builder for now
                // We will create the attribute contexts at the end
                attrCtxTreeBuilder.createAndStoreAttributeContextParameters(excludeAttributeName, currentPAS, currentPAS.currentResolvedAttribute, internal_1.cdmAttributeContextType.attributeExcluded, currentPAS.currentResolvedAttribute.attCtx, // lineage is the included attribute
                undefined // don't know who will point here yet, excluded, so... this could be the end for you.
                );
            }
        }
        // Create all the attribute contexts and construct the tree
        attrCtxTreeBuilder.constructAttributeContextTree(projCtx);
        return projOutputSet;
    }
}
exports.CdmOperationExcludeAttributes = CdmOperationExcludeAttributes;
//# sourceMappingURL=CdmOperationExcludeAttributes.js.map