import type { PluginOption } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import progress from 'vite-plugin-progress'
import { setupAnalyzer } from './analyzer'
import { setupCompression } from './compression'
import { setupDeploy } from './deploy'
import { setupDevtoolsPlugin } from './devtools'
import { setupHtmlPlugin } from './html'
import { setupImageOptimizer } from './image-optimizer'
import { setupPWA } from './pwa'
import { setupElegantRouter } from './router'
import { setupUnocss } from './unocss'
import { setupUnplugin } from './unplugin'

export function setupVitePlugins(viteEnv: Env.ImportMeta, buildTime: string, isBuild: boolean) {
  const plugins: PluginOption = [
    // 基础插件
    vue(),
    vueJsx(),
    setupDevtoolsPlugin(viteEnv),
    setupElegantRouter(),
    setupUnocss(viteEnv),
    ...setupUnplugin(viteEnv),
    progress(),
    setupHtmlPlugin(buildTime),

    // 优化插件
    setupImageOptimizer(),
    setupCompression(),
    setupPWA(),
    setupDeploy(isBuild),
  ]

  // 构建时添加打包分析插件
  if (isBuild) {
    plugins.push(setupAnalyzer())
  }

  return plugins
}
