import { renderPage } from 'vike/server'
import '../dist/server/entry.mjs'

export default async function handler(req, res) {
  const pageContextInit = {
    urlOriginal: req.url,
    headersOriginal: req.headers,
    method: req.method
  }

  const pageContext = await renderPage(pageContextInit)
  const { httpResponse } = pageContext

  if (!httpResponse) {
    res.statusCode = 404
    res.end('Not Found')
    return
  }

  const { body, statusCode, headers } = httpResponse
  headers.forEach(([name, value]) => res.setHeader(name, value))
  res.statusCode = statusCode
  res.end(body)
}
