




const path = require('path');
const PrerenderSPAPlugin = require('@dreysolano/prerender-spa-plugin');
const Renderer = PrerenderSPAPlugin.PuppeteerRenderer;
const SitemapWebpackPlugin = require('sitemap-webpack-plugin').default;

const getProxyConfig = () => {
    return {
        "/api": {
            target: "https://fapi.binance.com",
            onProxyRes(proxyRes, req, res) {
                const realUrl = new URL(req.url || '', "https://fapi.binance.com")?.href || '';
                proxyRes.headers['x-real-url'] = realUrl;
            },
            pathRewrite: { "^/api": "" },
            changeOrigin: true,
        },
    };
};

const createPrerenderPlugin = () => {
    return new PrerenderSPAPlugin({
        staticDir: path.join(__dirname, 'dist'),
        routes: ['/', '/IndexIntroduce', '/dataDisplay', '/personal', '/Alarm', '/help'],
        timeout: 10000,
        renderer: new Renderer({
            headless: true,
            inject: {
                foo: 'bar',
            },
        }),
        postProcess(context) {
            console.log(`Rendering route: ${context.route}`);
            return context;
        },
    });
};

module.exports = (config) => {
    const devServerProxy = getProxyConfig();

    if (process.env.NODE_ENV === 'production') {
        const prerenderPlugin = createPrerenderPlugin();

        const paths = [
            {
                path: '/',
                lastmod: new Date().toISOString(),
                priority: 1.0,
                changefreq: 'daily'
            },
            {
                path: '/IndexIntroduce',
                lastmod: new Date().toISOString(),
                priority: 0.8,
                changefreq: 'weekly'
            },
            {
                path: '/dataDisplay',
                lastmod: new Date().toISOString(),
                priority: 0.8,
                changefreq: 'weekly'
            },
            {
                path: '/personal',
                lastmod: new Date().toISOString(),
                priority: 0.6,
                changefreq: 'weekly'
            },
            {
                path: '/Alarm',
                lastmod: new Date().toISOString(),
                priority: 0.7,
                changefreq: 'weekly'
            },
            {
                path: '/help',
                lastmod: new Date().toISOString(),
                priority: 0.6,
                changefreq: 'weekly'
            }
        ];

        const sitemapPlugin = new SitemapWebpackPlugin({
            base: 'https://coinbus.cc',
            paths,
            options: {
                filename: 'sitemap.xml',
                lastmod: true,
                skipgzip: true, // 不生成 .gz 文件
                formatter: null, // 使用默认 formatter
                xmlNs: 'http://www.sitemaps.org/schemas/sitemap/0.9', // 添加正确的 XML 命名空间
                headers: {
                    'Content-Type': 'application/xml; charset=utf-8'
                }
            }
        });



        return {
            publicPath: '/',
            devServer: {
                proxy: devServerProxy
            },
            chainWebpack: (config) => {
                if (prerenderPlugin) {
                    config.plugin('prerender').use(prerenderPlugin);
                }
                if (sitemapPlugin) {
                    config.plugin('sitemap').use(sitemapPlugin);
                }
            },
            configureWebpack: {
                plugins: [
                    sitemapPlugin
                ]
            },
            lintOnSave: false
        };
    } else {
        return {
            publicPath: '/',
            devServer: {
                proxy: devServerProxy
            },
            lintOnSave: false
        };
    }
};