export const addApplicationIndex = (applicationsWithEntities) => {
    applicationsWithEntities.forEach((applicationWithEntities, applicationIndex) => {
        applicationWithEntities.config.applicationIndex = applicationIndex;
    });
};
export const customizeForMicroservices = (applicationsWithEntities) => {
    const gatewayApplications = Object.values(applicationsWithEntities).filter(app => app.config.applicationType === 'gateway');
    for (const gateway of gatewayApplications) {
        const { microfrontends = [], clientFramework: gatewayClientFramework, serverPort: gatewayServerPort } = gateway.config;
        for (const mf of microfrontends) {
            const { baseName, clientFramework } = applicationsWithEntities[mf.baseName].config;
            if (clientFramework !== gatewayClientFramework) {
                throw new Error(`Using different client frameworks in microfrontends is not supported. Tried to use: ${gatewayClientFramework} with ${clientFramework} (${baseName})`);
            }
        }
        const relatedBaseNames = microfrontends.map(mf => mf.baseName);
        gateway.entities
            .map(entity => entity.microserviceName)
            .filter(Boolean)
            .forEach(basename => !relatedBaseNames.includes(basename) && relatedBaseNames.push(basename));
        if (relatedBaseNames.length > 0) {
            gateway.config.applications = Object.fromEntries(relatedBaseNames.map(baseName => {
                const appConfig = applicationsWithEntities[baseName]?.config ?? {};
                appConfig.gatewayServerPort = appConfig.gatewayServerPort || gatewayServerPort;
                const { clientFramework, serverPort, applicationIndex, devServerPort } = appConfig;
                return [baseName, { clientFramework, serverPort, applicationIndex, devServerPort }];
            }));
        }
    }
};
