"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/server.edge.ts
var server_edge_exports = {};
__export(server_edge_exports, {
  AppRouterHighlight: () => AppRouterHighlight,
  EdgeHighlight: () => EdgeHighlight,
  H: () => import_cloudflare2.H,
  PageRouterHighlight: () => PageRouterHighlight,
  registerHighlight: () => registerHighlight
});
module.exports = __toCommonJS(server_edge_exports);
var import_cloudflare2 = require("@highlight-run/cloudflare");

// src/util/highlight-edge.ts
var import_cloudflare = require("@highlight-run/cloudflare");
var globalRequestMetadata = {
  secureSessionId: "",
  requestId: ""
};
var executionContext;
var H = {
  ...import_cloudflare.H,
  init: (_) => {
    throw new Error(
      "H.init is not supported by the Edge runtime. Try H.initEdge instead."
    );
  },
  isInitialized: () => import_cloudflare.H.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 = (0, import_cloudflare.extractRequestMetadata)(request);
    return import_cloudflare.H.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) {
      import_cloudflare.H.recordMetric({ ...localRequestMetadata, ...m });
    }
  },
  consumeAndFlush: async (error) => {
    import_cloudflare.H.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 import_cloudflare.H.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.");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  AppRouterHighlight,
  EdgeHighlight,
  H,
  PageRouterHighlight,
  registerHighlight
});
//# sourceMappingURL=server.edge.cjs.map