"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.ProjectionPersistence = void 0;
const internal_1 = require("../../../internal");
const EntityReferencePersistence_1 = require("../EntityReferencePersistence");
const OperationAddAttributeGroupPersistence_1 = require("./OperationAddAttributeGroupPersistence");
const OperationAddCountAttributePersistence_1 = require("./OperationAddCountAttributePersistence");
const OperationAddSupportingAttributePersistence_1 = require("./OperationAddSupportingAttributePersistence");
const OperationAddTypeAttributePersistence_1 = require("./OperationAddTypeAttributePersistence");
const OperationExcludeAttributesPersistence_1 = require("./OperationExcludeAttributesPersistence");
const OperationArrayExpansionPersistence_1 = require("./OperationArrayExpansionPersistence");
const OperationCombineAttributesPersistence_1 = require("./OperationCombineAttributesPersistence");
const OperationRenameAttributesPersistence_1 = require("./OperationRenameAttributesPersistence");
const OperationReplaceAsForeignKeyPersistence_1 = require("./OperationReplaceAsForeignKeyPersistence");
const OperationIncludeAttributesPersistence_1 = require("./OperationIncludeAttributesPersistence");
const OperationAlterTraitsPersistence_1 = require("./OperationAlterTraitsPersistence");
const OperationAddArtifactAttributePersistence_1 = require("./OperationAddArtifactAttributePersistence");
/**
 * Projection persistence
 */
