/**
 * @since 4.0.0
 */
import * as Context from "../../Context.js";
import * as Effect from "../../Effect.js";
import * as Layer from "../../Layer.js";
import * as HttpClient from "../http/HttpClient.js";
import * as HttpRouter from "../http/HttpRouter.js";
import * as HttpServerRequest from "../http/HttpServerRequest.js";
import * as HttpServerResponse from "../http/HttpServerResponse.js";
import * as HttpApiBuilder from "./HttpApiBuilder.js";
import * as HttpApiClient from "./HttpApiClient.js";
/**
 * @since 4.0.0
 * @category Testing
 */
export const groups = /*#__PURE__*/Effect.fnUntraced(function* (api, groupNames, options) {
  let context = yield* Effect.context();
  for (const name in api.groups) {
    const group = api.groups[name];
    if (groupNames.includes(name)) {
      continue;
    }
    const handlers = new Map();
    const routes = [];
    for (const endpointName in group.endpoints) {
      const endpoint = group.endpoints[endpointName];
      const handler = {
        endpoint: endpoint,
        handler: () => Effect.die(new Error(`Unhandled endpoint: ${endpointName}`)),
        isRaw: false,
        uninterruptible: false
      };
      handlers.set(endpointName, handler);
      routes.push(HttpApiBuilder.handlerToRoute(group, handler, context));
    }
    context = Context.add(context, group, {
      handlers,
      routes
    });
  }
  const layer = HttpApiBuilder.layer(api).pipe(Layer.provide(Layer.succeedContext(context)));
  const handler = yield* HttpRouter.toHttpEffect(layer);
  const httpClient = HttpClient.make(Effect.fnUntraced(function* (request) {
    const serverRequest = HttpServerRequest.fromClientRequest(request);
    const response = yield* handler.pipe(Effect.provideService(HttpServerRequest.HttpServerRequest, serverRequest), Effect.orDie);
    return HttpServerResponse.toClientResponse(response);
  }, Effect.scoped));
  return yield* HttpApiClient.makeWith(api, {
    httpClient,
    baseUrl: options?.baseUrl ?? "http://localhost:3000"
  });
});
//# sourceMappingURL=HttpApiTest.js.map