"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.DataPartitionPersistence = void 0;
const internal_1 = require("../../internal");
const copyDataUtils = require("../../Utilities/CopyDataUtils");
const timeUtils = require("../../Utilities/timeUtils");
const utils = require("./utils");
class DataPartitionPersistence {
    /**
     * Creates an instance from data object.
     * @param ctx The context.
     * @param dataObj The data object.
     * @returns A CdmDataPartitionDefinition instance.
     */
    static fromData(ctx, dataObj) {
        const newPartition = ctx.corpus.MakeObject(internal_1.cdmObjectType.dataPartitionDef);
        newPartition.location = dataObj.location;
        if (dataObj.name) {
            newPartition.name = dataObj.name;
        }
        if (dataObj.specializedSchema) {
            newPartition.specializedSchema = dataObj.specializedSchema;
        }
        if (dataObj.lastFileStatusCheckTime) {
            newPartition.lastFileStatusCheckTime = new Date(dataObj.lastFileStatusCheckTime);
        }
        if (dataObj.lastFileModifiedTime) {
            newPartition.lastFileModifiedTime = new Date(dataObj.lastFileModifiedTime);
        }
        utils.addArrayToCdmCollection(newPartition.exhibitsTraits, utils.createTraitReferenceArray(ctx, dataObj.exhibitsTraits));
        if (dataObj.arguments) {
            for (const argument of dataObj.arguments) {
                let argName;
                let argValue;
                const entries = Object.entries(argument);
                if (argument.name !== undefined) {
                    argName = argument.name;
                    argValue = argument.value;
                }
                else if (argument['key'] !== undefined) {
                    argName = argument['key'];
                    argValue = argument.value;
                }
                else if (entries.length === 1) {
                    argName = entries[0][0];
                    argValue = entries[0][1];
                }
                if (!argName || !argValue) {
                    internal_1.Logger.warning(ctx, this.TAG, this.fromData.name, undefined, internal_1.cdmLogCode.WarnPartitionInvalidArguments, dataObj.location.toString());
                }
                if (newPartition.arguments.has(argName)) {
                    newPartition.arguments.get(argName)
                        .push(argValue);
                }
                else {
                    newPartition.arguments.set(argName, [argValue]);
                }
            }
        }
        return newPartition;
    }
    static toData(instance, resOpt, options) {
        const dataCopy = {
            name: instance.name,
            location: instance.location,
            lastFileStatusCheckTime: timeUtils.getFormattedDateString(instance.lastFileStatusCheckTime),
            lastFileModifiedTime: timeUtils.getFormattedDateString(instance.lastFileModifiedTime),
            exhibitsTraits: copyDataUtils.arrayCopyData(resOpt, instance.exhibitsTraits, options),
            arguments: undefined,
            specializedSchema: instance.specializedSchema
        };
        const argumentsCopy = [];
        if (instance.arguments) {
            instance.arguments.forEach((argValueList, key) => {
                argValueList.forEach((argValue) => {
                    argumentsCopy.push({ name: key, value: argValue });
                });
            });
        }
        dataCopy.arguments = argumentsCopy.length > 0 ? argumentsCopy : undefined;
        return dataCopy;
    }
}
exports.DataPartitionPersistence = DataPartitionPersistence;
DataPartitionPersistence.TAG = DataPartitionPersistence.name;
//# sourceMappingURL=DataPartitionPersistence.js.map