import { BridgeSpanExporter } from "./BridgeSpanExporter.js";
import { Resource } from "@opentelemetry/resources";
import { BatchSpanProcessor, SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base";
import { WebTracerProvider } from "@opentelemetry/sdk-trace-web";
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";

//#region src/otel/setupBrowserTracing.ts
let isInitialized = false;
let provider = null;
async function setupBrowserTracing(config) {
	if (isInitialized) return;
	try {
		if (!config.bridge) throw new Error("Bridge is required for browser tracing");
		const exporter = new BridgeSpanExporter(config.bridge, config.otelEndpoint);
		let spanProcessor;
		if (config.useBatching) spanProcessor = new BatchSpanProcessor(exporter, {
			maxQueueSize: 100,
			maxExportBatchSize: 10,
			scheduledDelayMillis: 500
		});
		else spanProcessor = new SimpleSpanProcessor(exporter);
		provider = new WebTracerProvider({
			resource: new Resource({ [ATTR_SERVICE_NAME]: config.serviceName || "telecine-browser" }),
			spanProcessors: [spanProcessor]
		});
		const { ZoneContextManager } = await import("@opentelemetry/context-zone");
		provider.register({ contextManager: new ZoneContextManager() });
		isInitialized = true;
	} catch (error) {
		console.error("Failed to initialize browser tracing:", error);
		throw error;
	}
}

//#endregion
export { setupBrowserTracing };
//# sourceMappingURL=setupBrowserTracing.js.map