"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 server_exports = {};
__export(server_exports, {
  _serverHandler: () => _serverHandler,
  serverAdapterFactory: () => serverAdapterFactory
});
module.exports = __toCommonJS(server_exports);
var import_server = require("@whatwg-node/server");
var import_graphql = require("graphql");
var import_graphql_yoga = require("graphql-yoga");
var import_config = require("../lib/config");
var import_match = require("./match");
var import_session = require("./session");
const config_file = (0, import_config.getCurrentConfig)();
const session_keys = (0, import_config.localApiSessionKeys)(config_file);
function _serverHandler({
  schema,
  yoga,
  client,
  production,
  manifest,
  graphqlEndpoint,
  on_render,
  componentCache
}) {
  if (schema && !yoga) {
    yoga = (0, import_graphql_yoga.createYoga)({
      schema,
      landingPage: !production,
      graphqlEndpoint
    });
  }
  client.componentCache = componentCache;
  if (schema) {
    client.registerProxy(graphqlEndpoint, async ({ query, variables, session }) => {
      const parsed = (0, import_graphql.parse)(query);
      return await (0, import_graphql.execute)(schema, parsed, null, session, variables);
    });
  }
  return async (request) => {
    if (!manifest) {
      return new Response(
        "Adapter did not provide the project's manifest. Please open an issue on github.",
        { status: 500 }
      );
    }
    const url = new URL(request.url).pathname;
    if (yoga && url === (0, import_config.localApiEndpoint)(config_file)) {
      return yoga(request);
    }
    const authResponse = await (0, import_session.handle_request)({
      request,
      config: config_file,
      session_keys
    });
    if (authResponse) {
      return authResponse;
    }
    const [match] = (0, import_match.find_match)(config_file, manifest, url);
    const rendered = await on_render({
      url,
      match,
      session: await (0, import_session.get_session)(request.headers, session_keys),
      manifest,
      componentCache
    });
    if (rendered) {
      return rendered;
    }
    return new Response("404", { status: 404 });
  };
}
const serverAdapterFactory = (args) => {
  return (0, import_server.createServerAdapter)(_serverHandler(args));
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  _serverHandler,
  serverAdapterFactory
});
