// https://nuxt.com/docs/api/configuration/nuxt-config
// 每次热更新都会执行defineNuxtConfig
import {exec} from "node:child_process";
import open, {apps} from "open";


export default defineNuxtConfig({
    vite: {
        esbuild: {
            target: 'esnext' // 或 'es2020'
        },
        optimizeDeps: {
            esbuildOptions: {
                target: 'esnext'
            }
        }
    },
    compatibilityDate: '2024-11-01',
    devtools: {enabled: true},
    modules: ['@nuxt/eslint'],
    devServer: {
        host: '0.0.0.0',
        port: 3000,
    },
    features: {
        devLogs: 'silent' // 这是默认值，确保它没有被设置为 true，日志区分server和client， false为不在client打印
    },
    css: [
        '~/assets/css/style.css',
        '~/assets/css/font.css',
        '~/assets/css/value.css',
        '~/assets/css/iconfont.css',
        '~/assets/css/transitions.css',
        '~/assets/css/Ni.css',
        'bytemd/dist/index.css', // byteMD 编辑器
    ],
    app: {
        head: {
            link: [
                {
                    // LXGW WenKai 落霞孤鹜
                    rel: "stylesheet",
                    href: 'https://chinese-fonts-cdn.deno.dev/packages/lxgwwenkai/dist/LXGWWenKai-Regular/result.css'
                },
                {
                    // Huiwen-mincho 汇文明朝体
                    rel: "stylesheet",
                    href: 'https://chinese-fonts-cdn.deno.dev/packages/hwmct/dist/%E6%B1%87%E6%96%87%E6%98%8E%E6%9C%9D%E4%BD%93/result.css'
                    //   article {
                    //     font-family:'Huiwen-mincho';
                    //     font-weight:'400'
                    // };
                },
            ]
        },
        layoutTransition: {
            name: 'indexFade',
            mode: 'out-in',
            type: 'transition', // 明确指定动画类型
            duration: {
                enter: 300,
                leave: 300
            },
            appear: true
        },
    },
    hooks: {
        'listen': (server, listener) => {
            const {address, url} = listener
            startBroswer(address.address, url)
        },
    },
    runtimeConfig: {
        redis: {
            host: process.env.REDIS_HOST || '127.0.0.1',
            port: Number(process.env.REDIS_PORT) || 6379,
            connectName: 'star-writ',
            database: Number(process.env.REDIS_DB) || 9,
            username: 'default',
            password: process.env.REDIS_PASSWORD || 'Hxl1314521',
        },
        mysql: {
            host: process.env.DB_HOST || '127.0.0.1',
            port: Number(process.env.DB_PORT) || 3306,
            user: process.env.DB_USER || 'root',
            password: process.env.DB_PASSWORD,
            database: process.env.DB_NAME || 'yuheng',
            // ssl:
            //     process.env.NODE_ENV === 'production'
            //         ? {
            //           rejectUnauthorized: false,
            //           servername: '', // 明确置空servername参数
            //         }
            //         : null,
        },
        jwt: {
            secret: process.env.JWT_SECRET || 'Hxl1314521',
            accessExpiresIn: process.env.JWT_ACCESS_EXPIRES || '20m',
            refreshExpiresIn: process.env.JWT_REFRESH_EXPIRES || '14d',
            whitelist: [
                '/user/login',
                '/user/register',
                '/user/refreshToken',
                '/',
                // '/module',
                '/docs*',
                '/docs/json'
            ],
        },
        defaultToken: {
            username: process.env.USERNAME || 'expressgy',
            nickname: process.env.PASSWORD || 'Nie',
            userId: '1'
        }
    },
})

// 首次启动浏览器
async function startBroswer(address: string, url: string) {
    if (!process.env.START) {
        const host = address === '::' ? 'localhost' : address
        const port = url.split(':')[2]
        const rrealUrl = `http://${host}:${port}`;
        console.info(`Listening on ${rrealUrl}`);
        try {
            await open(rrealUrl, {
                app: {
                    name: apps.chrome,
                    arguments: [
                        '--remote-debugging-port=9222',
                        '--auto-open-devtools-for-tabs'
                    ]
                }
            });
        } catch (e) {
            console.error((e as Error).message)
            switch (process.platform) {
                case 'win32':
                    console.log('Windows 系统');
                    exec(`start ${url}`)
                    break;
                case 'darwin':
                    console.log('macOS 系统');
                    exec(`open ${url}`)
                    break;
                case 'linux':
                    console.log('Linux 系统');
                    exec(`xdg-open ${url}`)
                    break;
                default:
                    console.log('其他系统:', process.platform);
            }
        }
        process.env.START = String(true)
    }
}
