"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.LocalEntityDeclarationPersistence = void 0;
const __1 = require("..");
const internal_1 = require("../../internal");
const copyDataUtils = require("../../Utilities/CopyDataUtils");
const Logger_1 = require("../../Utilities/Logging/Logger");
const timeUtils = require("../../Utilities/timeUtils");
const types_1 = require("./types");
const utils = require("./utils");
class LocalEntityDeclarationPersistence {
    /**
     * Creates an instance of the local entity declaration from data.
     * @param ctx The context.
     * @param object The object to read the data from.
     * @returns a local entity declartion instance.
     */
    static fromData(ctx, prefixPath, dataObj) {
        const localDec = ctx.corpus.MakeObject(internal_1.cdmObjectType.localEntityDeclarationDef, dataObj.entityName);
        let entityPath = dataObj.entityPath;
        // check for the old format, it has to be there then.
        if (!entityPath) {
            entityPath = dataObj.entitySchema;
            if (!entityPath) {
                Logger_1.Logger.error(ctx, this.TAG, this.fromData.name, null, internal_1.cdmLogCode.ErrPersistEntityPathNotFound, dataObj.entityName);
            }
        }
        localDec.entityPath = entityPath;
        if (dataObj.lastFileStatusCheckTime) {
            localDec.lastFileStatusCheckTime = new Date(dataObj.lastFileStatusCheckTime);
        }
        if (dataObj.lastFileModifiedTime) {
            localDec.lastFileModifiedTime = new Date(dataObj.lastFileModifiedTime);
        }
        if (dataObj.lastChildFileModifiedTime) {
            localDec.lastChildFileModifiedTime = new Date(dataObj.lastChildFileModifiedTime);
        }
        if (dataObj.explanation) {
            localDec.explanation = dataObj.explanation;
        }
        utils.addArrayToCdmCollection(localDec.exhibitsTraits, utils.createTraitReferenceArray(ctx, dataObj.exhibitsTraits));
        if (dataObj.dataPartitions) {
            for (const dataPartition of dataObj.dataPartitions) {
                localDec.dataPartitions.push(__1.CdmFolder.DataPartitionPersistence.fromData(ctx, dataPartition));
            }
        }
        if (dataObj.dataPartitionPatterns) {
            for (const pattern of dataObj.dataPartitionPatterns) {
                localDec.dataPartitionPatterns.push(__1.CdmFolder.DataPartitionPatternPersistence.fromData(ctx, pattern));
            }
        }
        return localDec;
    }
    static toData(instance, resOpt, options) {
        return {
            type: types_1.entityDeclarationDefinitionType.localEntity,
            entityName: instance.entityName,
            explanation: instance.explanation,
            lastFileStatusCheckTime: timeUtils.getFormattedDateString(instance.lastFileStatusCheckTime),
            lastFileModifiedTime: timeUtils.getFormattedDateString(instance.lastFileModifiedTime),
            lastChildFileModifiedTime: timeUtils.getFormattedDateString(instance.lastChildFileModifiedTime),
            exhibitsTraits: copyDataUtils.arrayCopyData(resOpt, instance.exhibitsTraits, options),
            entityPath: instance.entityPath,
            dataPartitions: copyDataUtils.arrayCopyData(resOpt, instance.dataPartitions, options),
            dataPartitionPatterns: copyDataUtils.arrayCopyData(resOpt, instance.dataPartitionPatterns, options)
        };
    }
}
exports.LocalEntityDeclarationPersistence = LocalEntityDeclarationPersistence;
LocalEntityDeclarationPersistence.TAG = LocalEntityDeclarationPersistence.name;
//# sourceMappingURL=LocalEntityDeclarationPersistence.js.map