import { defineConfig } from 'vite'

// Custom Riot.js plugin for Vite
function riotPlugin() {
  return {
    name: 'vite-plugin-riot',
    async transform(code, id) {
      if (!id.endsWith('.riot')) return null

      const { compile } = await import('@riotjs/compiler')
      const { code: compiled } = compile(code, { file: id })

      return {
        code: compiled,
        map: null,
      }
    },
  }
}

const apiPort = process.env.VITE_API_PORT || '3001'
const clientPort = parseInt(process.env.VITE_PORT || '3000')

export default defineConfig({
  plugins: [riotPlugin()],
  server: {
    port: clientPort,
    proxy: {
      '/api': {
        target: `http://localhost:${apiPort}`,
        changeOrigin: true,
      },
      '/ws': {
        target: `ws://localhost:${apiPort}`,
        ws: true,
      },
      '/uploads': {
        target: `http://localhost:${apiPort}`,
        changeOrigin: true,
      },
    },
  },
})
