/// <reference types="vitest" />
import path from 'path';

import react from '@vitejs/plugin-react';
import { visualizer } from 'rollup-plugin-visualizer';
import { loadEnv } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import wasm from 'vite-plugin-wasm';
import { defineConfig } from 'vitest/config';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  process.env = { ...process.env, ...loadEnv(mode, process.cwd()) };
  const base = process.env.VITE_BASENAME;
  return {
    base: base?.length != 0 ? base : '/',
    build: {
      sourcemap: true,
      chunkSizeWarningLimit: Infinity,
      target: 'ESNEXT',
      minify: 'esbuild',
      rollupOptions: {
        treeshake: true,
        output: {
          manualChunks: {
            react: ['react', 'react-dom', 'react-router-dom'],
            cms: ['contract.cm'],
          },
        },
        onwarn(warning, warn) {
          if (warning.code === 'INVALID_ANNOTATION') {
            // Suppress if the message contains "__PURE__"
            if (warning.message.includes('__PURE__')) {
              return;
            }
          }
          // Suppress eval warning from vm-browserify
          if (warning.message.includes('Use of eval in "../../node_modules/vm-browserify/index.js"')) {
            return;
          }
          warn(warning);
        },
      },
    },
    plugins: [
      react(),
      wasm(),
      nodePolyfills({
        include: ['process', 'buffer', 'vm', 'crypto', 'stream', 'assert', 'http', 'https', 'os', 'url', 'util'],
      }),
      visualizer({ open: false, filename: 'bundle-visualization.html' }),
    ],
    server: {
      port: 3000,
      fs: {
        cachedChecks: false,
      },
      proxy: {
        '/api/getTvl': {
          target: `${process.env.VITE_CWAP_API_URL}/cwap_cm/getTvl`,
          changeOrigin: true,
          rewrite: (path) => {
            const result = path.replace('/api/getTvl', '');
            const searchParams = new URLSearchParams({
              btcC1: process.env.VITE_L2_CONTRACT_ADDRESS_BASE_BTC ?? '',
              btcC2: process.env.VITE_L2_CONTRACT_ADDRESS_MAKER_BTC ?? '',
              ethC1: process.env.VITE_L2_CONTRACT_ADDRESS_BASE_ETH ?? '',
              ethC2: process.env.VITE_L2_CONTRACT_ADDRESS_MAKER_ETH ?? '',
              usdtEthC1: process.env.VITE_L2_CONTRACT_ADDRESS_BASE_USDT_ETH ?? '',
              usdtEthC2: process.env.VITE_L2_CONTRACT_ADDRESS_MAKER_USDT_ETH ?? '',
              usdtBnbC1: process.env.VITE_L2_CONTRACT_ADDRESS_BASE_USDT_BNB ?? '',
              usdtBnbC2: process.env.VITE_L2_CONTRACT_ADDRESS_MAKER_USDT_BNB ?? '',
              bnbC1: process.env.VITE_L2_CONTRACT_ADDRESS_BASE_BNB ?? '',
              bnbC2: process.env.VITE_L2_CONTRACT_ADDRESS_MAKER_BNB ?? '',
            });
            return result + '?' + searchParams.toString();
          },
        },
      },
    },
    test: {
      globals: true,
      environment: 'jsdom',
      setupFiles: './vitest.setup.ts',
      silent: true,
    },
    define: {
      __MOCKS__: false,
    },
    resolve: {
      alias: {
        process: 'process/browser',
        buffer: 'buffer',
        crypto: 'crypto-browserify',
        stream: 'stream-browserify',
        assert: 'assert',
        http: 'stream-http',
        https: 'https-browserify',
        os: 'os-browserify',
        url: 'url',
        util: 'util',
        '@': path.resolve(__dirname, './src'),
      },
    },
  };
});
