import { mkdir } from 'fs/promises'
import { existsSync } from 'fs'
import { resolve } from 'path'

/**
 * Spark Vite Plugin
 * 
 * This plugin integrates Spark functionality into the Vite build process.
 * Currently provides a minimal implementation that can be extended with:
 * - Spark runtime injection
 * - Configuration validation
 * - Development server enhancements
 */

export default function sparkPlugin() {
  return {
    name: 'spark-vite-plugin',
    
    configResolved(config: any) {
      // TODO: Add Spark-specific configuration handling if needed
      // This hook is called after the Vite config is resolved
    },
    
    transformIndexHtml(html: string) {
      // TODO: Add Spark runtime injection to HTML if needed
      // Currently returns HTML unchanged
      return html
    },

    async buildStart() {
      const tmpDist = '/tmp/dist'
      if (!existsSync(tmpDist)) {
        try {
          await mkdir(tmpDist, { recursive: true })
        } catch (err) {
          console.warn('[spark-vite-plugin] Could not create /tmp/dist:', err)
        }
      }
    },

    closeBundle() {
      // Build completed successfully
      // The Spark runtime may attempt to copy additional files after the build
      // This hook ensures the build process completes gracefully
    }
  }
}
