/**
 * Vite Configuration for TimeSafari Daily Notification Plugin
 * 
 * Integrates with TimeSafari PWA's Vite build system for optimal
 * tree-shaking, SSR safety, and platform-specific builds.
 * 
 * @author Matthew Raymer
 * @version 1.0.0
 */

import { defineConfig } from 'vite';
import { resolve } from 'path';

export default defineConfig({
  // Plugin-specific configuration
  plugins: [],
  
  // Build configuration
  build: {
    // Library mode for plugin distribution
    lib: {
      entry: resolve(__dirname, 'src/index.ts'),
      name: 'TimeSafariDailyNotification',
      fileName: (format) => `timesafari-daily-notification.${format}.js`,
      formats: ['es', 'cjs', 'umd']
    },
    
    // Rollup options for fine-grained control
    rollupOptions: {
      // External dependencies (not bundled)
      external: [
        '@capacitor/core',
        '@capacitor/android',
        '@capacitor/ios',
        '@capacitor/web'
      ],
      
      // Output configuration
      output: {
        // Global variable name for UMD build
        globals: {
          '@capacitor/core': 'CapacitorCore',
          '@capacitor/android': 'CapacitorAndroid',
          '@capacitor/ios': 'CapacitorIOS',
          '@capacitor/web': 'CapacitorWeb'
        },
        
        // Asset file names
        assetFileNames: (assetInfo) => {
          if (assetInfo.name === 'style.css') return 'timesafari-daily-notification.css';
          return assetInfo.name || 'asset';
        }
      }
    },
    
    // Source maps for debugging
    sourcemap: true,
    
    // Minification
    minify: 'terser',
    
    // Target environment
    target: 'es2015'
  },
  
  // Development server configuration
  server: {
    port: 3000,
    host: true,
    cors: true
  },
  
  // Preview server configuration
  preview: {
    port: 3001,
    host: true,
    cors: true
  },
  
  // Define global constants
  define: {
    __PLUGIN_VERSION__: JSON.stringify(process.env.npm_package_version || '1.0.0'),
    __BUILD_TIME__: JSON.stringify(new Date().toISOString())
  },
  
  // Resolve configuration
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
      '@examples': resolve(__dirname, 'examples'),
      '@tests': resolve(__dirname, 'src/__tests__')
    }
  },
  
  // Optimize dependencies
  optimizeDeps: {
    include: [
      '@capacitor/core'
    ],
    exclude: [
      '@capacitor/android',
      '@capacitor/ios'
    ]
  },
  
  // Test configuration
  test: {
    globals: true,
    environment: 'jsdom',
    setupFiles: ['./tests/setup.ts']
  }
});
