"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.TypeAttributePersistence = void 0;
const __1 = require("..");
const internal_1 = require("../../internal");
const ExtensionHelper_1 = require("./ExtensionHelper");
const types_1 = require("./types");
class TypeAttributePersistence {
    static fromData(ctx, object, extensionTraitDefList, localExtensionTraitDefList) {
        return __awaiter(this, void 0, void 0, function* () {
            const attribute = ctx.corpus.MakeObject(internal_1.cdmObjectType.typeAttributeDef, `${object.name} attribute`);
            attribute.name = object.name;
            // Do a conversion between CDM data format and model.json data type.
            attribute.dataFormat = this.dataTypeFromData(object.dataType);
            if (!internal_1.StringUtils.isBlankByCdmStandard(object.description)) {
                attribute.description = object.description;
            }
            if (object.isHidden === true) {
                const isHiddenTrait = ctx.corpus.MakeObject(internal_1.cdmObjectType.traitRef, 'is.hidden');
                isHiddenTrait.isFromProperty = true;
                attribute.appliedTraits.push(isHiddenTrait);
            }
            yield __1.ModelJson.utils.processAnnotationsFromData(ctx, object, attribute.appliedTraits);
            (0, ExtensionHelper_1.processExtensionFromJson)(ctx, object, types_1.attributeBaseProperties, attribute.appliedTraits, extensionTraitDefList, localExtensionTraitDefList);
            return attribute;
        });
    }
    static toData(instance, resOpt, options) {
        return __awaiter(this, void 0, void 0, function* () {
            const attribute = {
                name: instance.name,
                description: instance.getProperty('description'),
                dataType: this.dataTypeToData(instance.dataFormat),
                annotations: undefined,
                'cdm:traits': undefined
            };
            yield __1.ModelJson.utils.processTraitsAndAnnotationsToData(instance.ctx, attribute, instance.appliedTraits);
            const t2pm = new internal_1.traitToPropertyMap(instance);
            const isHiddenTrait = t2pm.fetchTraitReference('is.hidden');
            if (isHiddenTrait !== undefined) {
                attribute.isHidden = true;
            }
            return attribute;
        });
    }
    // case insensitive for input
    static dataTypeFromData(dataType) {
        switch (dataType.toLowerCase()) {
            case 'string':
                return internal_1.cdmDataFormat.string;
            case 'int64':
                return internal_1.cdmDataFormat.int64;
            case 'double':
                return internal_1.cdmDataFormat.double;
            case 'date':
                return internal_1.cdmDataFormat.date;
            case 'time':
                return internal_1.cdmDataFormat.time;
            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 'guid':
                return internal_1.cdmDataFormat.guid;
            case 'json':
                return internal_1.cdmDataFormat.json;
            default:
                return undefined;
        }
    }
    // outputs to camelcase
    static dataTypeToData(dataType) {
        switch (dataType) {
            case internal_1.cdmDataFormat.int16:
            case internal_1.cdmDataFormat.int32:
            case internal_1.cdmDataFormat.int64:
                return 'int64';
            case internal_1.cdmDataFormat.float:
            case internal_1.cdmDataFormat.double:
                return 'double';
            case internal_1.cdmDataFormat.char:
            case internal_1.cdmDataFormat.string:
                return 'string';
            case internal_1.cdmDataFormat.guid:
                return 'guid';
            case internal_1.cdmDataFormat.binary:
                return 'boolean';
            case internal_1.cdmDataFormat.time:
                return 'time';
            case internal_1.cdmDataFormat.date:
                return '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.json:
                return 'json';
            default:
                return 'unclassified';
        }
    }
}
exports.TypeAttributePersistence = TypeAttributePersistence;
//# sourceMappingURL=TypeAttributePersistence.js.map