import tailwindcss from '@tailwindcss/vite';
import react from '@vitejs/plugin-react';
import laravel from 'laravel-vite-plugin';
import { resolve } from 'node:path';
import { defineConfig } from 'vite';
import { visualizer } from "rollup-plugin-visualizer";

export default defineConfig(({ command, ssrBuild }) => ({
    plugins: [
        laravel({
            input: ['resources/css/app.css', 'resources/js/app.tsx', 'resources/js/pages/home/home.tsx'],
            ssr: 'resources/js/ssr.tsx',
            refresh: true,
        }),
        react(),
        tailwindcss(),
        // visualizer({
        //     filename: 'dist/stats.html',
        //     open: true,           // Automatically open in browser
        //     gzipSize: true,       // Show gzipped sizes
        //     brotliSize: true,     // Show brotli compressed sizes
        //     template: 'treemap',  // Can be 'treemap', 'sunburst', 'network'
        // })
    ],
    esbuild: {
        jsx: 'automatic',
        // Uncomment for production to remove console logs
        // drop: ["console", "debugger"],
    },
    resolve: {
        alias: {
            'ziggy-js': resolve(__dirname, 'vendor/tightenco/ziggy'),
            '@': resolve(__dirname, 'resources/js'),
        },
    },
    optimizeDeps: {
        exclude: ['@ffmpeg/ffmpeg', '@ffmpeg/util']
    },
    build: {
        // Temporarily increase limit while optimizing
        chunkSizeWarningLimit: 1000,

        // Additional optimizations
        minify: 'esbuild',
        target: 'es2020',

        // Enable source maps for better debugging (optional)
        sourcemap: false,

        // Commented out manual chunks to fix SSR build
        // rollupOptions: {
        //     output: {
        //         manualChunks: {
        //             // React MUST be first and separate to avoid dependency issues
        //             'react-vendor': ['react', 'react-dom'],
        //             // ... other chunks
        //         }
        //     }
        // }
    },
    server: {
        headers: {
            'Cross-Origin-Opener-Policy': 'same-origin',
            'Cross-Origin-Embedder-Policy': 'require-corp'
        }
    }
}));
