import { GENERATOR_NAME } from '../export-utils.js';
export function formatApplicationsToExport(applications, configuration) {
    if (!applications) {
        throw new Error('Applications have to be passed to be exported.');
    }
    return Object.values(applications).map(application => {
        return setUpApplicationStructure(application, configuration);
    });
}
export function formatApplicationToExport(application, configuration = {}) {
    return setUpApplicationStructure(application, configuration);
}
function setUpApplicationStructure(application) {
    let applicationToExport = {
        [GENERATOR_NAME]: {},
    };
    applicationToExport[GENERATOR_NAME] = getApplicationConfig(application);
    applicationToExport.entities = application.getEntityNames();
    if (application.hasConfigurationOption('creationTimestamp')) {
        applicationToExport[GENERATOR_NAME].creationTimestamp = parseInt(application.getConfigurationOptionValue('creationTimestamp'), 10);
    }
    applicationToExport = cleanUpOptions(applicationToExport);
    return applicationToExport;
}
function getApplicationConfig(application) {
    const result = {};
    application.forEachConfigurationOption(option => {
        result[option.name] = option.getValue();
    });
    return result;
}
function cleanUpOptions(application) {
    if (!application[GENERATOR_NAME].frontEndBuilder) {
        delete application[GENERATOR_NAME].frontEndBuilder;
    }
    delete application.entityNames;
    if (application[GENERATOR_NAME].blueprints) {
        application[GENERATOR_NAME].blueprints = application[GENERATOR_NAME].blueprints.map(blueprintName => ({
            name: blueprintName,
        }));
    }
    if (application[GENERATOR_NAME].microfrontends) {
        application[GENERATOR_NAME].microfrontends = application[GENERATOR_NAME].microfrontends.map(baseName => ({
            baseName,
        }));
    }
    return application;
}
