import BasicEntityConverter from './jdl-to-json-basic-entity-converter.js';
import FieldConverter from './jdl-to-json-field-converter.js';
import RelationshipConverter from './jdl-to-json-relationship-converter.js';
import OptionConverter from './jdl-to-json-option-converter.js';
let entities;
let jdlObject;
export default {
    convert,
};
export function convert(args = {}) {
    if (!args.jdlObject || !args.applicationName || !args.databaseType) {
        throw new Error("The JDL object, the application's name and its the database type are mandatory.");
    }
    init(args);
    setBasicEntityInformation();
    setOptions();
    setFields();
    setRelationships();
    setApplicationToEntities();
    return new Map([[args.applicationName, Object.values(entities)]]);
}
function init(args) {
    if (jdlObject) {
        resetState();
    }
    jdlObject = args.jdlObject;
    entities = {};
}
function resetState() {
    jdlObject = null;
    entities = null;
}
function setBasicEntityInformation() {
    const convertedEntities = BasicEntityConverter.convert(jdlObject.getEntities());
    convertedEntities.forEach((jsonEntity, entityName) => {
        entities[entityName] = jsonEntity;
    });
}
function setOptions() {
    const convertedOptionContents = OptionConverter.convert(jdlObject);
    convertedOptionContents.forEach((optionContent, entityName) => {
        if (!entities[entityName]) {
            console.log('---entityName-notfound--', entityName);
            return;
        }
        entities[entityName].setOptions(optionContent);
    });
}
function setFields() {
    const convertedFields = FieldConverter.convert(jdlObject);
    convertedFields.forEach((entityFields, entityName) => {
        entities[entityName].addFields(entityFields);
    });
}
function setRelationships() {
    const convertedRelationships = RelationshipConverter.convert(jdlObject.getRelationships(), jdlObject.getEntityNames());
    convertedRelationships.forEach((entityRelationships, entityName) => {
        entities[entityName].addRelationships(entityRelationships);
    });
}
function setApplicationToEntities() {
    Object.keys(entities).forEach(entityName => {
        entities[entityName].applications = '*';
    });
}
