"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.viewer-request-handler.ts
var static_hosting_viewer_request_handler_exports = {};
__export(static_hosting_viewer_request_handler_exports, {
  addDefaultDocument: () => addDefaultDocument,
  addDomainFolder: () => addDomainFolder,
  spaHandler: () => spaHandler,
  staticHandler: () => staticHandler
});
module.exports = __toCommonJS(static_hosting_viewer_request_handler_exports);
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;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  addDefaultDocument,
  addDomainFolder,
  spaHandler,
  staticHandler
});
//# sourceMappingURL=static-hosting.viewer-request-handler.js.map