import { createParamDecorator, ExecutionContext } from '@nestjs/common'

import { FastifyRequestWithViewer } from './auth.guard'

export const ViewerParam = createParamDecorator((_data: unknown, ctx: ExecutionContext): any => {
  const request = ctx.switchToHttp().getRequest<FastifyRequestWithViewer>()
  const viewer = request.viewer

  if (!viewer) {
    throw new Error('Please use the AuthGuard to access the viewer!')
  }
  return viewer
})
