import createJDLApplication from '../../models/jdl-application-factory.js';
import { convertOptions } from './option-converter.js';
import ApplicationOptions from '../../jhipster/application-options.js';
const { OptionNames: { BASE_NAME }, } = ApplicationOptions;
export default { convertApplications };
export function convertApplications(parsedApplications) {
    if (!parsedApplications) {
        throw new Error('Applications have to be passed so as to be converted.');
    }
    return parsedApplications.map(parsedApplication => {
        const jdlApplication = createJDLApplication(parsedApplication.config, parsedApplication.namespaceConfigs);
        jdlApplication.addEntityNames(parsedApplication.entities);
        const entityOptions = getEntityOptionsInApplication(parsedApplication);
        checkEntityNamesInOptions(jdlApplication.getConfigurationOptionValue(BASE_NAME), entityOptions, parsedApplication.entities);
        entityOptions.forEach(option => jdlApplication.addOption(option));
        return jdlApplication;
    });
}
function getEntityOptionsInApplication(parsedApplication) {
    return convertOptions(parsedApplication.options, parsedApplication.useOptions);
}
function checkEntityNamesInOptions(applicationName, entityOptions, entityNamesInApplication) {
    const entityNamesInApplicationSet = new Set(entityNamesInApplication);
    entityOptions.forEach(option => {
        const entityNamesForTheOption = option.resolveEntityNames(entityNamesInApplication);
        entityNamesForTheOption.forEach(entityNameForTheOption => {
            if (!entityNamesInApplicationSet.has(entityNameForTheOption)) {
                throw new Error(`The entity ${entityNameForTheOption} in the ${option.name} option isn't declared in ${applicationName}'s entity list.`);
            }
        });
    });
}
