"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.cardinalitySettingsToData = exports.cardinalitySettingsFromData = exports.propertyFromDataToBool = exports.propertyFromDataToInt = exports.propertyFromDataToString = exports.copyIdentifierRef = exports.createAttributeArray = exports.createAttribute = exports.createConstant = exports.addArrayToCdmCollection = exports.createTraitReferenceArray = void 0;
const internal_1 = require("../../internal");
const __1 = require("..");
function isTypeAttribute(object) {
    return !('entity' in object);
}
function isEntityAttribute(object) {
    return 'entity' in object;
}
function isAttributeGroupReference(object) {
    return 'attributeGroupReference' in object;
}
/**
 * Converts a JSON object to a CdmCollection of TraitReferences and TraitGroupReferences
 */
function createTraitReferenceArray(ctx, object) {
    if (!object || !object.map) {
        return;
    }
    const result = [];
    object.forEach((traitReference) => {
        if (typeof traitReference !== 'string' && 'traitGroupReference' in traitReference) {
            result.push(__1.CdmFolder.TraitGroupReferencePersistence.fromData(ctx, traitReference));
        }
        else {
            result.push(__1.CdmFolder.TraitReferencePersistence.fromData(ctx, traitReference));
        }
    });
    return result;
}
exports.createTraitReferenceArray = createTraitReferenceArray;
/**
 * Adds all elements of an array to a CdmCollection
 */
function addArrayToCdmCollection(cdmCollection, array) {
    if (cdmCollection && array) {
        for (const element of array) {
            cdmCollection.push(element);
        }
    }
}
exports.addArrayToCdmCollection = addArrayToCdmCollection;
/**
 * Creates a CDM object from a JSON object
 */
function createConstant(ctx, object) {
    if (object === undefined || object === null) {
        return undefined;
    }
    if (typeof object === 'string') {
        return object;
    }
    else {
        const objectproperties = Object.getOwnPropertyNames(object);
        const checkExistingProperty = (propertyName) => {
            return objectproperties.some((element) => {
                return element === propertyName;
            });
        };
        if (checkExistingProperty('purpose') || checkExistingProperty('dataType') || checkExistingProperty('entity')) {
            if (checkExistingProperty('dataType')) {
                return __1.CdmFolder.TypeAttributePersistence.fromData(ctx, object);
            }
            else if (checkExistingProperty('entity')) {
                return __1.CdmFolder.EntityAttributePersistence.fromData(ctx, object);
            }
            else {
                return object;
            }
        }
        else if (checkExistingProperty('purposeReference')) {
            return __1.CdmFolder.PurposeReferencePersistence.fromData(ctx, object);
        }
        else if (checkExistingProperty('traitReference')) {
            return __1.CdmFolder.TraitReferencePersistence.fromData(ctx, object);
        }
        else if (checkExistingProperty('traitGroupReference')) {
            return __1.CdmFolder.TraitGroupReferencePersistence.fromData(ctx, object);
        }
        else if (checkExistingProperty('dataTypeReference')) {
            return __1.CdmFolder.DataTypeReferencePersistence.fromData(ctx, object);
        }
        else if (checkExistingProperty('entityReference')) {
            return __1.CdmFolder.EntityReferencePersistence.fromData(ctx, object);
        }
        else if (checkExistingProperty('attributeGroupReference')) {
            return __1.CdmFolder.AttributeGroupReferencePersistence.fromData(ctx, object);
        }
        else {
            return object;
        }
    }
}
exports.createConstant = createConstant;
/**
 * Converts a JSON object to an Attribute object
 */
function createAttribute(ctx, object, entityName) {
    if (!object) {
        return undefined;
    }
    if (typeof object === 'string' || isAttributeGroupReference(object)) {
        return __1.CdmFolder.AttributeGroupReferencePersistence.fromData(ctx, object, entityName);
    }
    else if (isEntityAttribute(object)) {
        return __1.CdmFolder.EntityAttributePersistence.fromData(ctx, object);
    }
    else if (isTypeAttribute(object)) {
        return __1.CdmFolder.TypeAttributePersistence.fromData(ctx, object, entityName);
    }
}
exports.createAttribute = createAttribute;
/**
 * Converts a JSON object to a CdmCollection of attributes
 */
function createAttributeArray(ctx, object, entityName) {
    if (!object) {
        return undefined;
    }
    const result = [];
    const l = object.length;
    for (let i = 0; i < l; i++) {
        const ea = object[i];
        result.push(createAttribute(ctx, ea, entityName));
    }
    return result;
}
exports.createAttributeArray = createAttributeArray;
/**
 * Create a copy of the reference object
 */
function copyIdentifierRef(objRef, resOpt, options) {
    const identifier = objRef.namedReference;
    if (!options || !options.stringRefs) {
        return identifier;
    }
    const resolved = objRef.fetchObjectDefinition(resOpt);
    if (resolved === undefined) {
        return identifier;
    }
    return {
        corpusPath: resolved.atCorpusPath,
        identifier: identifier
    };
}
exports.copyIdentifierRef = copyIdentifierRef;
/**
 * Converts dynamic input into a string for a property (ints are converted to string)
 * @param value The value that should be converted to a string.
 */
function propertyFromDataToString(value) {
    if (typeof value === 'string' && value.trim() !== '') {
        return value;
    }
    else if (typeof value === 'number') {
        return value.toString();
    }
    return undefined;
}
exports.propertyFromDataToString = propertyFromDataToString;
/**
 * Converts dynamic input into an int for a property (numbers represented as strings are converted to int)
 * @param value The value that should be converted to an int.
 */
function propertyFromDataToInt(value) {
    if (typeof value === 'number') {
        return value;
    }
    else if (typeof value === 'string') {
        const numberValue = Number(value);
        if (!isNaN(numberValue)) {
            return numberValue;
        }
    }
    return undefined;
}
exports.propertyFromDataToInt = propertyFromDataToInt;
/**
 * Converts dynamic input into a boolean for a property (booleans represented as strings are converted to boolean)
 * @param value The value that should be converted to a boolean.
 */
function propertyFromDataToBool(value) {
    if (typeof value === 'boolean') {
        return value;
    }
    else if (typeof value === 'string') {
        if (value === 'true' || value === 'True') {
            return true;
        }
        else if (value === 'false' || value === 'False') {
            return false;
        }
    }
    return undefined;
}
exports.propertyFromDataToBool = propertyFromDataToBool;
/**
 * Converts cardinality data into a CardinalitySettings object
 * @param object The dasta representation of CardinalitySettings.
 * @param attribute The attribute object where the cardinality object belongs.
 * @returns The CardinalitySettings object.
 */
function cardinalitySettingsFromData(object, attribute) {
    if (!object) {
        return;
    }
    const cardinality = new internal_1.CardinalitySettings(attribute);
    cardinality.minimum = object.minimum;
    cardinality.maximum = object.maximum;
    return cardinality.minimum !== undefined && cardinality.maximum !== undefined ?
        cardinality : undefined;
}
exports.cardinalitySettingsFromData = cardinalitySettingsFromData;
/**
 * Converts CardinalitySettings into a CardinalitySettingsData object
 * @param instance The CardinalitySettings object.
 */
function cardinalitySettingsToData(instance) {
    if (!instance || instance.minimum === undefined || instance.maximum === undefined) {
        return undefined;
    }
    return {
        minimum: instance.minimum,
        maximum: instance.maximum
    };
}
exports.cardinalitySettingsToData = cardinalitySettingsToData;
//# sourceMappingURL=utils.js.map