"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.CdmOperationAddSupportingAttribute = void 0;
const internal_1 = require("../../internal");
/**
 * Class to handle AddSupportingAttribute operations
 */
class CdmOperationAddSupportingAttribute extends internal_1.CdmOperationBase {
    constructor(ctx) {
        super(ctx);
        this.TAG = CdmOperationAddSupportingAttribute.name;
        this.objectType = internal_1.cdmObjectType.operationAddSupportingAttributeDef;
        this.type = internal_1.cdmOperationType.addSupportingAttribute;
    }
    /**
     * @inheritdoc
     */
    copy(resOpt, host) {
        if (!resOpt) {
            resOpt = new internal_1.resolveOptions(this, this.ctx.corpus.defaultResolutionDirectives);
        }
        const copy = !host ? new CdmOperationAddSupportingAttribute(this.ctx) : host;
        copy.supportingAttribute = this.supportingAttribute ? this.supportingAttribute.copy(resOpt) : undefined;
        this.copyProj(resOpt, copy);
        return copy;
    }
    /**
     * @inheritdoc
     */
    getName() {
        return 'operationAddSupportingAttribute';
    }
    /**
     * @inheritdoc
     */
    getObjectType() {
        return internal_1.cdmObjectType.operationAddSupportingAttributeDef;
    }
    /**
     * @inheritdoc
     */
    validate() {
        const missingFields = [];
        if (!this.supportingAttribute) {
            missingFields.push('supportingAttribute');
        }
        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 attrCtxOpAddSupportingAttrParam = {
            under: attrCtx,
            type: internal_1.cdmAttributeContextType.operationAddSupportingAttribute,
            name: `operation/index${this.index}/${this.getName()}`
        };
        const attrCtxOpAddSupportingAttr = internal_1.CdmAttributeContext
            .createChildUnder(projCtx.projectionDirective.resOpt, attrCtxOpAddSupportingAttrParam);
        // Create a new attribute context for the supporting attribute we will create
        const attrCtxSupportingAttrParam = {
            under: attrCtxOpAddSupportingAttr,
            type: internal_1.cdmAttributeContextType.addedAttributeSupporting,
            name: this.supportingAttribute.name
        };
        const attrCtxSupportingAttr = internal_1.CdmAttributeContext
            .createChildUnder(projCtx.projectionDirective.resOpt, attrCtxSupportingAttrParam);
        // TODO: this if statement keeps the functionality the same way it works currently in resolution guidance.
        // This should be changed to point to the foreign key attribute instead. 
        // There has to be some design decisions about how this will work and will be done in the next release.
        if (projCtx.currentAttributeStateSet.states.length > 0) {
            const lastIndex = projCtx.currentAttributeStateSet.states.length - 1;
            const lastState = projCtx.currentAttributeStateSet.states[lastIndex];
            const inSupportOfTrait = this.supportingAttribute.appliedTraits.push('is.addedInSupportOf');
            inSupportOfTrait.arguments.push('inSupportOf', lastState.currentResolvedAttribute.resolvedName);
        }
        // Create the supporting attribute with the specified 'supportingAttribute' property as its target 
        // and apply the trait 'is.virtual.attribute' to it
        const addTrait = ['is.virtual.attribute'];
        const newResAttr = internal_1.CdmOperationBase.createNewResolvedAttribute(projCtx, attrCtxSupportingAttr, this.supportingAttribute, null, addTrait);
        // Create a new projection attribute state for the new supporting attribute and add it to the output set
        // There is no previous state for the newly created supporting attribute
        const newPAS = new internal_1.ProjectionAttributeState(projOutputSet.ctx);
        newPAS.currentResolvedAttribute = newResAttr;
        projOutputSet.add(newPAS);
        return projOutputSet;
    }
}
exports.CdmOperationAddSupportingAttribute = CdmOperationAddSupportingAttribute;
//# sourceMappingURL=CdmOperationAddSupportingAttribute.js.map