"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.CdmOperationBase = void 0;
const internal_1 = require("../../internal");
/**
 * Base class for all operations
 */
class CdmOperationBase extends internal_1.CdmObjectDefinitionBase {
    constructor(ctx) {
        super(ctx);
    }
    /**
     * @internal
     */
    copyProj(resOpt, copy) {
        copy.type = this.type;
        copy.index = this.index;
        copy.condition = this.condition;
        copy.sourceInput = this.sourceInput;
        this.copyDef(resOpt, copy);
        return copy;
    }
    /**
     * @inheritdoc
     */
    isDerivedFrom(base, resOpt) {
        return false;
    }
    /**
     * Projections require a new resolved attribute to be created multiple times
     * This function allows us to create new resolved attributes based on a input attribute
     * @internal
     */
    static createNewResolvedAttribute(projCtx, attrCtxUnder, target_attr_or_resolved_attr, overrideDefaultName = null, addedSimpleRefTraits = null) {
        const targetAttr = target_attr_or_resolved_attr instanceof internal_1.CdmAttribute
            ? target_attr_or_resolved_attr.copy()
            : target_attr_or_resolved_attr.target.copy();
        const newResAttr = new internal_1.ResolvedAttribute(projCtx.projectionDirective.resOpt, targetAttr, overrideDefaultName ? overrideDefaultName : targetAttr.getName(), attrCtxUnder);
        targetAttr.inDocument = projCtx.projectionDirective.owner.inDocument;
        if (target_attr_or_resolved_attr instanceof internal_1.CdmAttribute) {
            if (addedSimpleRefTraits) {
                for (const trait of addedSimpleRefTraits) {
                    if (!targetAttr.appliedTraits.item(trait)) {
                        targetAttr.appliedTraits.push(trait, true);
                    }
                }
            }
            const resTraitSet = targetAttr.fetchResolvedTraits(projCtx.projectionDirective.resOpt);
            // Create deep a copy of traits to avoid conflicts in case of parameters
            if (resTraitSet) {
                newResAttr.resolvedTraits = resTraitSet.deepCopy();
            }
        }
        else {
            newResAttr.resolvedTraits = target_attr_or_resolved_attr.resolvedTraits.deepCopy();
            if (addedSimpleRefTraits) {
                for (const trait of addedSimpleRefTraits) {
                    const traitReference = new internal_1.CdmTraitReference(targetAttr.ctx, trait, true, false);
                    newResAttr.resolvedTraits = newResAttr.resolvedTraits.mergeSet(traitReference.fetchResolvedTraits());
                }
            }
        }
        return newResAttr;
    }
    /**
     * Replace the wildcard character. {a/A} will be replaced with the current attribute's original name.
     * {m/M} will be replaced with the attribute name of the projection owner.
     * {mo/Mo} will be replaced with the current attribute's resolved name.
     * {o} will be replaced with the index of the attribute after an array expansion.
     * @internal
     */
    static replaceWildcardCharacters(format, projectionOwnerName, currentPAS) {
        var _a, _b, _c;
        if (!format) {
            return '';
        }
        const ordinal = currentPAS.ordinal != null ? currentPAS.ordinal.toString() : "";
        const originalMemberAttributeName = (_b = (_a = currentPAS.currentResolvedAttribute.target) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : "";
        const resolvedMemberAttributeName = (_c = currentPAS.currentResolvedAttribute.resolvedName) !== null && _c !== void 0 ? _c : "";
        let value = internal_1.StringUtils.replace(format, 'a', projectionOwnerName);
        value = internal_1.StringUtils.replace(value, 'o', ordinal);
        value = internal_1.StringUtils.replace(value, 'mo', originalMemberAttributeName);
        value = internal_1.StringUtils.replace(value, 'm', resolvedMemberAttributeName);
        return value;
    }
}
exports.CdmOperationBase = CdmOperationBase;
//# sourceMappingURL=CdmOperationBase.js.map