import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';

let targetUrl = 'http://192.168.2.156:10010/'
//targetUrl = 'https://attiya.fun/ebike/'
targetUrl = 'https://www.cdzhuojing.cn/'
// https://vite.dev/config/
export default defineConfig({
    plugins: [
        vue()
        , Components({
            resolvers: [AntDesignVueResolver({
                importStyle: 'less',// 启用LESS主题定制 
                resolveIcons: true
            })],
        })
    ],
    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url))
        }
    },
    base: './',
    build: {
        assetsDir: 'assets'
    },
    server: {
        proxy: {
            '^/operate/*': {
                changeOrigin: true,
                target: targetUrl,
                rewrite: (path) => {
                    return path.replace(/^\/operate/, 'operate')
                }
                // ,bypass(req, res, options) {
                //   //@ts-ignore
                //   const proxyUrl = new URL(options.rewrite(req.url) || '',(options.target))?.href || '';
                //   console.log(proxyUrl);
                //   req.headers['x-req-proxyUrl'] = proxyUrl
                //   res.setHeader('x-res-proxyUrl',proxyUrl)
                // }
            },
            '^/maintenance/*': {
                changeOrigin: true,
                target: targetUrl,
                rewrite: (path) => {
                    return path.replace(/^\/maintenance/, 'maintenance')
                }
                // ,bypass(req, res, options) {
                //   //@ts-ignore
                //   const proxyUrl = new URL(options.rewrite(req.url) || '',(options.target))?.href || '';
                //   console.log(proxyUrl);
                //   req.headers['x-req-proxyUrl'] = proxyUrl
                //   res.setHeader('x-res-proxyUrl',proxyUrl)
                // }
            },
            '^/user/*': {
                changeOrigin: true,
                target: targetUrl,
                rewrite: (path) => {
                    return path.replace(/^\/user/, 'user')
                }
                // ,bypass(req, res, options) {
                //   //@ts-ignore
                //   const proxyUrl = new URL(options.rewrite(req.url) || '',(options.target))?.href || '';
                //   console.log(proxyUrl);
                //   req.headers['x-req-proxyUrl'] = proxyUrl
                //   res.setHeader('x-res-proxyUrl',proxyUrl)
                // }
            },
            '^/order/*': {
                changeOrigin: true,
                target: targetUrl,
                rewrite: (path) => {
                    return path.replace(/^\/order/, 'order')
                }
                // ,bypass(req, res, options) {
                //   //@ts-ignore
                //   const proxyUrl = new URL(options.rewrite(req.url) || '',(options.target))?.href || '';
                //   console.log(proxyUrl);
                //   req.headers['x-req-proxyUrl'] = proxyUrl
                //   res.setHeader('x-res-proxyUrl',proxyUrl)
                // }
            }
        },
        cors: true,
        host: '0.0.0.0'
    }
})
