"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.EntityPersistence = void 0;
const __1 = require("..");
const internal_1 = require("../../internal");
const copyDataUtils = require("../../Utilities/CopyDataUtils");
const utils = require("./utils");
class EntityPersistence {
    static fromData(ctx, object) {
        if (!object) {
            return undefined;
        }
        const entity = ctx.corpus.MakeObject(internal_1.cdmObjectType.entityDef, object.entityName);
        entity.extendsEntity = __1.CdmFolder.EntityReferencePersistence.fromData(ctx, object.extendsEntity);
        entity.extendsEntityResolutionGuidance =
            __1.CdmFolder.AttributeResolutionGuidancePersistence.fromData(ctx, object.extendsEntityResolutionGuidance);
        entity.explanation = utils.propertyFromDataToString(object.explanation);
        utils.addArrayToCdmCollection(entity.exhibitsTraits, utils.createTraitReferenceArray(ctx, object.exhibitsTraits));
        entity.sourceName = utils.propertyFromDataToString(object.sourceName);
        entity.displayName = utils.propertyFromDataToString(object.displayName);
        entity.description = utils.propertyFromDataToString(object.description);
        entity.version = utils.propertyFromDataToString(object.version);
        entity.cdmSchemas = object.cdmSchemas;
        if (object.attributeContext) {
            entity.attributeContext = __1.CdmFolder.AttributeContextPersistence.fromData(ctx, object.attributeContext);
        }
        utils.addArrayToCdmCollection(entity.attributes, utils.createAttributeArray(ctx, object.hasAttributes, entity.entityName));
        return entity;
    }
    static toData(instance, resOpt, options) {
        const exhibitsTraits = instance.exhibitsTraits ?
            instance.exhibitsTraits.allItems.filter((trait) => trait instanceof internal_1.CdmTraitGroupReference || !trait.isFromProperty) : undefined;
        const object = {
            explanation: instance.explanation,
            entityName: instance.entityName,
            extendsEntity: instance.extendsEntity ?
                instance.extendsEntity.copyData(resOpt, options) : undefined,
            extendsEntityResolutionGuidance: instance.extendsEntityResolutionGuidance ?
                instance.extendsEntityResolutionGuidance.copyData(resOpt, options) : undefined,
            exhibitsTraits: copyDataUtils.arrayCopyData(resOpt, exhibitsTraits, options)
        };
        if (instance.sourceName) {
            object.sourceName = instance.getProperty('sourceName');
        }
        if (instance.displayName) {
            object.displayName = instance.getProperty('displayName');
        }
        if (instance.description) {
            object.description = instance.getProperty('description');
        }
        if (instance.version) {
            object.version = instance.getProperty('version');
        }
        if (instance.cdmSchemas) {
            object.cdmSchemas = instance.getProperty('cdmSchemas');
        }
        // after the properties so they show up first in doc
        if (instance.attributes) {
            object.hasAttributes =
                copyDataUtils.arrayCopyData(resOpt, instance.attributes, options);
            object.attributeContext = instance.attributeContext ?
                instance.attributeContext.copyData(resOpt, options) : undefined;
        }
        return object;
    }
}
exports.EntityPersistence = EntityPersistence;
//# sourceMappingURL=EntityPersistence.js.map