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