import { NextRequest } from "next/server";

export function createRequest(
  url: string,
  options: {
    method?: string;
    body?: unknown;
    headers?: Record<string, string>;
  } = {},
): NextRequest {
  const { method = "GET", body, headers = {} } = options;
  type NextRequestInit = ConstructorParameters<typeof NextRequest>[1];
  const requestHeaders: Record<string, string> = { ...headers };
  const init: NextRequestInit = { method, headers: requestHeaders };
  if (body !== undefined) {
    init.body = JSON.stringify(body);
    requestHeaders["content-type"] = "application/json";
  }
  return new NextRequest(new URL(url, "http://localhost:3721"), init);
}

export function createRouteContext(
  params: Record<string, string>,
): { params: Promise<Record<string, string>> } {
  return { params: Promise.resolve(params) };
}

export async function parseJsonResponse(response: Response) {
  const data = await response.json();
  return { status: response.status, data };
}
