import "./chunk-LZOMFHX3.mjs";

// src/components/static-hosting/static-hosting.viewer-request-handler.ts
var isTestEnv = process.env.JEST_WORKER_ID !== void 0;
var spaHandler = async (event) => {
  return runHandler(event, "spa");
};
var staticHandler = async (event) => {
  return runHandler(event, "static");
};
var runHandler = async (event, mode) => {
  if (!isTestEnv) {
    console.log("Request Event: ", JSON.stringify(event, null, 2));
  }
  let request = event.Records[0].cf.request;
  request = addDefaultDocument(request, mode);
  request = addDomainFolder(request);
  if (!isTestEnv) {
    console.log("Resulting Request: ", JSON.stringify(request, null, 2));
  }
  return request;
};
var addDefaultDocument = (request, mode) => {
  if (request.uri === "/" || request.uri === "/index.html") {
    request.uri = "/index.html";
    return request;
  }
  const segments = request.uri.split("/").filter(Boolean);
  const lastSegment = segments[segments.length - 1] ?? "";
  const looksLikeStaticFile = lastSegment.includes(".");
  if (looksLikeStaticFile) {
    return request;
  }
  if (mode === "spa") {
    request.uri = "/index.html";
    return request;
  }
  const normalized = request.uri.endsWith("/") ? request.uri.slice(0, -1) : request.uri;
  request.uri = `${normalized}/index.html`;
  return request;
};
var addDomainFolder = (request) => {
  const hostHeader = request.headers.host[0].value;
  request.uri = `/${hostHeader}${request.uri}`;
  return request;
};
export {
  addDefaultDocument,
  addDomainFolder,
  spaHandler,
  staticHandler
};
//# sourceMappingURL=static-hosting.viewer-request-handler.mjs.map