import { capitalize } from 'lodash-es';
import { validations } from '../../core/built-in-options/index.js';
import { formatComment } from '../../core/utils/format-utils.js';
import { camelCase } from '../../core/utils/string-utils.js';
const { Validations: { UNIQUE, REQUIRED }, } = validations;
export default { convert };
export function convert(jdlObject) {
    if (!jdlObject) {
        throw new Error('A JDL Object must be passed to convert JDL fields to JSON.');
    }
    const convertedFields = new Map();
    jdlObject.forEachEntity(jdlEntity => {
        const convertedEntityFields = getConvertedFieldsForEntity(jdlEntity, jdlObject);
        convertedFields.set(jdlEntity.name, convertedEntityFields);
    });
    return convertedFields;
}
function getConvertedFieldsForEntity(jdlEntity, jdlObject) {
    const convertedEntityFields = [];
    jdlEntity.forEachField((jdlField) => {
        let fieldData = {
            fieldName: camelCase(jdlField.name),
            fieldType: jdlField.type,
        };
        const comment = formatComment(jdlField.comment);
        if (comment) {
            fieldData.documentation = comment;
        }
        if (jdlObject.hasEnum(jdlField.type)) {
            fieldData.fieldValues = jdlObject.getEnum(fieldData.fieldType)?.getValuesAsString();
            const fieldTypeComment = jdlObject.getEnum(fieldData.fieldType)?.comment;
            if (fieldTypeComment) {
                fieldData.fieldTypeDocumentation = fieldTypeComment;
            }
            const fieldValuesJavadocs = jdlObject.getEnum(fieldData.fieldType)?.getValueJavadocs();
            if (fieldValuesJavadocs && Object.keys(fieldValuesJavadocs).length > 0) {
                fieldData.fieldValuesJavadocs = fieldValuesJavadocs;
            }
        }
        if (jdlField.validationQuantity() !== 0) {
            const fieldValidations = getFieldValidations(jdlField);
            fieldData = {
                ...fieldData,
                ...fieldValidations,
            };
        }
        if (jdlField.optionQuantity() !== 0) {
            const fieldOptions = getOptionsForField(jdlField);
            fieldData = {
                ...fieldData,
                ...fieldOptions,
            };
        }
        convertedEntityFields.push(fieldData);
    });
    return convertedEntityFields;
}
function getFieldValidations(jdlField) {
    const fieldValidations = {
        fieldValidateRules: [],
    };
    jdlField.forEachValidation(validation => {
        fieldValidations.fieldValidateRules.push(validation.name);
        if (validation.name !== REQUIRED && validation.name !== UNIQUE) {
            fieldValidations[`fieldValidateRules${capitalize(validation.name)}`] = validation.value;
        }
    });
    return fieldValidations;
}
function getOptionsForField(jdlField) {
    const fieldOptions = {
        options: {},
    };
    fieldOptions.options = {};
    jdlField.forEachOption(([key, value]) => {
        fieldOptions.options[key] = value;
    });
    return fieldOptions;
}
