import { existsSync, unlinkSync } from 'fs'
import { parseAll, hasPhpTranslations } from './loader'

export default function i18n(langPath: string = 'lang') {
  let files: { name: string; path: string }[] = []
  let exitHandlersBound: boolean = false

  const clean = () => {
    files.forEach((file) => unlinkSync(file.path))

    files = []
  }

  return {
    name: 'i18n',
    enforce: 'post',
    config(config) {
      if (!hasPhpTranslations(langPath)) {
        return
      }

      files = parseAll(langPath)

      /** @ts-ignore */
      process.env.VITE_LARAVEL_REACT_I18N_HAS_PHP = true

      return {
        define: {
          'process.env.LARAVEL_REACT_I18N_HAS_PHP': true
        }
      }
    },
    buildEnd: clean,
    handleHotUpdate(ctx) {
      if (/lang\/.*\.php$/.test(ctx.file)) {
        files = parseAll(langPath)
      }
    },
    configureServer(server) {
      if (exitHandlersBound) {
        return
      }

      process.on('exit', clean)
      process.on('SIGINT', process.exit)
      process.on('SIGTERM', process.exit)
      process.on('SIGHUP', process.exit)

      exitHandlersBound = true
    }
  }
}
