import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import { copyFileSync, existsSync, mkdirSync } from 'node:fs'
import { resolve } from 'node:path'

const mpVerifyFiles = [
  'MP_verify_0UoX8yClVgREjgPj.txt',
  'MP_verify_4E1dVbOcZ9KzTzoc.txt',
  'MP_verify_yU2Z8mp831jh6QX7.txt'
]

function copyMpVerifyFiles() {
  return {
    name: 'copy-mp-verify-files',
    closeBundle() {
      const outputDir = resolve(process.cwd(), 'dist/build/h5')

      if (!existsSync(outputDir)) {
        return
      }

      mkdirSync(outputDir, { recursive: true })

      mpVerifyFiles.forEach((file) => {
        const source = resolve(process.cwd(), file)

        if (existsSync(source)) {
          copyFileSync(source, resolve(outputDir, file))
        }
      })
    }
  }
}

export default defineConfig({
  plugins: [uni(), copyMpVerifyFiles()],
  root: process.cwd(),
  css: {
    preprocessorOptions: {
      scss: {
        silenceDeprecations: ['legacy-js-api', 'import']
      }
    }
  }
})
