"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.AttributeContextPersistence = void 0;
const __1 = require("..");
const internal_1 = require("../../internal");
const copyDataUtils = require("../../Utilities/CopyDataUtils");
const AttributeContextReferencePersistence_1 = require("./AttributeContextReferencePersistence");
const utils = require("./utils");
class AttributeContextPersistence {
    static fromData(ctx, object) {
        if (!object) {
            return;
        }
        const attributeContext = ctx.corpus.MakeObject(internal_1.cdmObjectType.attributeContextDef, object.name);
        attributeContext.type = AttributeContextPersistence.mapTypeNameToEnum(object.type);
        if (object.parent) {
            attributeContext.parent = __1.CdmFolder.AttributeContextReferencePersistence.fromData(ctx, object.parent);
        }
        if (object.explanation) {
            attributeContext.explanation = object.explanation;
        }
        if (object.definition) {
            switch (attributeContext.type) {
                case internal_1.cdmAttributeContextType.entity:
                case internal_1.cdmAttributeContextType.entityReferenceExtends:
                    attributeContext.definition = __1.CdmFolder.EntityReferencePersistence.fromData(ctx, object.definition);
                    break;
                case internal_1.cdmAttributeContextType.attributeGroup:
                    attributeContext.definition = __1.CdmFolder.AttributeGroupReferencePersistence.fromData(ctx, object.definition);
                    break;
                case internal_1.cdmAttributeContextType.addedAttributeNewArtifact:
                case internal_1.cdmAttributeContextType.addedAttributeSupporting:
                case internal_1.cdmAttributeContextType.addedAttributeIdentity:
                case internal_1.cdmAttributeContextType.addedAttributeExpansionTotal:
                case internal_1.cdmAttributeContextType.addedAttributeSelectedType:
                case internal_1.cdmAttributeContextType.attributeDefinition:
                case internal_1.cdmAttributeContextType.attributeExcluded:
                    attributeContext.definition = __1.CdmFolder.AttributeReferencePersistence.fromData(ctx, object.definition);
                    break;
                default:
            }
        }
        // I know the trait collection names look wrong. but I wanted to use the def baseclass
        utils.addArrayToCdmCollection(attributeContext.exhibitsTraits, utils.createTraitReferenceArray(ctx, object.appliedTraits));
        if (object.contents && object.contents.length > 0) {
            const l = object.contents.length;
            for (let i = 0; i < l; i++) {
                const ct = object.contents[i];
                if (typeof (ct) === 'string') {
                    attributeContext.contents.push(__1.CdmFolder.AttributeReferencePersistence.fromData(ctx, ct));
                }
                else {
                    attributeContext.contents.push(AttributeContextPersistence.fromData(ctx, ct));
                }
            }
        }
        if (object.lineage) {
            attributeContext.lineage = new internal_1.CdmCollection(ctx, attributeContext, internal_1.cdmObjectType.attributeContextRef);
            for (const ct of object.lineage) {
                attributeContext.lineage.push(AttributeContextReferencePersistence_1.AttributeContextReferencePersistence.fromData(ctx, ct));
            }
        }
        return attributeContext;
    }
    static toData(instance, resOpt, options) {
        return {
            explanation: instance.explanation,
            name: instance.name,
            type: AttributeContextPersistence.mapEnumToTypeName(instance.type),
            parent: instance.parent ? instance.parent.copyData(resOpt, options) : undefined,
            definition: instance.definition ? instance.definition.copyData(resOpt, options) : undefined,
            // i know the trait collection names look wrong. but I wanted to use the def baseclass
            appliedTraits: copyDataUtils.arrayCopyData(resOpt, instance.exhibitsTraits.allItems.filter((trait) => trait instanceof internal_1.CdmTraitGroupReference || !trait.isFromProperty), options),
            contents: copyDataUtils.arrayCopyData(resOpt, instance.contents, options),
            lineage: copyDataUtils.arrayCopyData(resOpt, instance.lineage, options)
        };
    }
    static mapTypeNameToEnum(typeName) {
        switch (typeName) {
            case 'entity':
                return internal_1.cdmAttributeContextType.entity;
            case 'entityReferenceExtends':
                return internal_1.cdmAttributeContextType.entityReferenceExtends;
            case 'attributeGroup':
                return internal_1.cdmAttributeContextType.attributeGroup;
            case 'attributeDefinition':
                return internal_1.cdmAttributeContextType.attributeDefinition;
            case 'attributeExcluded':
                return internal_1.cdmAttributeContextType.attributeExcluded;
            case 'addedAttributeNewArtifact':
                return internal_1.cdmAttributeContextType.addedAttributeNewArtifact;
            case 'addedAttributeSupporting':
                return internal_1.cdmAttributeContextType.addedAttributeSupporting;
            case 'addedAttributeIdentity':
                return internal_1.cdmAttributeContextType.addedAttributeIdentity;
            case 'addedAttributeExpansionTotal':
                return internal_1.cdmAttributeContextType.addedAttributeExpansionTotal;
            case 'addedAttributeSelectedType':
                return internal_1.cdmAttributeContextType.addedAttributeSelectedType;
            case 'generatedRound':
                return internal_1.cdmAttributeContextType.generatedRound;
            case 'generatedSet':
                return internal_1.cdmAttributeContextType.generatedSet;
            case 'projection':
                return internal_1.cdmAttributeContextType.projection;
            case 'source':
                return internal_1.cdmAttributeContextType.source;
            case 'operations':
                return internal_1.cdmAttributeContextType.operations;
            case 'operationAddCountAttribute':
                return internal_1.cdmAttributeContextType.operationAddCountAttribute;
            case 'operationAddSupportingAttribute':
                return internal_1.cdmAttributeContextType.operationAddSupportingAttribute;
            case 'operationAddTypeAttribute':
                return internal_1.cdmAttributeContextType.operationAddTypeAttribute;
            case 'operationExcludeAttributes':
                return internal_1.cdmAttributeContextType.operationExcludeAttributes;
            case 'operationArrayExpansion':
                return internal_1.cdmAttributeContextType.operationArrayExpansion;
            case 'operationCombineAttributes':
                return internal_1.cdmAttributeContextType.operationCombineAttributes;
            case 'operationRenameAttributes':
                return internal_1.cdmAttributeContextType.operationRenameAttributes;
            case 'operationReplaceAsForeignKey':
                return internal_1.cdmAttributeContextType.operationReplaceAsForeignKey;
            case 'operationIncludeAttributes':
                return internal_1.cdmAttributeContextType.operationIncludeAttributes;
            case 'operationAddAttributeGroup':
                return internal_1.cdmAttributeContextType.operationAddAttributeGroup;
            case 'operationAlterTraits':
                return internal_1.cdmAttributeContextType.operationAlterTraits;
            case 'operationAddArtifactAttribute':
                return internal_1.cdmAttributeContextType.operationAddArtifactAttribute;
            case 'unknown':
                return internal_1.cdmAttributeContextType.unknown;
            default:
                return -1;
        }
    }
    static mapEnumToTypeName(enumVal) {
        switch (enumVal) {
            case internal_1.cdmAttributeContextType.entity:
                return 'entity';
            case internal_1.cdmAttributeContextType.entityReferenceExtends:
                return 'entityReferenceExtends';
            case internal_1.cdmAttributeContextType.attributeGroup:
                return 'attributeGroup';
            case internal_1.cdmAttributeContextType.attributeDefinition:
                return 'attributeDefinition';
            case internal_1.cdmAttributeContextType.attributeExcluded:
                return 'attributeExcluded';
            case internal_1.cdmAttributeContextType.addedAttributeNewArtifact:
                return 'addedAttributeNewArtifact';
            case internal_1.cdmAttributeContextType.addedAttributeSupporting:
                return 'addedAttributeSupporting';
            case internal_1.cdmAttributeContextType.addedAttributeIdentity:
                return 'addedAttributeIdentity';
            case internal_1.cdmAttributeContextType.addedAttributeExpansionTotal:
                return 'addedAttributeExpansionTotal';
            case internal_1.cdmAttributeContextType.addedAttributeSelectedType:
                return 'addedAttributeSelectedType';
            case internal_1.cdmAttributeContextType.generatedRound:
                return 'generatedRound';
            case internal_1.cdmAttributeContextType.generatedSet:
                return 'generatedSet';
            case internal_1.cdmAttributeContextType.projection:
                return 'projection';
            case internal_1.cdmAttributeContextType.source:
                return 'source';
            case internal_1.cdmAttributeContextType.operations:
                return 'operations';
            case internal_1.cdmAttributeContextType.operationAddCountAttribute:
                return 'operationAddCountAttribute';
            case internal_1.cdmAttributeContextType.operationAddSupportingAttribute:
                return 'operationAddSupportingAttribute';
            case internal_1.cdmAttributeContextType.operationAddTypeAttribute:
                return 'operationAddTypeAttribute';
            case internal_1.cdmAttributeContextType.operationExcludeAttributes:
                return 'operationExcludeAttributes';
            case internal_1.cdmAttributeContextType.operationArrayExpansion:
                return 'operationArrayExpansion';
            case internal_1.cdmAttributeContextType.operationCombineAttributes:
                return 'operationCombineAttributes';
            case internal_1.cdmAttributeContextType.operationRenameAttributes:
                return 'operationRenameAttributes';
            case internal_1.cdmAttributeContextType.operationReplaceAsForeignKey:
                return 'operationReplaceAsForeignKey';
            case internal_1.cdmAttributeContextType.operationIncludeAttributes:
                return 'operationIncludeAttributes';
            case internal_1.cdmAttributeContextType.operationAddAttributeGroup:
                return 'operationAddAttributeGroup';
            case internal_1.cdmAttributeContextType.operationAlterTraits:
                return 'operationAlterTraits';
            case internal_1.cdmAttributeContextType.operationAddArtifactAttribute:
                return 'operationAddArtifactAttribute';
            default:
                return 'unknown';
        }
    }
}
exports.AttributeContextPersistence = AttributeContextPersistence;
//# sourceMappingURL=AttributeContextPersistence.js.map