"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityPersistence = void 0;
const __1 = require("..");
const internal_1 = require("../../internal");
const Logger_1 = require("../../Utilities/Logging/Logger");
const extensionHelper = require("./ExtensionHelper");
const types_1 = require("./types");
class EntityPersistence {
    static fromData(ctx, object, extensionTraitDefList, localExtensionTraitDefList) {
        return __awaiter(this, void 0, void 0, function* () {
            const entity = ctx.corpus.MakeObject(internal_1.cdmObjectType.entityDef, object.name);
            if (object.description && object.description.trim() !== '') {
                entity.description = object.description;
            }
            yield __1.ModelJson.utils.processAnnotationsFromData(ctx, object, entity.exhibitsTraits);
            const localEntity = object;
            if (localEntity.attributes) {
                for (const element of localEntity.attributes) {
                    const typeAttribute = yield __1.ModelJson.TypeAttributePersistence.fromData(ctx, element, extensionTraitDefList, localExtensionTraitDefList);
                    if (typeAttribute !== undefined) {
                        entity.attributes.push(typeAttribute);
                    }
                    else {
                        Logger_1.Logger.error(ctx, this.TAG, this.fromData.name, null, internal_1.cdmLogCode.ErrPersistModelJsonToAttrConversionFailure);
                        return undefined;
                    }
                }
            }
            extensionHelper.processExtensionFromJson(ctx, localEntity, types_1.localEntityBaseProperties, entity.exhibitsTraits, extensionTraitDefList, localExtensionTraitDefList);
            return entity;
        });
    }
    static toData(instance, resOpt, options, ctx) {
        return __awaiter(this, void 0, void 0, function* () {
            const result = {
                $type: 'LocalEntity',
                name: instance.entityName,
                description: instance.getProperty('description'),
                isHidden: undefined,
                annotations: undefined,
                'cdm:traits': undefined,
                attributes: undefined,
                partitions: undefined,
                schemas: undefined,
                'cdm:imports': undefined
            };
            __1.ModelJson.utils.processTraitsAndAnnotationsToData(instance.ctx, result, instance.exhibitsTraits);
            if (instance.attributes !== undefined) {
                result.attributes = [];
                for (const element of instance.attributes.allItems) {
                    if (element.objectType !== internal_1.cdmObjectType.typeAttributeDef) {
                        Logger_1.Logger.error(ctx, this.TAG, this.toData.name, null, internal_1.cdmLogCode.ErrPersistModelJsonEntityAttrError);
                        return undefined;
                    }
                    const attribute = yield __1.ModelJson.TypeAttributePersistence.toData(element, resOpt, options);
                    if (attribute !== undefined) {
                        result.attributes.push(attribute);
                    }
                    else {
                        Logger_1.Logger.error(ctx, this.TAG, this.toData.name, null, internal_1.cdmLogCode.ErrPersistModelJsonFromAttrConversionFailure);
                        return undefined;
                    }
                }
            }
            return result;
        });
    }
}
exports.EntityPersistence = EntityPersistence;
EntityPersistence.TAG = EntityPersistence.name;
//# sourceMappingURL=EntityPersistence.js.map