// electron-vite config. Three targets:
//   - main: the Electron main-process entry (electron/main.ts)
//   - preload: the contextBridge script (electron/preload.ts)
//   - renderer: the existing React app at apps/desktop/ (unchanged root)
//
// Manual chunks + optimizeDeps config is ported from the pre-migration
// vite.config.ts so LiveKit/libsodium/supabase/react vendor bundles keep
// caching independently across app updates.

import react from '@vitejs/plugin-react';
import { defineConfig, externalizeDepsPlugin } from 'electron-vite';
import path from 'node:path';

const rendererAliases = {
  '@': path.resolve(__dirname, './src'),
  '@shared': path.resolve(__dirname, '../../packages/shared/src'),
  '@db-types': path.resolve(__dirname, '../../packages/db-types/src'),
  '@ui-web': path.resolve(__dirname, '../../packages/ui-web/src'),
};

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin()],
    build: {
      outDir: 'out/main',
      rollupOptions: {
        input: path.resolve(__dirname, 'electron/main.ts'),
      },
    },
  },
  preload: {
    plugins: [externalizeDepsPlugin()],
    build: {
      outDir: 'out/preload',
      rollupOptions: {
        input: path.resolve(__dirname, 'electron/preload.ts'),
      },
    },
  },
  renderer: {
    root: '.',
    // file:// loading from inside app.asar can't resolve root-absolute
    // asset URLs (`/assets/...`) — they hit the OS root instead of the
    // bundle. Relative base produces `./assets/...` which works in both
    // dev (served from /) and packaged builds.
    base: './',
    plugins: [react()],
    resolve: {
      alias: rendererAliases,
    },
    optimizeDeps: {
      // libsodium-wrappers-sumo (and the compact variant) ship broken
      // "import" conditions in package exports — the ESM bundle
      // references a sibling ./libsodium.mjs that isn't in the
      // published artefact. Force esbuild to pick the "require"
      // condition so the self-contained CJS build is used.
      include: ['libsodium-wrappers-sumo'],
      esbuildOptions: {
        conditions: ['require', 'node', 'default'],
      },
    },
    build: {
      outDir: 'out/renderer',
      rollupOptions: {
        input: path.resolve(__dirname, 'index.html'),
        output: {
          manualChunks: (id: string): string | undefined => {
            if (id.includes('node_modules/livekit-client')) return 'vendor-livekit';
            if (id.includes('node_modules/libsodium-wrappers-sumo'))
              return 'vendor-sodium';
            if (id.includes('node_modules/@supabase')) return 'vendor-supabase';
            if (
              id.includes('node_modules/react-router') ||
              id.includes('node_modules/react-dom') ||
              id.includes('node_modules/react/')
            ) {
              return 'vendor-react';
            }
            return undefined;
          },
        },
      },
    },
    server: {
      port: 1420,
      strictPort: true,
    },
    clearScreen: false,
  },
});
