// src/util/with-highlight-nodejs-page-router.ts
import { HIGHLIGHT_REQUEST_HEADER } from "@highlight-run/node";

// src/util/highlight-node.ts
import { H as NodeH } from "@highlight-run/node";
var H = {
  ...NodeH,
  init: (options) => {
    if (process.env.NEXT_RUNTIME === "nodejs") {
      if (!NodeH.isInitialized()) {
        NodeH.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: () => NodeH.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) {
      NodeH.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[HIGHLIGHT_REQUEST_HEADER]) {
        const [secureSessionId2, requestId2] = `${req.headers[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
import { HIGHLIGHT_REQUEST_HEADER as HIGHLIGHT_REQUEST_HEADER2, H as H2 } from "@highlight-run/node";
function Highlight2(options) {
  return (originalHandler) => async (request, context) => {
    try {
      H2.init(options);
      return await originalHandler(request, context);
    } catch (error) {
      const { secureSessionId, requestId } = processHighlightHeaders(request);
      if (error instanceof Error) {
        await H2.consumeAndFlush(error, secureSessionId, requestId);
      }
      await H2.stop();
      throw error;
    }
  };
}
function processHighlightHeaders(request) {
  const header = request.headers.get(HIGHLIGHT_REQUEST_HEADER2);
  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
import { H as H3 } from "@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}`);
}
export {
  AppRouterHighlight,
  EdgeHighlight,
  H3 as H,
  Highlight3 as Highlight,
  PageRouterHighlight,
  registerHighlight
};
//# sourceMappingURL=server.js.map