"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.TypeAttributePersistence = void 0;
const __1 = require("..");
const internal_1 = require("../../internal");
const copyDataUtils = require("../../Utilities/CopyDataUtils");
const utils = require("./utils");
class TypeAttributePersistence {
    static fromData(ctx, object, entityName) {
        const typeAttribute = ctx.corpus.MakeObject(internal_1.cdmObjectType.typeAttributeDef, object.name);
        if (object.explanation) {
            typeAttribute.explanation = object.explanation;
        }
        typeAttribute.purpose = __1.CdmFolder.PurposeReferencePersistence.fromData(ctx, object.purpose);
        typeAttribute.dataType = __1.CdmFolder.DataTypeReferencePersistence.fromData(ctx, object.dataType);
        typeAttribute.cardinality = utils.cardinalitySettingsFromData(object.cardinality, typeAttribute);
        typeAttribute.attributeContext =
            __1.CdmFolder.AttributeContextReferencePersistence.fromData(ctx, object.attributeContext);
        utils.addArrayToCdmCollection(typeAttribute.appliedTraits, utils.createTraitReferenceArray(ctx, object.appliedTraits));
        typeAttribute.resolutionGuidance =
            __1.CdmFolder.AttributeResolutionGuidancePersistence.fromData(ctx, object.resolutionGuidance);
        if (object.isPrimaryKey && entityName) {
            const t2pMap = new internal_1.traitToPropertyMap(typeAttribute);
            t2pMap.updatePropertyValue('isPrimaryKey', entityName + '/(resolvedAttributes)/' + typeAttribute.name);
        }
        typeAttribute.isReadOnly = utils.propertyFromDataToBool(object.isReadOnly);
        typeAttribute.isNullable = utils.propertyFromDataToBool(object.isNullable);
        typeAttribute.sourceName = utils.propertyFromDataToString(object.sourceName);
        typeAttribute.sourceOrdering = utils.propertyFromDataToInt(object.sourceOrdering);
        typeAttribute.displayName = utils.propertyFromDataToString(object.displayName);
        typeAttribute.description = utils.propertyFromDataToString(object.description);
        typeAttribute.valueConstrainedToList = utils.propertyFromDataToBool(object.valueConstrainedToList);
        typeAttribute.maximumLength = utils.propertyFromDataToInt(object.maximumLength);
        typeAttribute.maximumValue = utils.propertyFromDataToString(object.maximumValue);
        typeAttribute.minimumValue = utils.propertyFromDataToString(object.minimumValue);
        typeAttribute.projection = __1.CdmFolder.ProjectionPersistence.fromData(ctx, object.projection);
        if (object.dataFormat !== undefined) {
            typeAttribute.dataFormat = TypeAttributePersistence.dataTypeFromData(object.dataFormat);
            if (typeAttribute.dataFormat === undefined) {
                internal_1.Logger.warning(ctx, this.TAG, this.fromData.name, undefined, internal_1.cdmLogCode.WarnPersitEnumNotFound, object.dataFormat);
            }
        }
        if (object.defaultValue !== undefined) {
            typeAttribute.defaultValue = object.defaultValue;
        }
        return typeAttribute;
    }
    static toData(instance, resOpt, options) {
        if (!instance) {
            return undefined;
        }
        const appliedTraits = instance.appliedTraits ?
            instance.appliedTraits.allItems.filter((trait) => trait instanceof internal_1.CdmTraitGroupReference || !trait.isFromProperty) : undefined;
        const object = {
            explanation: instance.explanation,
            purpose: instance.purpose
                ? instance.purpose.copyData(resOpt, options)
                : undefined,
            dataType: instance.dataType ? instance.dataType.copyData(resOpt, options) : undefined,
            name: instance.name,
            appliedTraits: copyDataUtils.arrayCopyData(resOpt, appliedTraits, options),
            resolutionGuidance: instance.resolutionGuidance
                ? instance.resolutionGuidance.copyData(resOpt, options) : undefined,
            attributeContext: instance.attributeContext ? instance.attributeContext.copyData(resOpt, options) : undefined,
            cardinality: utils.cardinalitySettingsToData(instance.cardinality)
        };
        object.projection = instance.projection ? instance.projection.copyData(resOpt, options) : undefined;
        const isReadOnly = instance.getProperty('isReadOnly');
        object.isReadOnly = isReadOnly ? isReadOnly : undefined;
        const isNullable = instance.getProperty('isNullable');
        object.isNullable = isNullable ? isNullable : undefined;
        object.sourceName = instance.getProperty('sourceName');
        const sourceOrdering = instance.getProperty('sourceOrdering');
        object.sourceOrdering = !isNaN(sourceOrdering) && sourceOrdering !== 0 ? sourceOrdering : undefined;
        object.displayName = instance.getProperty('displayName');
        object.description = instance.getProperty('description');
        const valueConstrainedToList = instance.getProperty('valueConstrainedToList');
        object.valueConstrainedToList = valueConstrainedToList ? valueConstrainedToList : undefined;
        const isPrimaryKey = instance.getProperty('isPrimaryKey');
        object.isPrimaryKey = isPrimaryKey ? isPrimaryKey : undefined;
        object.maximumLength = instance.getProperty('maximumLength');
        object.maximumValue = instance.getProperty('maximumValue');
        object.minimumValue = instance.getProperty('minimumValue');
        const dataFormat = instance.getProperty('dataFormat');
        object.dataFormat = dataFormat !== internal_1.cdmDataFormat.unknown ? this.dataTypeToData(dataFormat) : undefined;
        const defaultValue = instance.getProperty('defaultValue');
        if (defaultValue instanceof Array) {
            object.defaultValue = defaultValue.length > 0 ? defaultValue : undefined;
        }
        else if (defaultValue) {
            object.defaultValue = defaultValue;
        }
        return object;
    }
    // case insensitive for input
    static dataTypeFromData(dataType) {
        switch (dataType.toLowerCase()) {
            case 'string':
                return internal_1.cdmDataFormat.string;
            case 'char':
                return internal_1.cdmDataFormat.char;
            case 'int16':
                return internal_1.cdmDataFormat.int16;
            case 'int32':
                return internal_1.cdmDataFormat.int32;
            case 'int64':
                return internal_1.cdmDataFormat.int64;
            case 'float':
                return internal_1.cdmDataFormat.float;
            case 'double':
                return internal_1.cdmDataFormat.double;
            case 'time':
                return internal_1.cdmDataFormat.time;
            case 'date':
                return internal_1.cdmDataFormat.date;
            case 'datetime':
                return internal_1.cdmDataFormat.dateTime;
            case 'datetimeoffset':
                return internal_1.cdmDataFormat.dateTimeOffset;
            case 'decimal':
                return internal_1.cdmDataFormat.decimal;
            case 'boolean':
                return internal_1.cdmDataFormat.boolean;
            case 'byte':
                return internal_1.cdmDataFormat.byte;
            case 'binary':
                return internal_1.cdmDataFormat.binary;
            case 'guid':
                return internal_1.cdmDataFormat.guid;
            case 'json':
                return internal_1.cdmDataFormat.json;
            default:
                return internal_1.cdmDataFormat.unknown;
        }
    }
    // outputs pascal case
    static dataTypeToData(dataType) {
        switch (dataType) {
            case internal_1.cdmDataFormat.int16:
                return 'Int16';
            case internal_1.cdmDataFormat.int32:
                return 'Int32';
            case internal_1.cdmDataFormat.int64:
                return 'Int64';
            case internal_1.cdmDataFormat.float:
                return 'Float';
            case internal_1.cdmDataFormat.double:
                return 'Double';
            case internal_1.cdmDataFormat.char:
                return 'Char';
            case internal_1.cdmDataFormat.string:
                return 'String';
            case internal_1.cdmDataFormat.guid:
                return 'Guid';
            case internal_1.cdmDataFormat.time:
            case internal_1.cdmDataFormat.date:
            case internal_1.cdmDataFormat.dateTime:
                return 'DateTime';
            case internal_1.cdmDataFormat.dateTimeOffset:
                return 'DateTimeOffset';
            case internal_1.cdmDataFormat.boolean:
                return 'Boolean';
            case internal_1.cdmDataFormat.decimal:
                return 'Decimal';
            case internal_1.cdmDataFormat.byte:
                return 'Byte';
            case internal_1.cdmDataFormat.binary:
                return 'Binary';
            case internal_1.cdmDataFormat.json:
                return 'Json';
            case internal_1.cdmDataFormat.unknown:
            default:
                return undefined;
        }
    }
}
exports.TypeAttributePersistence = TypeAttributePersistence;
TypeAttributePersistence.TAG = TypeAttributePersistence.name;
//# sourceMappingURL=TypeAttributePersistence.js.map