"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.CdmOperationAddTypeAttribute = void 0;
const internal_1 = require("../../internal");
/**
 * Class to handle AddTypeAttribute operations
 */
class CdmOperationAddTypeAttribute extends internal_1.CdmOperationBase {
    constructor(ctx) {
        super(ctx);
        this.TAG = CdmOperationAddTypeAttribute.name;
        this.objectType = internal_1.cdmObjectType.operationAddTypeAttributeDef;
        this.type = internal_1.cdmOperationType.addTypeAttribute;
    }
    /**
     * @inheritdoc
     */
    copy(resOpt, host) {
        if (!resOpt) {
            resOpt = new internal_1.resolveOptions(this, this.ctx.corpus.defaultResolutionDirectives);
        }
        let copy = !host ? new CdmOperationAddTypeAttribute(this.ctx) : host;
        copy.typeAttribute = this.typeAttribute ? this.typeAttribute.copy(resOpt) : undefined;
        this.copyProj(resOpt, copy);
        return copy;
    }
    /**
     * @inheritdoc
     */
    getName() {
        return 'operationAddTypeAttribute';
    }
    /**
     * @inheritdoc
     */
    getObjectType() {
        return internal_1.cdmObjectType.operationAddTypeAttributeDef;
    }
    /**
     * @inheritdoc
     */
    validate() {
        const missingFields = [];
        if (!this.typeAttribute) {
            missingFields.push('typeAttribute');
        }
        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) {
        // 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 attrCtxOpAddTypeParam = {
            under: attrCtx,
            type: internal_1.cdmAttributeContextType.operationAddTypeAttribute,
            name: `operation/index${this.index}/operationAddTypeAttribute`
        };
        const attrCtxOpAddType = internal_1.CdmAttributeContext.createChildUnder(projCtx.projectionDirective.resOpt, attrCtxOpAddTypeParam);
        // Create a new attribute context for the Type attribute we will create
        const attrCtxTypeAttrParam = {
            under: attrCtxOpAddType,
            type: internal_1.cdmAttributeContextType.addedAttributeSelectedType,
            name: '_selectedEntityName'
        };
        const attrCtxTypeAttr = internal_1.CdmAttributeContext.createChildUnder(projCtx.projectionDirective.resOpt, attrCtxTypeAttrParam);
        // Create the Type attribute with the specified "typeAttribute" (from the operation) as its target and apply the trait "is.linkedEntity.name" to it
        const addTrait = ['is.linkedEntity.name'];
        const newResAttr = internal_1.CdmOperationBase.createNewResolvedAttribute(projCtx, attrCtxTypeAttr, this.typeAttribute, undefined, addTrait);
        // Create a new projection attribute state for the new Type attribute and add it to the output set
        // There is no previous state for the newly created Type attribute
        const newPAS = new internal_1.ProjectionAttributeState(projOutputSet.ctx);
        newPAS.currentResolvedAttribute = newResAttr;
        projOutputSet.add(newPAS);
        return projOutputSet;
    }
}
exports.CdmOperationAddTypeAttribute = CdmOperationAddTypeAttribute;
//# sourceMappingURL=CdmOperationAddTypeAttribute.js.map