"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.ParameterPersistence = void 0;
const internal_1 = require("../../internal");
const __1 = require("..");
const utils = require("./utils");
class ParameterPersistence {
    static fromData(ctx, object) {
        const parameter = ctx.corpus.MakeObject(internal_1.cdmObjectType.parameterDef, object.name);
        parameter.explanation = object.explanation;
        parameter.required = object.required ? object.required : false;
        parameter.defaultValue = utils.createConstant(ctx, object.defaultValue);
        parameter.dataTypeRef = __1.CdmFolder.DataTypeReferencePersistence.fromData(ctx, object.dataType);
        return parameter;
    }
    static toData(instance, resOpt, options) {
        let defVal;
        if (instance.defaultValue) {
            if (typeof (instance.defaultValue) === 'object' && 'copyData' in instance.defaultValue
                && typeof (instance.defaultValue.copyData) === 'function') {
                defVal = instance.defaultValue.copyData(resOpt, options);
            }
            else if (typeof (instance.defaultValue) === 'object' && typeof (instance.defaultValue) === 'string') {
                defVal = instance.defaultValue;
            }
        }
        const result = {
            name: instance.name,
            dataType: instance.dataTypeRef ? instance.dataTypeRef.copyData(resOpt, options) : undefined
        };
        if (instance.explanation !== undefined) {
            result.explanation = instance.explanation;
        }
        if (instance.defaultValue !== undefined) {
            result.defaultValue = defVal;
        }
        if (instance.required !== undefined) {
            result.required = instance.required;
        }
        return result;
    }
}
exports.ParameterPersistence = ParameterPersistence;
//# sourceMappingURL=ParameterPersistence.js.map