"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);

// src/components/static-hosting/static-hosting.origin-request-handler.ts
var static_hosting_origin_request_handler_exports = {};
__export(static_hosting_origin_request_handler_exports, {
  API_HOST_PREFIX: () => API_HOST_PREFIX,
  VIEWER_HOST_HEADER: () => VIEWER_HOST_HEADER,
  VIEWER_HOST_PREFIX: () => VIEWER_HOST_PREFIX,
  applyOriginOverride: () => applyOriginOverride,
  computeApiHost: () => computeApiHost,
  originRequestHandler: () => originRequestHandler,
  readViewerHost: () => readViewerHost
});
module.exports = __toCommonJS(static_hosting_origin_request_handler_exports);
var isTestEnv = process.env.JEST_WORKER_ID !== void 0;
var VIEWER_HOST_HEADER = "x-viewer-host";
var VIEWER_HOST_PREFIX = process.env.VIEWER_HOST_PREFIX ?? "admin";
var API_HOST_PREFIX = process.env.API_HOST_PREFIX ?? "api";
var originRequestHandler = async (event) => {
  if (!isTestEnv) {
    console.log("Origin Request Event: ", JSON.stringify(event, null, 2));
  }
  const request = event.Records[0].cf.request;
  const viewerHost = readViewerHost(request);
  if (viewerHost === null) {
    if (!isTestEnv) {
      console.log("Missing x-viewer-host header; passing through.");
    }
    return request;
  }
  const apiHost = computeApiHost(viewerHost);
  if (apiHost === null) {
    if (!isTestEnv) {
      console.log(
        `x-viewer-host "${viewerHost}" does not start with "${VIEWER_HOST_PREFIX}"; passing through.`
      );
    }
    return request;
  }
  return applyOriginOverride(request, apiHost);
};
var readViewerHost = (request) => {
  const header = request.headers[VIEWER_HOST_HEADER];
  if (!header || header.length === 0) {
    return null;
  }
  const value = header[0]?.value;
  return value && value.length > 0 ? value : null;
};
var computeApiHost = (viewerHost, viewerPrefix = VIEWER_HOST_PREFIX, apiPrefix = API_HOST_PREFIX) => {
  if (!viewerHost.startsWith(viewerPrefix)) {
    return null;
  }
  return apiPrefix + viewerHost.slice(viewerPrefix.length);
};
var applyOriginOverride = (request, apiHost) => {
  if (request.origin?.custom) {
    request.origin.custom.domainName = apiHost;
  }
  request.headers.host = [{ key: "Host", value: apiHost }];
  return request;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  API_HOST_PREFIX,
  VIEWER_HOST_HEADER,
  VIEWER_HOST_PREFIX,
  applyOriginOverride,
  computeApiHost,
  originRequestHandler,
  readViewerHost
});
//# sourceMappingURL=static-hosting.origin-request-handler.js.map