const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { ModuleFederationPlugin } = require('webpack').container;
const webpack = require('webpack');

// Import the microfrontends registry
const { getExposesConfig } = require('../../web/src/microfrontends.js');

module.exports = {
  mode: 'development',
  entry: './src/index.tsx',
  devServer: {
    port: 3002,
    headers: {
      'Access-Control-Allow-Origin': '*',
    },
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js', '.jsx'],
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx|ts|tsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              '@babel/preset-react',
              '@babel/preset-typescript',
            ],
          },
        },
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
    ],
  },
  plugins: [
    new ModuleFederationPlugin({
      name: 'kms',
      filename: 'remoteEntry.js',
      exposes: getExposesConfig('kms'),
      shared: {
        react: {
          singleton: true,
          requiredVersion: '^18.2.0',
          eager: false,
        },
        'react-dom': {
          singleton: true,
          requiredVersion: '^18.2.0',
          eager: false,
        },
        '@mantine/core': {
          singleton: true,
          requiredVersion: '^7.0.0',
          eager: false,
        },
        '@mantine/hooks': {
          singleton: true,
          requiredVersion: '^7.0.0',
          eager: false,
        },
        '@mantine/notifications': {
          singleton: true,
          requiredVersion: '^7.0.0',
          eager: false,
        },
        '@tabler/icons-react': {
          singleton: true,
          requiredVersion: '^2.40.0',
          eager: false,
        },
      },
    }),
    new HtmlWebpackPlugin({
      template: './public/index.html',
    }),
    new webpack.DefinePlugin({
      'process.env': JSON.stringify(process.env),
    }),
  ],
};
