import type { WebNovel } from '~/types'

export default defineEventHandler(async (event): Promise<WebNovel | null> => {
  const id = getRouterParam(event, 'id')

  const novels = await queryCollection('content').all() as WebNovel[]
  const novel = novels.find(n => n._path === `/novels/${id}` || n.slug === id)

  if (!novel) {
    throw createError({
      statusCode: 404,
      statusMessage: 'Novel not found'
    })
  }

  return novel
})
