// src/server.edge.ts
import { H as H2 } from "@highlight-run/cloudflare";

// src/util/highlight-edge.ts
import {
  H as CloudflareH,
  extractRequestMetadata
} from "@highlight-run/cloudflare";
var globalRequestMetadata = {
  secureSessionId: "",
  requestId: ""
};
var executionContext;
var H = {
  ...CloudflareH,
  init: (_) => {
    throw new Error(
      "H.init is not supported by the Edge runtime. Try H.initEdge instead."
    );
  },
  isInitialized: () => CloudflareH.isInitialized(),
  initEdge: function init(request, env, ctx) {
    polyfillWaitUntil(ctx);
    executionContext = ctx;
    const cloudflareEnv = {
      HIGHLIGHT_PROJECT_ID: env.projectID,
      HIGHLIGHT_OTLP_ENDPOINT: env.otlpEndpoint
    };
    if (env.serviceVersion) {
      console.warn(
        "Highlight does not support serviceVersion on Cloudflare Workers"
      );
    }
    if (env.enableFsInstrumentation) {
      console.warn(
        "Highlight does not support enableFsInstrumentation on Cloudflare Workers"
      );
    }
    globalRequestMetadata = extractRequestMetadata(request);
    return CloudflareH.init(request, cloudflareEnv, ctx, env.serviceName);
  },
  metrics: (metrics, requestMetadata) => {
    const localRequestMetadata = requestMetadata || globalRequestMetadata;
    if (!localRequestMetadata.secureSessionId) {
      return console.warn(
        "H.metrics session could not be inferred the handler context. Consider passing the request metadata explicitly as a second argument to this function."
      );
    }
    for (const m of metrics) {
      CloudflareH.recordMetric({ ...localRequestMetadata, ...m });
    }
  },
  consumeAndFlush: async (error) => {
    CloudflareH.consumeError(error);
    if (executionContext?.waitUntilFinished) {
      await executionContext.waitUntilFinished();
    }
  },
  stop: async () => {
    throw new Error("H.stop is not supported by the Edge runtime.");
  },
  flush: async () => {
    throw new Error(
      "H.flush is not supported by the Edge runtime. try H.consumeAndFlush instead."
    );
  },
  recordMetric: (secureSessionId, name, value, requestId, tags) => {
    return CloudflareH.recordMetric({
      secureSessionId,
      name,
      value,
      requestId,
      tags
    });
  }
};
function polyfillWaitUntil(ctx) {
  if (typeof ctx.waitUntil !== "function") {
    if (!Array.isArray(ctx.__waitUntilPromises)) {
      ctx.__waitUntilPromises = [];
    }
    ctx.waitUntil = function waitUntil(promise) {
      ctx.__waitUntilPromises.push(promise);
      ctx.__waitUntilTimer = setInterval(() => {
        Promise.allSettled(ctx.__waitUntilPromises || []).then(() => {
          if (ctx.__waitUntilTimer) {
            clearInterval(ctx.__waitUntilTimer);
            ctx.__waitUntilTimer = void 0;
          }
        });
      }, 200);
    };
  }
  ctx.waitUntilFinished = async function waitUntilFinished() {
    if (ctx.__waitUntilPromises) {
      await Promise.allSettled(ctx.__waitUntilPromises);
    }
  };
}

// src/util/with-highlight-edge.ts
function Highlight(env) {
  return function withHighlight(handler) {
    return async function(request, event) {
      H.initEdge(request, env, event);
      try {
        const response = await handler(request, event);
        H.sendResponse(response);
        return response;
      } catch (error) {
        if (error instanceof Error) {
          H.consumeError(error);
        }
        if (event.waitUntilFinished) {
          await event.waitUntilFinished();
        }
        throw error;
      }
    };
  };
}

// src/server.edge.ts
function registerHighlight() {
}
function EdgeHighlight(env) {
  if (process.env.NEXT_RUNTIME === "edge") {
    return Highlight(env);
  } else {
    throw new Error(
      `Do not use EdgeHighlight() in the ${process.env.NEXT_RUNTIME} runtime`
    );
  }
}
function PageRouterHighlight(_) {
  throw new Error("Do not use PageRouterHighlight() in the edge runtime.");
}
function AppRouterHighlight(_) {
  throw new Error("Do not use AppRouterHighlight() in the edge runtime.");
}
export {
  AppRouterHighlight,
  EdgeHighlight,
  H2 as H,
  PageRouterHighlight,
  registerHighlight
};
//# sourceMappingURL=server.edge.js.map