"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.EntityAttributePersistence = void 0;
const __1 = require("..");
const internal_1 = require("../../internal");
const copyDataUtils = require("../../Utilities/CopyDataUtils");
const ProjectionPersistence_1 = require("./Projections/ProjectionPersistence");
const utils = require("./utils");
class EntityAttributePersistence {
    static fromData(ctx, object) {
        const entityAttribute = ctx.corpus.MakeObject(internal_1.cdmObjectType.entityAttributeDef, object.name);
        entityAttribute.description = utils.propertyFromDataToString(object.description);
        entityAttribute.displayName = utils.propertyFromDataToString(object.displayName);
        entityAttribute.explanation = utils.propertyFromDataToString(object.explanation);
        entityAttribute.cardinality = utils.cardinalitySettingsFromData(object.cardinality, entityAttribute);
        entityAttribute.isPolymorphicSource = object.isPolymorphicSource;
        if (object.entity && typeof (object.entity) !== 'string' && 'source' in object.entity) {
            const inlineEntityRef = ctx.corpus.MakeObject(internal_1.cdmObjectType.entityRef, undefined);
            inlineEntityRef.explicitReference = ProjectionPersistence_1.ProjectionPersistence.fromData(ctx, object.entity);
            entityAttribute.entity = inlineEntityRef;
        }
        else {
            entityAttribute.entity = __1.CdmFolder.EntityReferencePersistence.fromData(ctx, object.entity);
        }
        entityAttribute.purpose = __1.CdmFolder.PurposeReferencePersistence.fromData(ctx, object.purpose);
        utils.addArrayToCdmCollection(entityAttribute.appliedTraits, utils.createTraitReferenceArray(ctx, object.appliedTraits));
        // Ignore resolution guidance if the entity is a projection
        if (object.resolutionGuidance && object.entity && typeof (object.entity) !== 'string' && 'source' in object.entity) {
            internal_1.Logger.error(ctx, this.TAG, this.fromData.name, null, internal_1.cdmLogCode.ErrPersistEntityAttrUnsupported, entityAttribute.name);
        }
        else {
            entityAttribute.resolutionGuidance =
                __1.CdmFolder.AttributeResolutionGuidancePersistence.fromData(ctx, object.resolutionGuidance);
        }
        return entityAttribute;
    }
    static toData(instance, resOpt, options) {
        let entity;
        entity = instance.entity ? instance.entity.copyData(resOpt, options) : undefined;
        const appliedTraits = instance.appliedTraits ?
            instance.appliedTraits.allItems.filter((trait) => trait instanceof internal_1.CdmTraitGroupReference || !trait.isFromProperty) : undefined;
        const object = {
            name: instance.name,
            description: instance.description,
            displayName: instance.displayName,
            explanation: instance.explanation,
            isPolymorphicSource: instance.isPolymorphicSource,
            purpose: instance.purpose
                ? instance.purpose.copyData(resOpt, options)
                : undefined,
            entity: entity,
            appliedTraits: copyDataUtils.arrayCopyData(resOpt, appliedTraits, options),
            resolutionGuidance: instance.resolutionGuidance
                ? instance.resolutionGuidance.copyData(resOpt, options) : undefined,
            cardinality: utils.cardinalitySettingsToData(instance.cardinality)
        };
        return object;
    }
}
exports.EntityAttributePersistence = EntityAttributePersistence;
EntityAttributePersistence.TAG = EntityAttributePersistence.name;
//# sourceMappingURL=EntityAttributePersistence.js.map