import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import fs from 'fs'

const stgToken = 'C1qziFGlIv3tnCQxcFaStrLuZOO2ZZXjN7FB_G0WlrOLjclfObbSaXAKzl4RWwQBf_0Zhsm0CoVvdVsYMD18iM_LJrxtn7LHJJQuF9UoUuF3fvqOwrG4EF6Z4GahtxtQ2oeaPQBBNKlgVW1xUW7tkhEdXWqzDHPA_I_91Lczk0PI4guhx1c88Hst4-HI8pdMbiUdEJzj3d3a2W06Fa0XA9Q0taAwaRd1k9jUrDVyj9GfS84_SIgJF4SPjWVfsraV79ieb_StgRcUwZjbscGPMlifnJD6F00wwNbxG7AuCHbl3EtMfSed1vuVx3AsizIckwzIVSVRpOGw72cdAMui-I6es9Ozj2ITzSa5KgyXEpX4qCHF1VcCM1wlHLQ_5hLnJIi4r8NsnJPsxMYrTw'

const prdToken = 'mVcexzY_mjtGAL5_exPlmAyfOJxuuEthWY1mk9tUFC_HwceY58uRZ2WDhz7-ttexCdUtFN8C7V636_jIq6fzaSfqIj8OQyhUPKPMa2eZjLlblT77ySqBt_lYM6iEAhrj7-raGmySMmkLS4Rqh651Ak2tqmUbjS64cqv5ofMsuadOCg1J-CtLFt7NeSoU4N3Kpm5MJ_4sOFBhQGfBym88dcwxosFl9LbvhpyleXFf6fOZkkOj0l2X8Nr2pfNjYs3_VOmCQxrxXh1XZ_a1v9qj5_rA9k9wGNNQfmr2JwJTUT4V9NwtNq94gNFt8C0J6MWKVE2eyr25Rke8tkKu3CGNNmspmEFpr6LavPlaWnWOIh9CRJ1cIDB70pg_JD2l0nPTkPbtaTQaIGTz'

// https://vite.dev/config/
export default defineConfig({
  plugins: [vue()],
  server: {
    host: '0.0.0.0',
    proxy: {
      '/openapi-stg': {
        target: 'https://fls-ai-stg-sit.pingan.com.cn/openapi', // 设置代理目标
        changeOrigin: true,
        rewrite: (path) => {
          // 提取原始路径
          const originalPath = path.replace(/^\/openapi-stg/, '');
          // 判断是否包含查询参数，如果有则追加 &，否则追加 ?
          const separator = originalPath.indexOf('?') === -1 ? '?' : '&';
          // 拼接 token 参数
          return `${originalPath}${separator}token=${stgToken}&channelId=ASP-TEST&sceneId=ASP-TEST`;
        }
      },
      '/openapi-prd': {
        target: 'https://fls-ai.pingan.com.cn/openapi', // 设置代理目标
        changeOrigin: true,
        //rewrite: (path) => path.replace(/^\/openapi-prd/, '') // 重写路径
        rewrite: (path) => {
          // 提取原始路径
          const originalPath = path.replace(/^\/openapi-prd/, '');
          // 判断是否包含查询参数，如果有则追加 &，否则追加 ?
          const separator = originalPath.indexOf('?') === -1 ? '?' : '&';
          // 拼接 token 参数
          return `${originalPath}${separator}token=${prdToken}&channelId=ASP-TEST&sceneId=ASP-TEST`;
        }
      },
      '/testTts': {
        target: 'http://192.168.92.190:5001/', // 设置代理目标
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/testTts/, '') // 重写路径
      }
    }
  }
})
