import process from 'node:process';
import { URL, fileURLToPath } from 'node:url';
import { defineConfig, loadEnv } from 'vite';
import { setupVitePlugins } from './build/plugins';
import { createViteProxy, getBuildTime } from './build/config';

// export const proxyTarget = 'http://192.168.3.248:9996';
export const proxyTarget = 'http://10.0.20.81:9996';
// export const proxyTarget = 'http://192.168.1.119:9996';
// export const proxyTarget = 'http://192.168.1.119:9996';


// 视频地址
export const videdUrl = 'ws://10.0.20.81:9999';

// 固定接口地址
export const apiUrl = 'http://10.0.20.81:9996';

// WS 地址
export const wsUrl = 'ws://10.0.20.81:9999';


export default defineConfig(configEnv => {

  const viteEnv = loadEnv(configEnv.mode, process.cwd()) as unknown as Env.ImportMeta;

  const buildTime = getBuildTime();

  return {
    base: viteEnv.VITE_BASE_URL,
    resolve: {
      alias: {
        '~': fileURLToPath(new URL('./', import.meta.url)),
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "./src/styles/scss/global.scss" as *;`
        }
      }
    },
    plugins: setupVitePlugins(viteEnv, buildTime),
    define: {
      BUILD_TIME: JSON.stringify(buildTime)
    },
    server: {
      host: '0.0.0.0',
      port: 9527,
      open: true,
      // 集成别人封装的代理项 & 晶合项目的代理项
      proxy: {
        ...createViteProxy(viteEnv, configEnv.command === 'serve'),
        // '/menus': 'http://192.168.1.102:9995/menus',
        // '/menus': 'http://192.168.1.173:10000/menus'
        '/v1': {
          target: proxyTarget,
          changeOrigin: true
        }
      },
      fs: {
        cachedChecks: false
      }
    },
    preview: {
      port: 9725
    },
    build: {
      reportCompressedSize: false,
      sourcemap: viteEnv.VITE_SOURCE_MAP === 'Y',
      commonjsOptions: {
        ignoreTryCatch: false
      }
    }
  };
});

// 123456zy@
