import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { resolve } from 'path'

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  if (mode === 'lib') {
    // Library build configuration for npm publishing
    return {
      plugins: [react()],
      build: {
        lib: {
          // Entry point for your library
          entry: resolve(__dirname, 'src/index.ts'),
          name: 'GoGo',
          // File name for output files
          fileName: (format) => `index.${format === 'es' ? 'js' : 'cjs'}`,
          formats: ['es', 'cjs'] // Dual output: ESM + CommonJS
        },
        rollupOptions: {
          // Externalize React - don't bundle it with your library
          external: ['react', 'react-dom'],
          output: {
            globals: {
              react: 'React',
              'react-dom': 'ReactDOM'
            }
          }
        }
      }
    }
  }
  
  // Default development configuration
  return {
    plugins: [react()],
  }
})
