import { match } from "ts-pattern";
import { Makeswift as MakeswiftClient } from "../client";
import { createApiHandler } from "../../api-handler";
import * as AppRouter from "./config/app-router";
import * as PagesRouter from "./config/pages-router";
function MakeswiftApiHandler(apiKey, { apiOrigin, runtime, ...userConfig }) {
  const client = new MakeswiftClient(apiKey, { apiOrigin, runtime });
  async function handler(...args) {
    const { req, route, sendResponse, customRoutes, ...handlerConfig } = await match(args).with(AppRouter.argsPattern, ([req2, context]) => AppRouter.config({ req: req2, context, client })).with(PagesRouter.argsPattern, ([req2, res]) => PagesRouter.config({ req: req2, res, client })).exhaustive();
    const apiHandler = createApiHandler(apiKey, {
      ...userConfig,
      ...handlerConfig,
      runtime,
      client
    });
    const customRouteResponse = await customRoutes(route);
    if (customRouteResponse) {
      return customRouteResponse.res;
    }
    return sendResponse(await apiHandler(req, route));
  }
  return handler;
}
export {
  MakeswiftApiHandler
};
//# sourceMappingURL=index.js.map