import type { Chapter } from '~/types'

export default defineEventHandler(async (event): Promise<Chapter | null> => {
  const novelId = getRouterParam(event, 'novelId')
  const chapterId = getRouterParam(event, 'chapterId')

  const chapters = await queryCollection('content').all() as Chapter[]
  const chapter = chapters.find(c => c._path === `/novels/${novelId}/${chapterId}`)

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

  return chapter
})
