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

// Baked into the chat bundle; compared against /__bloby/version on reconnect so a chat that
// survived a self-update inside the immortal shell knows to trigger a one-time full reload.
const pkgVersion = JSON.parse(readFileSync(path.resolve(__dirname, 'package.json'), 'utf-8')).version;

export default defineConfig({
  root: 'supervisor/chat',
  base: '/bloby/',
  define: {
    __BLOBY_VERSION__: JSON.stringify(pkgVersion),
  },
  resolve: {
    alias: { '@': path.resolve(__dirname, 'supervisor/chat/src') },
  },
  build: {
    outDir: '../../dist-bloby',
    emptyOutDir: true,
    rolldownOptions: {
      input: {
        bloby: path.resolve(__dirname, 'supervisor/chat/bloby.html'),
        onboard: path.resolve(__dirname, 'supervisor/chat/onboard.html'),
      },
    },
  },
  server: {
    warmup: {
      clientFiles: ['./bloby-main.tsx', './onboard-main.tsx'],
    },
  },
  optimizeDeps: {
    include: [
      'react',
      'react-dom/client',
      'react/jsx-runtime',
      'lucide-react',
      'framer-motion',
      'streamdown',
      '@streamdown/code',
      'use-sync-external-store',
      'use-sync-external-store/shim',
    ],
  },
  plugins: [react(), tailwindcss()],
});
