import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'

export default defineConfig({
  plugins: [react()],
  server: {
    port: 3000,
    proxy: {
      '/api': {
        target: 'http://localhost:8000',
        changeOrigin: true,
      },
      '/ws': {
        target: 'ws://localhost:8000',
        ws: true,
      },
      '/openclaw': {
        target: 'http://localhost:8000',
        changeOrigin: true,
        ws: true,
      },
    },
  },
  build: {
    // Accepted oversized chunks (measured 2026-07-28):
    //   vendor      ~2.12 MB min / ~650 kB gzip — react + @ant-design/charts
    //                (charts CANNOT be split out: forced separate chunk creates
    //                a circular reference and a runtime TDZ crash, and charts
    //                loads on the default Home route anyway)
    //   vendor-antd ~620 kB min / ~173 kB gzip
    // Warning fires only above this limit, i.e. on a real size regression.
    chunkSizeWarningLimit: 2200,
    rollupOptions: {
      output: {
        manualChunks(id) {
          // Monaco Editor — only used by Scenarios page, cleanly isolated.
          if (id.includes('@monaco-editor') || id.includes('monaco-editor')) {
            return 'vendor-monaco'
          }
          // Ant Design core — used everywhere, one-way deps (no circular chunk).
          if (id.includes('antd') || id.includes('@ant-design/icons')) {
            return 'vendor-antd'
          }
          // Everything else (React, @ant-design/charts + @antv/*, etc.) → vendor.
          // NOTE: do NOT split @ant-design/charts into its own chunk — its deep
          // @antv dependency graph creates a circular chunk reference that causes
          // a runtime TDZ crash ("Cannot access 'd' before initialization").
          // Returning undefined for @antv doesn't help either: rollup still
          // merges it into vendor (verified 2026-07-28).
          if (id.includes('node_modules')) {
            return 'vendor'
          }
        },
      },
    },
  },
  test: {
    environment: 'jsdom',
    setupFiles: './src/test/setup.ts',
  },
})
