import {
  BatchError,
  executeBatch
} from "./chunk-ZCS6ATFM.js";
import {
  jsonOk,
  routeWrap
} from "./chunk-KKJ5D3II.js";
import {
  badRequest
} from "./chunk-EZYKRG4W.js";

// src/handlers/batch.ts
import { z } from "zod";
var OperationSchema = z.object({
  method: z.string().min(1),
  path: z.string().min(1),
  body: z.record(z.unknown()).optional()
});
var BatchSchema = z.object({
  operations: z.array(OperationSchema).min(1).max(50)
});
function handleBatch(request) {
  return routeWrap(request, async () => {
    const body = await request.json();
    const parsed = BatchSchema.safeParse(body);
    if (!parsed.success) throw badRequest(parsed.error.message);
    try {
      const results = executeBatch(parsed.data.operations);
      return jsonOk({ results });
    } catch (err) {
      if (err instanceof BatchError) {
        return jsonOk(
          {
            error: {
              type: "batch_error",
              message: err.message,
              failed_operation_index: err.failedIndex
            }
          },
          400
        );
      }
      throw err;
    }
  });
}

export {
  handleBatch
};
