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: any[] = [basicSsl()];
plugins.push(tailwindcss());
const devBackend = 'https://kevisual.silkyai.cn';
const meBackend = 'https://kevisual.xiongxiao.me';
// const meBackend = 'https://kevisual.cn';
// const backend = isDev ? devBackend : meBackend;
const backendWss = devBackend.replace(/^https:/, 'wss:');
const backend = meBackend;
let proxy = {};
if (false) {
  proxy = {
    '/api': {
      target: backend,
      changeOrigin: true,
      ws: true,
      cookieDomainRewrite: 'localhost',
      rewrite: (path: any) => path.replace(/^\/api/, '/api'),
    },
    '/user/login': {
      target: backend,
      changeOrigin: true,
      cookieDomainRewrite: 'localhost',
      rewrite: (path: any) => path.replace(/^\/user/, '/user'),
    },
    '/test': {
      target: backend,
    },
  };
}

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), ...plugins],

  css: {
    postcss: {},
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
  define: {
    DEV_SERVER: JSON.stringify(process.env.NODE_ENV === 'development'),
  },
  base: isDev ? '/' : '/root/center/',
  build: {
    rollupOptions: {
      output: {
        // 控制输出
        // 在rollup里面, hash代表将你的文件名和文件内容进行组合计算得来的结果
        assetFileNames: (chunkInfo) => {
          console.log(chunkInfo.names);
          if (chunkInfo.names?.includes('panda.png')) {
            return '[name].[ext]';
          }
          const qrcode = ['qrcode-8x8.jpg'];
          const names = chunkInfo.names || [];
          if (qrcode.some((name) => names.includes(name))) {
            return '[name].[ext]';
          }
          return '[name].[hash].[ext]';
        },
      },
    },
  },
  server: {
    port: 6020,
    host: '0.0.0.0',
    proxy: {
      '/system/lib': {
        target: 'https://kevisual.xiongxiao.me',
        changeOrigin: true,
      },
      '/api': {
        target: 'http://localhost:4005',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '/api'),
      },
      ...proxy,
    },
  },
});
