"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.ReferencedEntityDeclarationPersistence = void 0;
const internal_1 = require("../../internal");
const types_1 = require("./types");
const Logger_1 = require("../../Utilities/Logging/Logger");
const timeUtils = require("../../Utilities/timeUtils");
const ExtensionHelper_1 = require("./ExtensionHelper");
const utils_1 = require("./utils");
class ReferencedEntityDeclarationPersistence {
    static fromData(ctx, dataObj, location) {
        return __awaiter(this, void 0, void 0, function* () {
            const referencedEntity = ctx.corpus.MakeObject(internal_1.cdmObjectType.referencedEntityDeclarationDef, dataObj.name);
            referencedEntity.entityName = dataObj.name;
            const corpusPath = ctx.corpus.storage.adapterPathToCorpusPath(location);
            if (corpusPath === undefined) {
                Logger_1.Logger.error(ctx, this.TAG, this.fromData.name, null, internal_1.cdmLogCode.ErrPersistModelJsonRefEntityInvalidLocation, location, referencedEntity.entityName);
                return undefined;
            }
            referencedEntity.entityPath = `${corpusPath}/${dataObj.source}`;
            referencedEntity.explanation = dataObj.description;
            if (dataObj['cdm:lastFileStatusCheckTime']) {
                referencedEntity.lastFileStatusCheckTime = new Date(dataObj['cdm:lastFileStatusCheckTime']);
            }
            if (dataObj['cdm:lastFileModifiedTime']) {
                referencedEntity.lastFileModifiedTime = new Date(dataObj['cdm:lastFileModifiedTime']);
            }
            yield utils_1.processAnnotationsFromData(ctx, dataObj, referencedEntity.exhibitsTraits);
            if (dataObj.isHidden !== undefined) {
                const isHiddenTrait = ctx.corpus.MakeObject(internal_1.cdmObjectType.traitRef, 'is.hidden', true);
                isHiddenTrait.isFromProperty = true;
                referencedEntity.exhibitsTraits.push(isHiddenTrait);
            }
            const trait = ctx.corpus.MakeObject(internal_1.cdmObjectType.traitRef, 'is.propertyContent.multiTrait', false);
            trait.isFromProperty = true;
            const argument = ctx.corpus.MakeObject(internal_1.cdmObjectType.argumentDef, 'modelId');
            argument.value = dataObj.modelId;
            trait.arguments.push(argument);
            referencedEntity.exhibitsTraits.push(trait);
            const extensionTraitDefList = [];
            const extensionTraits = new internal_1.CdmTraitCollection(ctx, referencedEntity);
            ExtensionHelper_1.processExtensionFromJson(ctx, dataObj, types_1.referenceEntityBaseProperties, extensionTraits, extensionTraitDefList);
            if (extensionTraitDefList.length > 0) {
                Logger_1.Logger.warning(ctx, this.TAG, this.fromData.name, null, internal_1.cdmLogCode.WarnPersistCustomExtNotSupported);
            }
            return referencedEntity;
        });
    }
    static toData(instance, resOpt, options) {
        return __awaiter(this, void 0, void 0, function* () {
            const referenceEntity = {};
            const t2pm = new internal_1.traitToPropertyMap(instance);
            const isHidden = t2pm.fetchTraitReference('is.hidden');
            const propertiesTrait = t2pm.fetchTraitReference('is.propertyContent.multiTrait');
            const sourceIndex = instance.entityPath.lastIndexOf('/');
            if (sourceIndex === -1) {
                Logger_1.Logger.error(instance.ctx, this.TAG, this.toData.name, instance.atCorpusPath, internal_1.cdmLogCode.ErrPersistModelJsonEntityRefConversionError, instance.entityName);
                return Promise.reject('Source name is not present in entityDeclaration path.');
            }
            referenceEntity.$type = 'ReferenceEntity';
            referenceEntity.name = instance.entityName;
            referenceEntity.source = instance.entityPath.slice(sourceIndex + 1);
            referenceEntity.description = instance.explanation;
            referenceEntity['cdm:lastFileStatusCheckTime'] = timeUtils.getFormattedDateString(instance.lastFileStatusCheckTime);
            referenceEntity['cdm:lastFileModifiedTime'] = timeUtils.getFormattedDateString(instance.lastFileModifiedTime);
            if (isHidden !== undefined) {
                referenceEntity.isHidden = true;
            }
            utils_1.processTraitsAndAnnotationsToData(instance.ctx, referenceEntity, instance.exhibitsTraits);
            if (propertiesTrait) {
                referenceEntity.modelId = propertiesTrait.arguments.allItems[0].value;
            }
            return referenceEntity;
        });
    }
}
exports.ReferencedEntityDeclarationPersistence = ReferencedEntityDeclarationPersistence;
ReferencedEntityDeclarationPersistence.TAG = ReferencedEntityDeclarationPersistence.name;
//# sourceMappingURL=ReferencedEntityDeclarationPersistence.js.map