import {ConfigEnv, UserConfigExport} from 'vite'
import vue from '@vitejs/plugin-vue'
import {viteMockServe} from "vite-plugin-mock";
import { viteVConsole } from 'vite-plugin-vconsole';
import * as path from 'path'

type Modes = "local" | "moicen" | "huiwing";

const Environments = {
    local: {
        host: 'music-room.localhost',
        upyun_domain: 'dev.moicen.com',
        upyun_bucket: 'local-dev',
        wx_domain: 'wx.localhost',
        wx_app_id: 'wx30919c9c9bb48e44', // 公众号app_id
        wx_mini_app_id: 'wx3b02c886bb554012', // 小程序app_id
        ngx_server: 'http://admin.localhost:8088',
        uc_server: 'http://admin.localhost:8088',
        ws_server: 'http://admin.localhost:8088',
        ts_server: 'http://ts.localhost:8088',
        kc_server: 'http://ts.localhost:8088',
        ai_server: 'http://localhost:5000',
        assets_base: "/"
    },
    moicen: {
        host: 'music-room.moicen.com',
        upyun_domain: 'moicen.com',
        upyun_bucket: 'moicen',
        wx_domain: 'wx.moicen.com',
        wx_app_id: 'wx30919c9c9bb48e44',
        wx_mini_app_id: 'wx612c8fc526341c0d',
        ngx_server: 'https://admin.moicen.com',
        uc_server: 'https://admin.moicen.com',
        ws_server: 'https://admin.moicen.com',
        kc_server: 'https://admin.moicen.com',
        ts_server: 'https://ts.moicen.com',
        ai_server: 'https://ai.alchemy-studio.cn',
        // ai_server: 'http://localhost:5000',
        // 与 OpenResty 发布的 music-room 同目录部署 dist，勿依赖未同步的 static.moicen.com 资源路径
        assets_base: "/"
    },
    huiwing: {
        host: 'music-room.huiwings.cn',
        upyun_domain: 'huiwings.cn',
        upyun_bucket: 'huiwing',
        wx_domain: 'wx.huiwings.cn',
        wx_app_id: 'wxfd3381693a9a3e70',
        wx_mini_app_id: 'wx3b02c886bb554012',
        ngx_server: 'https://admin.huiwings.cn',
        uc_server: 'https://admin.huiwings.cn',
        ws_server: 'https://admin.huiwings.cn',
        kc_server: 'https://admin.huiwings.cn',
        ts_server: 'https://ts.huiwings.cn',
        ai_server: 'https://ai.alchemy-studio.cn',
        assets_base: "https://static.huiwings.cn"
    }
}

const config = Environments[(process.env.mode || "local") as Modes];

export default ({command}: ConfigEnv): UserConfigExport => {
    return {
        base: config.assets_base,
        plugins: [
            vue(),
            viteMockServe({mockPath: 'mock', enable: false}),
            viteVConsole({
                entry: path.resolve('src/main.ts'),
                localEnabled: false,
                enabled: true,
                config: {theme: 'dark'}
            })
        ],
        define: {
            HOST: JSON.stringify(config.host),
            UPYUN_DOMAIN: JSON.stringify(config.upyun_domain),
            UPYUN_BUCKET: JSON.stringify(config.upyun_bucket),
            WX_DOMAIN: JSON.stringify(config.wx_domain),
            WX_APP_ID: JSON.stringify(config.wx_app_id),
            WX_MINI_APP_ID: JSON.stringify(config.wx_mini_app_id),
            NGX_SERVER: JSON.stringify(config.ngx_server),
            UC_SERVER: JSON.stringify(config.uc_server),
            WS_SERVER: JSON.stringify(config.ws_server),
            KC_SERVER: JSON.stringify(config.kc_server),
            TS_SERVER: JSON.stringify(config.ts_server),
            AI_SERVER: JSON.stringify(config.ai_server)
        },
        server: {
            port: 8000,
            proxy: {
                '/api/v1/coze': {
                    target: config.ai_server,
                    changeOrigin: true,
                },
            },
        },
        css: {
            preprocessorOptions: {
                less: {
                    lessOptions: {
                        javascriptEnabled: true
                    }
                }
            }
        },
        resolve: {
            alias: {
                '~': require('path').join(__dirname, 'src'),
                '@authcore/commons': path.resolve(
                    __dirname,
                    'node_modules/@authcore/commons/src/index.ts'
                ),
            }
        },
        optimizeDeps: {
            exclude: ['@authcore/commons'],
        }
    }
}
