import path from 'node:path'
import { defineConfig, searchForWorkspaceRoot } from 'vite'
import react from '@vitejs/plugin-react'

const workspaceRoot = path.resolve(__dirname, '../..')
const webSrc = path.resolve(__dirname, 'src')
const crossOriginIsolationHeaders = {
  'Cross-Origin-Opener-Policy': 'same-origin',
  'Cross-Origin-Embedder-Policy': 'require-corp',
  'Cross-Origin-Resource-Policy': 'same-origin'
}

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      '@': webSrc,
      'next/navigation': path.resolve(__dirname, 'src/shims/next-navigation.ts')
    }
  },
  server: {
    headers: crossOriginIsolationHeaders,
    fs: {
      allow: [searchForWorkspaceRoot(process.cwd()), workspaceRoot]
    },
    proxy: {
      '/api': {
        target: 'http://127.0.0.1:4000',
        changeOrigin: true
      }
    }
  },
  preview: {
    headers: crossOriginIsolationHeaders
  }
})
