import { NextRequest } from "next/server";
import {
  createRequest,
  createResponse
} from "node-mocks-http";
import { MakeswiftApiHandler } from "../api-handler";
import { ReactRuntime } from "../../react";
import { TestOrigins } from "../../testing/fixtures";
import { RewriteRuleMatches } from "../api-handler/preview";
function createHandler(apiKey, args = {}) {
  const runtime = new ReactRuntime();
  return MakeswiftApiHandler(apiKey, {
    ...args,
    runtime,
    apiOrigin: TestOrigins.apiOrigin,
    appOrigin: TestOrigins.appOrigin
  });
}
const hostUrl = (path) => new URL(path, "https://example.com");
function routeParams(url) {
  const segments = url.pathname.split("/").filter(Boolean).slice(1);
  return { [segments[0]]: segments.slice(1) };
}
function createNextApiRequest({
  method,
  path,
  originalPath,
  body,
  headers
}) {
  const url = hostUrl(originalPath ?? path);
  return createRequest({
    method,
    url: url.toString(),
    query: {
      ...routeParams(hostUrl(path)),
      ...Object.fromEntries(url.searchParams.entries()),
      ...originalPath ? { [RewriteRuleMatches.OriginalPath]: originalPath } : {}
    },
    body,
    headers
  });
}
function createNextRequestWithContext({
  method,
  path,
  originalPath,
  body,
  headers
}) {
  const request = new NextRequest(hostUrl(originalPath ?? path), {
    method,
    body: JSON.stringify(body),
    headers
  });
  return [request, { params: Promise.resolve(routeParams(hostUrl(path))) }];
}
function pagesRouterApiRequestFixture(args = {}, responseMock) {
  const apiKey = "pages-router-api-key";
  const handler = createHandler(apiKey, args);
  const response = responseMock ?? createResponse();
  const testApiRequest = async (reqParams) => {
    await handler(createNextApiRequest(reqParams), response);
    return {
      statusCode: response.statusCode,
      get jsonBody() {
        return response._getJSONData();
      },
      get textBody() {
        return response._getData();
      },
      headers: {
        getSetCookie() {
          return response._getHeaders()["set-cookie"];
        },
        get(key) {
          return key.toLowerCase() === "location" ? response._getRedirectUrl() || response._getHeaders()["location"] : response._getHeaders()[key];
        }
      }
    };
  };
  return { testApiRequest, apiKey };
}
function appRouterApiRequestFixture(args = {}) {
  const apiKey = "app-router-api-key";
  const handler = createHandler(apiKey, args);
  const testApiRequest = async (reqParams) => {
    const response = await handler(...createNextRequestWithContext(reqParams));
    return {
      statusCode: response?.status,
      get jsonBody() {
        return response?.json();
      },
      get textBody() {
        return response?.text();
      },
      headers: response?.headers ?? new Headers()
    };
  };
  return { testApiRequest, apiKey };
}
const apiRequestFixtures = [
  { fixture: pagesRouterApiRequestFixture, router: "pages" },
  { fixture: appRouterApiRequestFixture, router: "app" }
];
export {
  apiRequestFixtures,
  appRouterApiRequestFixture,
  hostUrl,
  pagesRouterApiRequestFixture
};
//# sourceMappingURL=test-utils.js.map