import { createJDLApplication } from '../../core/models/jdl-application-factory.js';
import { applicationOptions as ApplicationOptions } from '../../../jhipster/index.js';
import { convertOptions } from './option-converter.js';
const { OptionNames: { BASE_NAME }, } = ApplicationOptions;
export default { convertApplications };
export function convertApplications(parsedApplications, runtime) {
    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, runtime);
        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.`);
            }
        });
    });
}
