"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, undefined, 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) {
                const dataPartitionDef = __1.CdmFolder.DataPartitionPersistence.fromData(ctx, dataPartition);
                if (dataPartitionDef.isIncremental) {
                    this.errorMessage(ctx, this.fromData.name, null, dataPartitionDef, true);
                }
                else {
                    localDec.dataPartitions.push(dataPartitionDef);
                }
            }
        }
        if (dataObj.dataPartitionPatterns) {
            for (const pattern of dataObj.dataPartitionPatterns) {
                const dataPartitionPatternDef = __1.CdmFolder.DataPartitionPatternPersistence.fromData(ctx, pattern);
                if (dataPartitionPatternDef.isIncremental) {
                    this.errorMessage(ctx, this.fromData.name, null, dataPartitionPatternDef, true);
                }
                else {
                    localDec.dataPartitionPatterns.push(dataPartitionPatternDef);
                }
            }
        }
        if (dataObj.incrementalPartitions) {
            for (const incrementalPartition of dataObj.incrementalPartitions) {
                const incrementalPartitionDef = __1.CdmFolder.DataPartitionPersistence.fromData(ctx, incrementalPartition);
                if (!incrementalPartitionDef.isIncremental) {
                    this.errorMessage(ctx, this.fromData.name, null, incrementalPartitionDef, false);
                }
                else {
                    localDec.incrementalPartitions.push(incrementalPartitionDef);
                }
            }
        }
        if (dataObj.incrementalPartitionPatterns) {
            for (const incrementalPattern of dataObj.incrementalPartitionPatterns) {
                const incrementalPatternDef = __1.CdmFolder.DataPartitionPatternPersistence.fromData(ctx, incrementalPattern);
                if (!incrementalPatternDef.isIncremental) {
                    this.errorMessage(ctx, this.fromData.name, null, incrementalPatternDef, false);
                }
                else {
                    localDec.incrementalPartitionPatterns.push(incrementalPatternDef);
                }
            }
        }
        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, this.ensureNonIncremental(instance)),
            dataPartitionPatterns: copyDataUtils.arrayCopyData(resOpt, instance.dataPartitionPatterns, options, this.ensureNonIncremental(instance)),
            incrementalPartitions: copyDataUtils.arrayCopyData(resOpt, instance.incrementalPartitions, options, this.ensureIncremental(instance)),
            incrementalPartitionPatterns: copyDataUtils.arrayCopyData(resOpt, instance.incrementalPartitionPatterns, options, this.ensureIncremental(instance))
        };
    }
    static errorMessage(ctx, methodName, corpusPath, obj, isIncremental) {
        const propertyName = obj instanceof internal_1.CdmDataPartitionDefinition
            ? (isIncremental ? 'dataPartitions' : 'incrementalPartitions')
            : (isIncremental ? 'dataPartitionPatterns' : 'incrementalPartitionPatterns');
        if (isIncremental) {
            Logger_1.Logger.error(ctx, this.TAG, methodName, corpusPath, internal_1.cdmLogCode.ErrPersistIncrementalConversionError, obj.name, internal_1.constants.INCREMENTAL_TRAIT_NAME, propertyName);
        }
        else {
            Logger_1.Logger.error(ctx, this.TAG, methodName, corpusPath, internal_1.cdmLogCode.ErrPersistNonIncrementalConversionError, obj.name, internal_1.constants.INCREMENTAL_TRAIT_NAME, propertyName);
        }
    }
    static ensureNonIncremental(instance) {
        const compare = (obj) => {
            if ((obj instanceof internal_1.CdmDataPartitionDefinition && obj.isIncremental) ||
                (obj instanceof internal_1.CdmDataPartitionPatternDefinition && obj.isIncremental)) {
                this.errorMessage(instance.ctx, this.toData.name, instance.atCorpusPath, obj, true);
                return false;
            }
            return true;
        };
        return compare;
    }
    static ensureIncremental(instance) {
        const compare = (obj) => {
            if ((obj instanceof internal_1.CdmDataPartitionDefinition && !obj.isIncremental) ||
                (obj instanceof internal_1.CdmDataPartitionPatternDefinition && !obj.isIncremental)) {
                this.errorMessage(instance.ctx, this.toData.name, instance.atCorpusPath, obj, false);
                return false;
            }
            return true;
        };
        return compare;
    }
}
exports.LocalEntityDeclarationPersistence = LocalEntityDeclarationPersistence;
LocalEntityDeclarationPersistence.TAG = LocalEntityDeclarationPersistence.name;
//# sourceMappingURL=LocalEntityDeclarationPersistence.js.map