import { getBindings } from "@/lib/server/env";
import { BookRepository } from "@/lib/server/repositories/books";
import { createBookSourceApi } from "@/lib/server/source-api";

export async function GET(
  request: Request,
  context: { params: Promise<{ bookId: string }> },
) {
  const { bookId } = await context.params;
  const bindings = getBindings();
  const repository = new BookRepository(bindings.DB);
  const api = createBookSourceApi({
    findSource: (id) => repository.source(id),
    getObject: async (key, range) =>
      bindings.STORAGE.get(key, range ? { range } : undefined),
  });
  return api.get(request, bookId);
}
