import { defineConfig } from '@umijs/max';
import proxyDev from './config/proxy_dev';
import proxyProd from './config/proxy_prod';
import {
  alarmsRoute,
  commonManagerRoutes,
  forgotPasswordRoute,
  loginRoute,
  managerRouteBase,
  notFoundRoute,
} from './config/routes';

const envConfig = process.env as { REACT_APP_ENV?: 'dev' | 'test' | 'prod' };
const rawEnv = envConfig.REACT_APP_ENV;
const isProdBuild = process.env.NODE_ENV === 'production';
const resolvedEnv = isProdBuild && !rawEnv ? 'prod' : rawEnv || 'dev';
const proxyConfig = isProdBuild ? proxyProd : proxyDev;
const routes = [
  loginRoute,
  forgotPasswordRoute,
  alarmsRoute,
  {
    ...managerRouteBase,
    routes: commonManagerRoutes,
  },
  notFoundRoute,
];

export default defineConfig({
  antd: {},
  access: {},
  model: {},
  initialState: {},
  request: {},
  tailwindcss: {},
  layout: {
    title: 'BaseProject',
  },
  extraBabelPlugins:
    process.env.NODE_ENV === 'production'
      ? [['transform-remove-console', { exclude: ['error'] }]]
      : [],
  mfsu: false,
  locale: {
    useLocalStorage: true,
    default: 'vi-VN',
    baseNavigator: false,
    antd: true,
    title: false,
    baseSeparator: '-',
  },
  // Only use base routes when not in a specific environment that provides its own routes
  routes: process.env.DOMAIN_ENV ? undefined : routes,
  npmClient: 'pnpm',
  // Use the dev configuration from proxy_dev
  proxy: proxyConfig[resolvedEnv],
  define: {
    'process.env.DOMAIN_ENV': process.env.DOMAIN_ENV,
  },
});
