import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import { wayfinder } from '@laravel/vite-plugin-wayfinder';
import react from '@vitejs/plugin-react';
import tailwindcss from '@tailwindcss/vite';
import path from 'path';

export default defineConfig({
    plugins: [
        laravel({
            input: ['resources/js/app.tsx'],
            refresh: true,
            buildDirectory: 'vite'
        }),
        /*wayfinder({
            command: "docker exec taskco-app php artisan wayfinder:generate"
        }),*/
        react(),
        tailwindcss(),
    ],

    server: {
        host: '127.0.0.1',
        port: 5175,
        strictPort: true,
        https: undefined,
        hmr: {
            host: '127.0.0.1',
        },
        watch: {
            usePolling: process.env.CHOKIDAR_USEPOLLING === 'true',
            interval: 1000,
            ignored: ['**/node_modules/**', '**/vendor/**'],
        },

        proxy: {
            '/api': {
                target: 'http://localhost:8000',
                changeOrigin: true,
                secure: false,
            },
            '/ws': {
                target: 'ws://localhost:8000',
                ws: true,
            },
        },
    },

    build: {
        outDir: 'public/build',
        emptyOutDir: true,
        manifest: true,
        minify: 'esbuild',
        sourcemap: false,
        cssCodeSplit: true,
        chunkSizeWarningLimit: 1500,
        rollupOptions: {
            input: 'resources/js/app.tsx',
            output: {
                entryFileNames: 'assets/[name].[hash].js',
                chunkFileNames: 'assets/[name].[hash].js',
                assetFileNames: 'assets/[name].[hash].[ext]',
            },
        },
    },
    resolve: {
        alias: {
            '@': path.resolve(__dirname, 'resources/js'),
            '@admin': path.resolve(__dirname, 'AdminApp/resources/assets'),
        },
    },

    esbuild: {
        jsx: 'automatic',
        drop: process.env.NODE_ENV === 'production' ? ['console', 'debugger'] : [],
    },

    optimizeDeps: {
        include: [
            'react',
            'react-dom',
            'react/jsx-runtime',
            '@inertiajs/react',
            'react-hook-form',
            'zod',
        ],
        exclude: [
            '@react-types/shared',
            '@radix-ui/react-*',
        ],
    },
});
