import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import tailwindcss from '@tailwindcss/vite';
import basicSsl from '@vitejs/plugin-basic-ssl';

const isDev = process.env.NODE_ENV === 'development';
const plugins = [basicSsl()];
// const plugins = [];
plugins.push(tailwindcss());
let proxy = {};
if (true) {
  proxy = {
    '/api': {
      target: 'https://kevisual.silkyai.cn',
      changeOrigin: true,
      ws: true,
      cookieDomainRewrite: 'localhost',
      rewrite: (path) => path.replace(/^\/api/, '/api'),
    },
    '/api/router': {
      target: 'wss://kevisual.silkyai.cn',
      changeOrigin: true,
      ws: true,
      rewriteWsOrigin: true,
      rewrite: (path) => path.replace(/^\/api/, '/api'),
    },
    '/user/login': {
      target: 'https://kevisual.silkyai.cn',
      changeOrigin: true,
      cookieDomainRewrite: 'localhost',
      rewrite: (path) => path.replace(/^\/user/, '/user'),
    },
  };
}
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), ...plugins],

  css: {
    postcss: {},
  },
  resolve: {
    alias: {
      '@kevisual/resources': path.resolve(__dirname, './src'),
      // 'react/jsx-dev-runtime': 'https://cdn.jsdelivr.net/npm/react/jsx-dev-runtime/+esm',
      // 'react/jsx-runtime': 'https://cdn.jsdelivr.net/npm/react/jsx-runtime/+esm',
      // 'react/jsx-runtime': path.resolve(__dirname, './node_modules/react/jsx-runtime'),
      // 'react/jsx-dev-runtime': path.resolve(__dirname, './node_modules/react/jsx-dev-runtime'),
      // 'react-dom/client': 'https://cdn.jsdelivr.net/npm/react-dom/client/+esm',
      // react: 'https://cdn.jsdelivr.net/npm/react@19.0.0/+esm',
      // 'react-dom': 'https://cdn.jsdelivr.net/npm/react-dom@19.0.0/+esm',
    },
  },
  define: {
    DEV_SERVER: JSON.stringify(process.env.NODE_ENV === 'development'),
    BASE_NAME: JSON.stringify('/root/resources/'),
  },
  base: './',
  // base: isDev ? '/' : '/root/resources/',
  build: {
    rollupOptions: {
      // external: ['react', 'react-dom'],
    },
  },
  server: {
    port: 6022,
    host: '0.0.0.0',
    proxy: {
      '/terminal': {
        target: 'http://localhost:3000',
        changeOrigin: true,
        ws: true,
      },
      '/system/lib': {
        target: 'https://kevisual.xiongxiao.me',
        changeOrigin: true,
      },
      '/api': {
        target: 'http://localhost:4005',
        changeOrigin: true,
        ws: true,
        cookieDomainRewrite: 'localhost',
        rewrite: (path) => path.replace(/^\/api/, '/api'),
      },
      '/api/router': {
        target: 'ws://localhost:4005',
        changeOrigin: true,
        ws: true,
        rewriteWsOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '/api'),
      },
      ...proxy,
    },
  },
});