class ProjectionPersistence {
    static fromData(ctx, object) {
        if (!object) {
            return undefined;
        }
        const projection = ctx.corpus.MakeObject(internal_1.cdmObjectType.projectionDef);
        const source = EntityReferencePersistence_1.EntityReferencePersistence.fromData(ctx, object.source);
        projection.explanation = object.explanation;
        projection.condition = object.condition;
        projection.runSequentially = object.runSequentially;
        if ('operations' in object && object.operations) {
            const operationJsons = object.operations;
            operationJsons.forEach((operationJson) => {
                const type = operationJson.$type;
                switch (type) {
                    case 'addCountAttribute':
                        const addCountAttributeOp = OperationAddCountAttributePersistence_1.OperationAddCountAttributePersistence.fromData(ctx, operationJson);
                        projection.operations.push(addCountAttributeOp);
                        break;
                    case 'addSupportingAttribute':
                        const addSupportingAttributeOp = OperationAddSupportingAttributePersistence_1.OperationAddSupportingAttributePersistence.fromData(ctx, operationJson);
                        projection.operations.push(addSupportingAttributeOp);
                        break;
                    case 'addTypeAttribute':
                        const addTypeAttributeOp = OperationAddTypeAttributePersistence_1.OperationAddTypeAttributePersistence.fromData(ctx, operationJson);
                        projection.operations.push(addTypeAttributeOp);
                        break;
                    case 'excludeAttributes':
                        const excludeAttributesOp = OperationExcludeAttributesPersistence_1.OperationExcludeAttributesPersistence.fromData(ctx, operationJson);
                        projection.operations.push(excludeAttributesOp);
                        break;
                    case 'arrayExpansion':
                        const arrayExpansionOp = OperationArrayExpansionPersistence_1.OperationArrayExpansionPersistence.fromData(ctx, operationJson);
                        projection.operations.push(arrayExpansionOp);
                        break;
                    case 'combineAttributes':
                        const combineAttributesOp = OperationCombineAttributesPersistence_1.OperationCombineAttributesPersistence.fromData(ctx, operationJson);
                        projection.operations.push(combineAttributesOp);
                        break;
                    case 'renameAttributes':
                        const renameAttributesOp = OperationRenameAttributesPersistence_1.OperationRenameAttributesPersistence.fromData(ctx, operationJson);
                        projection.operations.push(renameAttributesOp);
                        break;
                    case 'replaceAsForeignKey':
                        const replaceAsForeignKeyOp = OperationReplaceAsForeignKeyPersistence_1.OperationReplaceAsForeignKeyPersistence.fromData(ctx, operationJson);
                        projection.operations.push(replaceAsForeignKeyOp);
                        break;
                    case 'includeAttributes':
                        const includeAttributesOp = OperationIncludeAttributesPersistence_1.OperationIncludeAttributesPersistence.fromData(ctx, operationJson);
                        projection.operations.push(includeAttributesOp);
                        break;
                    case 'addAttributeGroup':
                        const addAttributeGroupOp = OperationAddAttributeGroupPersistence_1.OperationAddAttributeGroupPersistence.fromData(ctx, operationJson);
                        projection.operations.push(addAttributeGroupOp);
                        break;
                    case 'alterTraits':
                        const alterTraitsOp = OperationAlterTraitsPersistence_1.OperationAlterTraitsPersistence.fromData(ctx, operationJson);
                        projection.operations.push(alterTraitsOp);
                        break;
                    case 'addArtifactAttribute':
                        const addArtifactAttributeOp = OperationAddArtifactAttributePersistence_1.OperationAddArtifactAttributePersistence.fromData(ctx, operationJson);
                        projection.operations.push(addArtifactAttributeOp);
                        break;
                    default:
                        internal_1.Logger.error(ctx, this.TAG, this.fromData.name, undefined, internal_1.cdmLogCode.ErrPersistProjInvalidOpsType, type);
                }
            });
        }
        projection.source = source;
        return projection;
    }
    static toData(instance, resOpt, options) {
        if (!instance) {
            return undefined;
        }
        let source = undefined;
        if (instance.source && typeof instance.source === 'string') {
            source = instance.source;
        }
        else if (instance.source && !internal_1.StringUtils.isNullOrWhiteSpace(instance.source.namedReference) && instance.source.explicitReference === undefined) {
            source = instance.source.namedReference;
        }
        else if (instance.source && instance.source.getObjectType() === internal_1.cdmObjectType.entityRef) {
            source = EntityReferencePersistence_1.EntityReferencePersistence.toData(instance.source, resOpt, options);
        }
        let operations;
        if (instance.operations && instance.operations.length > 0) {
            operations = [];
            instance.operations.allItems.forEach((operation) => {
                switch (operation.objectType) {
                    case internal_1.cdmObjectType.operationAddCountAttributeDef:
                        const addCountAttributeOp = OperationAddCountAttributePersistence_1.OperationAddCountAttributePersistence.toData(operation, resOpt, options);
                        operations.push(addCountAttributeOp);
                        break;
                    case internal_1.cdmObjectType.operationAddSupportingAttributeDef:
                        const addSupportingAttributeOp = OperationAddSupportingAttributePersistence_1.OperationAddSupportingAttributePersistence.toData(operation, resOpt, options);
                        operations.push(addSupportingAttributeOp);
                        break;
                    case internal_1.cdmObjectType.operationAddTypeAttributeDef:
                        const addTypeAttributeOp = OperationAddTypeAttributePersistence_1.OperationAddTypeAttributePersistence.toData(operation, resOpt, options);
                        operations.push(addTypeAttributeOp);
                        break;
                    case internal_1.cdmObjectType.operationExcludeAttributesDef:
                        const excludeAttributesOp = OperationExcludeAttributesPersistence_1.OperationExcludeAttributesPersistence.toData(operation, resOpt, options);
                        operations.push(excludeAttributesOp);
                        break;
                    case internal_1.cdmObjectType.operationArrayExpansionDef:
                        const arrayExpansionOp = OperationArrayExpansionPersistence_1.OperationArrayExpansionPersistence.toData(operation, resOpt, options);
                        operations.push(arrayExpansionOp);
                        break;
                    case internal_1.cdmObjectType.operationCombineAttributesDef:
                        const combineAttributesOp = OperationCombineAttributesPersistence_1.OperationCombineAttributesPersistence.toData(operation, resOpt, options);
                        operations.push(combineAttributesOp);
                        break;
                    case internal_1.cdmObjectType.operationRenameAttributesDef:
                        const renameAttributesOp = OperationRenameAttributesPersistence_1.OperationRenameAttributesPersistence.toData(operation, resOpt, options);
                        operations.push(renameAttributesOp);
                        break;
                    case internal_1.cdmObjectType.operationReplaceAsForeignKeyDef:
                        const replaceAsForeignKeyOp = OperationReplaceAsForeignKeyPersistence_1.OperationReplaceAsForeignKeyPersistence.toData(operation, resOpt, options);
                        operations.push(replaceAsForeignKeyOp);
                        break;
                    case internal_1.cdmObjectType.operationIncludeAttributesDef:
                        const includeAttributesOp = OperationIncludeAttributesPersistence_1.OperationIncludeAttributesPersistence.toData(operation, resOpt, options);
                        operations.push(includeAttributesOp);
                        break;
                    case internal_1.cdmObjectType.operationAddAttributeGroupDef:
                        const addAttributeGroupOp = OperationAddAttributeGroupPersistence_1.OperationAddAttributeGroupPersistence.toData(operation, resOpt, options);
                        operations.push(addAttributeGroupOp);
                        break;
                    case internal_1.cdmObjectType.operationAlterTraitsDef:
                        const alterTraitsOp = OperationAlterTraitsPersistence_1.OperationAlterTraitsPersistence.toData(operation, resOpt, options);
                        operations.push(alterTraitsOp);
                        break;
                    case internal_1.cdmObjectType.operationAddArtifactAttributeDef:
                        const addArtifactAttributeOp = OperationAddArtifactAttributePersistence_1.OperationAddArtifactAttributePersistence.toData(operation, resOpt, options);
                        operations.push(addArtifactAttributeOp);
                        break;
                    default:
                        const baseOp = {
                            $type: internal_1.OperationTypeConvertor.operationTypeToString(internal_1.cdmOperationType.error)
                        };
                        operations.push(baseOp);
                }
            });
        }
        return {
            explanation: instance.explanation,
            source: source,
            operations: operations,
            condition: instance.condition,
            runSequentially: instance.runSequentially
        };
    }
}
exports.ProjectionPersistence = ProjectionPersistence;
ProjectionPersistence.TAG = ProjectionPersistence.name;
//# sourceMappingURL=ProjectionPersistence.js.map