// file: kb_demo_app/vite.config.ts

import { defineConfig, normalizePath } from "vite";
import { NodePackageImporter } from "sass-embedded";
import { resolve } from 'path';

const host = process.env.TAURI_DEV_HOST;

// https://vite.dev/config/
export default defineConfig(() => ({

    envPrefix: ['VITE_', 'TAURI_ENV_*'],
    // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
    //
    // 1. prevent Vite from obscuring rust errors
    clearScreen: false,
    root: 'frontend', // Set this to your frontend directory
    publicDir: 'public',
    build: {
        outDir: './dist', // Output directory for the build
        emptyOutDir: true,
        rollupOptions: {
            input: {
                "main": normalizePath(resolve(__dirname, 'frontend/main.html')),
                "splash": normalizePath(resolve(__dirname, 'frontend/splash.html')),
                "demo_ws": normalizePath(resolve(__dirname, 'frontend/demo_ws.html')),
                "demo_http": normalizePath(resolve(__dirname, 'frontend/demo_http.html')),
                "demo_ws_manager": normalizePath(resolve(__dirname, 'frontend/demo_ws_manager.html')),
                "demo3old": normalizePath(resolve(__dirname, 'frontend/demo3old.html')),
                "demo3": normalizePath(resolve(__dirname, 'frontend/demo3.html')),
                "demo_pipeline": normalizePath(resolve(__dirname, 'frontend/demo_pipeline.html')),
                "demo_pipeline2": normalizePath(resolve(__dirname, 'frontend/demo_pipeline2.html'))
            },
            output: {
                entryFileNames: 'js/[name]-[hash].js',
                chunkFileNames: 'js/chunks/[name]-[hash].js',
                assetFileNames: (assetInfo) => {
                    const originalName = assetInfo.name ?? '';
                    const ext = originalName.substring(originalName.lastIndexOf('.') + 1).toLowerCase();
                    if (ext === 'js') {
                        return 'js/[name]-[hash][extname]';
                    }
                    // CSS
                    if (ext === 'css') {
                        return 'css/[name]-[hash][extname]';
                    }
                    if (['eot', 'otf', 'ttf', 'woff', 'woff2'].includes(ext)) {
                        return 'fonts/[name]-[hash][extname]';
                    }
                    if (['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'ico'].includes(ext)) {
                        return 'imgs/[name][extname]';
                    }
                    if (['mp4', 'webm'].includes(ext)) {
                        return 'videos/[name][extname]';
                    }
                    return 'otherassets/[name][extname]';
                },
            },
        },
        minify: true,
        sourcemap: false,
        cssCodeSplit: true
    },
    css: {
        preprocessorOptions: {
            scss: {
                quietDeps: true,
                silenceDeprecations: ["import", "color-functions", "global-builtin",],
                verbose: false,
                api: 'modern',
                importers: [new NodePackageImporter()],
            }
        }
    },
    // 2. tauri expects a fixed port, fail if that port is not available
    server: {
        port: 1420,
        strictPort: false,
        host: host || false,
        hmr: host
            ? {
                protocol: "ws",
                host,
                port: 1421,
            }
            : undefined,
        watch: {
            ignored: ["**/src/**"],
        },
    },
}));
