"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/server.ts
var server_exports = {};
__export(server_exports, {
  AppRouterHighlight: () => AppRouterHighlight,
  EdgeHighlight: () => EdgeHighlight,
  H: () => import_node4.H,
  Highlight: () => Highlight3,
  PageRouterHighlight: () => PageRouterHighlight,
  registerHighlight: () => registerHighlight
});
module.exports = __toCommonJS(server_exports);

// src/util/with-highlight-nodejs-page-router.ts
var import_node2 = require("@highlight-run/node");

// src/util/highlight-node.ts
var import_node = require("@highlight-run/node");
var H = {
  ...import_node.H,
  init: (options) => {
    if (process.env.NEXT_RUNTIME === "nodejs") {
      if (!import_node.H.isInitialized()) {
        import_node.H.init(options);
      }
    } else {
      throw new Error(
        `Highlight not registered due to unexpected runtime: NEXT_RUNTIME=${process.env.NEXT_RUNTIME}`
      );
    }
  },
  initEdge: () => {
    throw new Error(
      "H.initEdge is not supported by the Node runtime. Try H.init instead."
    );
  },
  isInitialized: () => import_node.H.isInitialized(),
  metrics: (metrics, opts) => {
    const h = global.__HIGHLIGHT__;
    if (h && !opts) {
      opts = h;
    }
    if (!opts?.secureSessionId) {
      return console.warn(
        "H.metrics session could not be inferred the handler context."
      );
    }
    for (const m of metrics) {
      import_node.H.recordMetric(
        opts.secureSessionId,
        m.name,
        m.value,
        opts.requestId,
        m.tags
      );
    }
  },
  sendResponse: (_) => {
    throw new Error(
      "H.sendResponse is not implemented for the Node runtime."
    );
  }
};

// src/util/with-highlight-nodejs-page-router.ts
var Highlight = (options) => (origHandler) => {
  return async (req, res) => {
    if (!H.isInitialized()) {
      H.init(options);
    }
    const processHighlightHeaders2 = () => {
      if (req.headers && req.headers[import_node2.HIGHLIGHT_REQUEST_HEADER]) {
        const [secureSessionId2, requestId2] = `${req.headers[import_node2.HIGHLIGHT_REQUEST_HEADER]}`.split("/");
        if (secureSessionId2 && requestId2) {
          ;
          global.__HIGHLIGHT__ = { secureSessionId: secureSessionId2, requestId: requestId2 };
          return { secureSessionId: secureSessionId2, requestId: requestId2 };
        }
      }
      return { secureSessionId: void 0, requestId: void 0 };
    };
    const { secureSessionId, requestId } = processHighlightHeaders2();
    const start = /* @__PURE__ */ new Date();
    try {
      return await origHandler(req, res);
    } catch (e) {
      if (e instanceof Error) {
        await H.consumeAndFlush(e, secureSessionId, requestId);
      }
      res.statusCode = 500;
      res.statusMessage = "Internal Server Error";
      await H.stop();
      throw e;
    } finally {
      const delta = ((/* @__PURE__ */ new Date()).getTime() - start.getTime()) * 1e6;
      const { secureSessionId: secureSessionId2, requestId: requestId2 } = processHighlightHeaders2();
      if (secureSessionId2) {
        H.recordMetric(secureSessionId2, "latency", delta, requestId2);
        await H.flush();
      }
    }
  };
};

// src/util/with-highlight-nodejs-app-router.ts
var import_node3 = require("@highlight-run/node");
function Highlight2(options) {
  return (originalHandler) => async (request, context) => {
    try {
      import_node3.H.init(options);
      return await originalHandler(request, context);
    } catch (error) {
      const { secureSessionId, requestId } = processHighlightHeaders(request);
      if (error instanceof Error) {
        await import_node3.H.consumeAndFlush(error, secureSessionId, requestId);
      }
      await import_node3.H.stop();
      throw error;
    }
  };
}
function processHighlightHeaders(request) {
  const header = request.headers.get(import_node3.HIGHLIGHT_REQUEST_HEADER);
  if (header) {
    const [secureSessionId, requestId] = header.split("/");
    if (secureSessionId && requestId) {
      ;
      global.__HIGHLIGHT__ = {
        secureSessionId,
        requestId
      };
      return { secureSessionId, requestId };
    }
  }
  return { secureSessionId: void 0, requestId: void 0 };
}

// src/util/registerHighlight.ts
async function registerHighlight(nodeOptions) {
  if (process.env.NEXT_RUNTIME === "nodejs") {
    const { H: H4 } = await import("@highlight-run/node");
    H4.init(nodeOptions);
  } else {
    console.info(
      `Highlight not registered: NEXT_RUNTIME=${process.env.NEXT_RUNTIME}`
    );
  }
}

// src/server.ts
var import_node4 = require("@highlight-run/node");
var Highlight3 = PageRouterHighlight;
function PageRouterHighlight(env) {
  if (process.env.NEXT_RUNTIME === "nodejs") {
    return Highlight(env);
  } else {
    throw new Error(
      `unidentified NEXT_RUNTIME: ${process.env.NEXT_RUNTIME}`
    );
  }
}
function AppRouterHighlight(env) {
  if (process.env.NEXT_RUNTIME === "nodejs") {
    return Highlight2(env);
  } else {
    throw new Error(
      `unidentified NEXT_RUNTIME: ${process.env.NEXT_RUNTIME}`
    );
  }
}
function EdgeHighlight(_) {
  throw new Error(`unsupported NEXT_RUNTIME: ${process.env.NEXT_RUNTIME}`);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  AppRouterHighlight,
  EdgeHighlight,
  H,
  Highlight,
  PageRouterHighlight,
  registerHighlight
});
//# sourceMappingURL=server.cjs.map