// Centralized registry for all microfrontends
// This file contains the single source of truth for all microfrontend configurations

// Define all microfrontends
export const microFrontends = {
  demo: {
    name: 'demo',
    url: 'http://localhost:3001',
    port: 3001,
    exposedModule: './src/App',
    importPath: 'demo/src/App',
  },
  kms: {
    name: 'kms',
    url: 'http://localhost:3002',
    port: 3002,
    exposedModule: './src/App',
    importPath: 'kms/src/App',
  },
  faas: {
    name: 'faas',
    url: 'http://localhost:3003',
    port: 3003,
    exposedModule: './src/App',
    importPath: 'faas/src/App',
  },
  user: {
    name: 'user',
    url: 'http://localhost:3004',
    port: 3004,
    exposedModule: './src/App',
    importPath: 'user/src/App',
  },
};

// Generate remotes configuration for Module Federation
export const getRemotesConfig = () => {
  const remotes = {};
  
  Object.values(microFrontends).forEach(mf => {
    remotes[mf.name] = `${mf.name}@${mf.url}/remoteEntry.js`;
  });
  
  return remotes;
};

// Generate exposes configuration for Module Federation (for microfrontends)
export const getExposesConfig = (appName) => {
  const appConfig = microFrontends[appName];
  if (!appConfig) {
    throw new Error(`Microfrontend ${appName} not found in registry`);
  }
  
  const exposes = {};
  exposes[appConfig.exposedModule] = appConfig.exposedModule;
  
  return exposes;
};

// Get app loader configuration for AppLoader component
export const getAppLoaderConfig = () => {
  return Object.keys(microFrontends).map(name => ({
    name,
    importPath: microFrontends[name].importPath,
  }));
};
