"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.DataPartitionPersistence = void 0;
const __1 = require("..");
const internal_1 = require("../../internal");
const timeUtils = require("../../Utilities/timeUtils");
const ExtensionHelper_1 = require("./ExtensionHelper");
const types_1 = require("./types");
class DataPartitionPersistence {
    static fromData(ctx, object, extensionTraitDefList, localExtensionTraitDefList, documentFolder) {
        return __awaiter(this, void 0, void 0, function* () {
            const dataPartition = ctx.corpus.MakeObject(internal_1.cdmObjectType.dataPartitionDef, object.name);
            if (!internal_1.StringUtils.isBlankByCdmStandard(object.description)) {
                dataPartition.description = object.description;
            }
            dataPartition.location = ctx.corpus.storage.createRelativeCorpusPath(ctx.corpus.storage.adapterPathToCorpusPath(object.location), documentFolder);
            dataPartition.refreshTime = object.refreshTime;
            if (object['cdm:lastFileModifiedTime'] !== undefined) {
                dataPartition.lastFileModifiedTime = new Date(object['cdm:lastFileModifiedTime']);
            }
            if (object['cdm:lastFileStatusCheckTime'] !== undefined) {
                dataPartition.lastFileStatusCheckTime = new Date(object['cdm:lastFileStatusCheckTime']);
            }
            if (internal_1.StringUtils.isBlankByCdmStandard(dataPartition.location)) {
                internal_1.Logger.warning(ctx, this.TAG, this.fromData.name, null, internal_1.cdmLogCode.WarnPersistPartitionLocMissing, dataPartition.name);
            }
            if (object.isHidden === true) {
                const isHiddenTrait = ctx.corpus.MakeRef(internal_1.cdmObjectType.traitRef, 'is.hidden', true);
                dataPartition.exhibitsTraits.push(isHiddenTrait);
            }
            yield __1.ModelJson.utils.processAnnotationsFromData(ctx, object, dataPartition.exhibitsTraits);
            let csvFormatTrait = dataPartition.exhibitsTraits.item('is.partition.format.CSV');
            if (object.fileFormatSettings !== undefined && object.fileFormatSettings.$type === 'CsvFormatSettings') {
                const partitionTraitExisted = !!csvFormatTrait;
                csvFormatTrait = __1.ModelJson.utils.createCsvTrait(object.fileFormatSettings, ctx, csvFormatTrait);
                if (csvFormatTrait === undefined) {
                    internal_1.Logger.error(ctx, this.TAG, this.fromData.name, null, internal_1.cdmLogCode.ErrPersistCsvProcessingError);
                    return undefined;
                }
                if (!partitionTraitExisted) {
                    dataPartition.exhibitsTraits.push(csvFormatTrait);
                }
            }
            ExtensionHelper_1.processExtensionFromJson(ctx, object, types_1.partitionBaseProperties, dataPartition.exhibitsTraits, extensionTraitDefList, localExtensionTraitDefList);
            return dataPartition;
        });
    }
    static toData(instance, resOpt, options) {
        return __awaiter(this, void 0, void 0, function* () {
            const t2pm = new internal_1.traitToPropertyMap(instance);
            const isHiddenTrait = t2pm.fetchTraitReference('is.hidden');
            const result = {
                name: instance.name,
                description: instance.explanation,
                annotations: undefined,
                location: instance.ctx.corpus.storage.corpusPathToAdapterPath(instance.ctx.corpus.storage.createAbsoluteCorpusPath(instance.location, instance.inDocument)),
                refreshTime: instance.refreshTime,
                fileFormatSettings: undefined,
                'cdm:traits': undefined,
                'cdm:lastFileModifiedTime': timeUtils.getFormattedDateString(instance.lastFileModifiedTime),
                'cdm:lastFileStatusCheckTime': timeUtils.getFormattedDateString(instance.lastFileStatusCheckTime)
            };
            if (result.name === undefined) {
                internal_1.Logger.warning(instance.ctx, this.TAG, this.toData.name, instance.atCorpusPath, internal_1.cdmLogCode.WarnPersistPartitionNameNull);
                result.name = '';
            }
            if (internal_1.StringUtils.isBlankByCdmStandard(result.location)) {
                internal_1.Logger.warning(instance.ctx, this.TAG, this.toData.name, instance.atCorpusPath, internal_1.cdmLogCode.WarnPersistPartitionLocMissing, result.name);
            }
            yield __1.ModelJson.utils.processTraitsAndAnnotationsToData(instance.ctx, result, instance.exhibitsTraits);
            if (isHiddenTrait) {
                result.isHidden = true;
            }
            const csvTrait = t2pm.fetchTraitReference('is.partition.format.CSV');
            if (csvTrait) {
                const csvFormatSettings = __1.ModelJson.utils.createCsvFormatSettings(csvTrait);
                if (csvFormatSettings !== undefined) {
                    result.fileFormatSettings = csvFormatSettings;
                    result.fileFormatSettings.$type = 'CsvFormatSettings';
                }
                else {
                    internal_1.Logger.error(instance.ctx, this.TAG, this.toData.name, instance.atCorpusPath, internal_1.cdmLogCode.ErrPersistCsvProcessingError);
                    return undefined;
                }
            }
            return result;
        });
    }
}
exports.DataPartitionPersistence = DataPartitionPersistence;
DataPartitionPersistence.TAG = DataPartitionPersistence.name;
//# sourceMappingURL=DataPartitionPersistence.js.map