"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.ReferencedEntityDeclarationPersistence = void 0;
const internal_1 = require("../../internal");
const copyDataUtils = require("../../Utilities/CopyDataUtils");
const timeUtils = require("../../Utilities/timeUtils");
const types_1 = require("./types");
const utils = require("./utils");
class ReferencedEntityDeclarationPersistence {
    /**
     * Creates an instance of referenced entity declartion from data object.
     * @param ctx The context.
     * @param object The object to get data from.
     */
    static fromData(ctx, prefixPath, dataObj) {
        const newRef = ctx.corpus.MakeObject(internal_1.cdmObjectType.referencedEntityDeclarationDef, dataObj.entityName);
        let entityPath = dataObj.entityPath !== undefined ? dataObj.entityPath : dataObj.entityDeclaration;
        if (entityPath === undefined) {
            internal_1.Logger.error(ctx, this.TAG, this.fromData.name, undefined, internal_1.cdmLogCode.ErrPersistEntityPathNotFound, dataObj.entityName);
        }
        // The entity path has to be absolute.
        // If the namespace is not present then add the "prefixPath" which has the absolute folder path.
        if (entityPath !== undefined && entityPath.indexOf(':/') === -1) {
            entityPath = `${prefixPath}${entityPath}`;
        }
        newRef.entityPath = entityPath;
        if (dataObj.lastFileStatusCheckTime) {
            newRef.lastFileStatusCheckTime = new Date(dataObj.lastFileStatusCheckTime);
        }
        if (dataObj.lastFileModifiedTime) {
            newRef.lastFileModifiedTime = new Date(dataObj.lastFileModifiedTime);
        }
        if (dataObj.explanation) {
            newRef.explanation = dataObj.explanation;
        }
        utils.addArrayToCdmCollection(newRef.exhibitsTraits, utils.createTraitReferenceArray(ctx, dataObj.exhibitsTraits));
        return newRef;
    }
    static toData(instance, resOpt, options) {
        return {
            type: types_1.entityDeclarationDefinitionType.referencedEntity,
            lastFileStatusCheckTime: timeUtils.getFormattedDateString(instance.lastFileStatusCheckTime),
            lastFileModifiedTime: timeUtils.getFormattedDateString(instance.lastFileModifiedTime),
            explanation: instance.explanation,
            entityName: instance.entityName,
            entityPath: instance.entityPath,
            exhibitsTraits: copyDataUtils.arrayCopyData(resOpt, instance.exhibitsTraits, options)
        };
    }
}
exports.ReferencedEntityDeclarationPersistence = ReferencedEntityDeclarationPersistence;
ReferencedEntityDeclarationPersistence.TAG = ReferencedEntityDeclarationPersistence.name;
//# sourceMappingURL=ReferencedEntityDeclarationPersistence.js.map