"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.CdmOperationAddCountAttribute = void 0;
const internal_1 = require("../../internal");
/**
 * Class to handle AddCountAttribute operations
 */
class CdmOperationAddCountAttribute extends internal_1.CdmOperationBase {
    constructor(ctx) {
        super(ctx);
        this.TAG = CdmOperationAddCountAttribute.name;
        this.objectType = internal_1.cdmObjectType.operationAddCountAttributeDef;
        this.type = internal_1.cdmOperationType.addCountAttribute;
    }
    /**
     * @inheritdoc
     */
    copy(resOpt, host) {
        if (!resOpt) {
            resOpt = new internal_1.resolveOptions(this, this.ctx.corpus.defaultResolutionDirectives);
        }
        const copy = !host ? new CdmOperationAddCountAttribute(this.ctx) : host;
        copy.countAttribute = this.countAttribute ? this.countAttribute.copy(resOpt) : undefined;
        this.copyProj(resOpt, copy);
        return copy;
    }
    /**
     * @inheritdoc
     */
    getName() {
        return 'operationAddCountAttribute';
    }
    /**
     * @inheritdoc
     */
    getObjectType() {
        return internal_1.cdmObjectType.operationAddCountAttributeDef;
    }
    /**
     * @inheritdoc
     */
    validate() {
        const missingFields = [];
        if (!this.countAttribute) {
            missingFields.push('countAttribute');
        }
        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 (this.countAttribute !== undefined && this.countAttribute.visit(`${path}/countAttribute/`, preChildren, postChildren)) {
            return true;
        }
        if (postChildren && postChildren(this, path)) {
            return true;
        }
        return false;
    }
    /**
     * @inheritdoc
     * @internal
     */
    appendProjectionAttributeState(projCtx, projOutputSet, attrCtx) {
        // Pass through all the input projection attribute states if there are any
        for (const currentPAS of projCtx.currentAttributeStateSet.states) {
            projOutputSet.add(currentPAS);
        }
        // Create a new attribute context for the operation
        const attrCtxOpAddCountParam = {
            under: attrCtx,
            type: internal_1.cdmAttributeContextType.operationAddCountAttribute,
            name: `operation/index${this.index}/operationAddCountAttribute`
        };
        const attrCtxOpAddCount = internal_1.CdmAttributeContext.createChildUnder(projCtx.projectionDirective.resOpt, attrCtxOpAddCountParam);
        // Create a new attribute context for the Count attribute we will create
        const attrCtxCountAttrParam = {
            under: attrCtxOpAddCount,
            type: internal_1.cdmAttributeContextType.addedAttributeExpansionTotal,
            name: this.countAttribute.name
        };
        const attrCtxCountAttr = internal_1.CdmAttributeContext.createChildUnder(projCtx.projectionDirective.resOpt, attrCtxCountAttrParam);
        // Create the Count attribute with the specified CountAttribute as its target and apply the trait "is.linkedEntity.array.count" to it
        const addTrait = ['is.linkedEntity.array.count'];
        const newResAttr = internal_1.CdmOperationBase.createNewResolvedAttribute(projCtx, attrCtxCountAttr, this.countAttribute, undefined, addTrait);
        // Create a new projection attribute state for the new Count attribute and add it to the output set
        // There is no previous state for the newly created Count attribute
        const newPAS = new internal_1.ProjectionAttributeState(projOutputSet.ctx);
        newPAS.currentResolvedAttribute = newResAttr;
        projOutputSet.add(newPAS);
        return projOutputSet;
    }
}
exports.CdmOperationAddCountAttribute = CdmOperationAddCountAttribute;
//# sourceMappingURL=CdmOperationAddCountAttribute.js.map