import compression from 'vite-plugin-compression'
import type { PluginOption } from 'vite'

// 定义环境变量类型（可根据实际需要扩展）
interface Env {
  VITE_BUILD_COMPRESS?: string
}

export default function createCompression(env: Env): PluginOption[] {
  const { VITE_BUILD_COMPRESS } = env
  const plugin: PluginOption[] = []

  if (VITE_BUILD_COMPRESS) {
    const compressList = VITE_BUILD_COMPRESS.split(',')

    if (compressList.includes('gzip')) {
      plugin.push(
        compression({
          ext: '.gz',
          deleteOriginFile: false
        })
      )
    }

    if (compressList.includes('brotli')) {
      plugin.push(
        compression({
          ext: '.br',
          algorithm: 'brotliCompress',
          deleteOriginFile: false
        })
      )
    }
  }

  return plugin
}
