import type { H3Event } from 'h3'
import { joinURL } from 'ufo'

export default defineEventHandler((event: H3Event) => {
  const { serverApiBaseUrl, clientProxyBasePath } = useRuntimeConfig().public
  const escapedClientProxyBasePath = clientProxyBasePath.replace(/^\//, '\\/')
  // Use the escaped value in the regex
  const path = event.path.replace(new RegExp(`^${escapedClientProxyBasePath}`), '')
  const target = joinURL(serverApiBaseUrl, path)

  console.log('🔀 proxying request to', target)

  return proxyRequest(event, target)
})
