import { NextResponse } from 'next/server';

import { type RouteHandler } from '@/lib/api/helpers';
import { errorResponse } from '@/lib/errors';
import { autocomplete } from '@/lib/services/yachts.service';

export const autocompleteHandler: RouteHandler = async (req, ctx) => {
  try {
    const q = req.nextUrl.searchParams.get('q') ?? '';
    const yachts = await autocomplete(ctx.portId, q);
    return NextResponse.json({ data: yachts });
  } catch (error) {
    return errorResponse(error);
  }
};
