"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var test_utils_exports = {};
__export(test_utils_exports, {
  apiRequestFixtures: () => apiRequestFixtures,
  appRouterApiRequestFixture: () => appRouterApiRequestFixture,
  hostUrl: () => hostUrl,
  pagesRouterApiRequestFixture: () => pagesRouterApiRequestFixture
});
module.exports = __toCommonJS(test_utils_exports);
var import_server = require("next/server");
var import_node_mocks_http = require("node-mocks-http");
var import_api_handler = require("../api-handler");
var import_react = require("../../react");
var import_fixtures = require("../../testing/fixtures");
var import_preview = require("../api-handler/preview");
function createHandler(apiKey, args = {}) {
  const runtime = new import_react.ReactRuntime();
  return (0, import_api_handler.MakeswiftApiHandler)(apiKey, {
    ...args,
    runtime,
    apiOrigin: import_fixtures.TestOrigins.apiOrigin,
    appOrigin: import_fixtures.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 (0, import_node_mocks_http.createRequest)({
    method,
    url: url.toString(),
    query: {
      ...routeParams(hostUrl(path)),
      ...Object.fromEntries(url.searchParams.entries()),
      ...originalPath ? { [import_preview.RewriteRuleMatches.OriginalPath]: originalPath } : {}
    },
    body,
    headers
  });
}
function createNextRequestWithContext({
  method,
  path,
  originalPath,
  body,
  headers
}) {
  const request = new import_server.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 ?? (0, import_node_mocks_http.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" }
];
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  apiRequestFixtures,
  appRouterApiRequestFixture,
  hostUrl,
  pagesRouterApiRequestFixture
});
//# sourceMappingURL=test-utils.js.map