import { H as t, J as e, N as s, V as n, Y as a, a as i, at as r, b as o, ct as l, dt as c, ft as u, it as d, j as p, lt as f, nt as v, pt as m, rt as b, tt as g } from "./ingest.worker-B1ibo6Wb.js";
const k = /\/(?![vV]\d{1,2}\/)([^/\d?]*\d+[^/?]*)/g;
const h = (t, e) => t || function(t) {
	return t ? t.replace(k, "/?") : "/";
}(e);
function w(t, e, s) {
	var n, a;
	const { ids: i, names: r } = function(t) {
		const e = {
			ids: [],
			names: []
		};
		for (const s of t) e.ids.push(s.viewId), s.viewName && e.names.push(s.viewName);
		return e.names = Array.from(new Set(e.names)), e;
	}(t.views), o = t.longTasks.map((t) => t.id).filter((t) => void 0 !== t), l = null !== (a = null === (n = t.actions) || void 0 === n ? void 0 : n.map((t) => t.id).filter((t) => void 0 !== t)) && void 0 !== a ? a : [], { ids: c, labels: u } = function(t) {
		const e = {
			ids: [],
			labels: []
		};
		if (!t) return e;
		for (const s of t) e.ids.push(s.id), e.labels.push(s.label);
		return e.labels = Array.from(new Set(e.labels)), e;
	}(t.vitals), d = { application: { id: e } };
	return s && (d.session = { id: s }), i.length && (d.view = {
		id: i,
		name: r
	}), o.length && (d.long_task = { id: o }), l.length && (d.action = {
		id: l,
		label: []
	}), c.length && (d.vital = {
		id: c,
		label: u
	}), d;
}
function y(t, e, s) {
	const a = n(e), i = w(t, e.applicationId, s), r = function(t) {
		return t.concat([
			"language:javascript",
			"runtime:chrome",
			"family:chrome",
			"host:browser"
		]);
	}(a);
	return {
		...i,
		attachments: ["wall-time.json"],
		start: new Date(t.startClocks.timeStamp).toISOString(),
		end: new Date(t.endClocks.timeStamp).toISOString(),
		family: "chrome",
		runtime: "chrome",
		format: "json",
		version: 4,
		tags_profiler: r.join(","),
		_dd: { clock_drift: d() }
	};
}
const C = a;
function I(t) {
	const e = s({ expireDelay: C });
	return t.subscribe(12, ({ rawRumEvent: t, startClocks: s, duration: n }) => {
		"long_task" === t.type && e.add({
			id: t.long_task.id,
			startClocks: s,
			duration: n,
			entryType: t.long_task.entry_type === p.LONG_TASK ? o.LONG_TASK : o.LONG_ANIMATION_FRAME
		}, s.relative).close(g(s.relative, n));
	}), e;
}
const _ = a;
function S(t) {
	const e = s({ expireDelay: _ });
	return t.subscribe(15, (t) => {
		e.add({
			id: t.id,
			label: "",
			startClocks: t.startClocks,
			duration: void 0
		}, t.startClocks.relative);
	}), t.subscribe(12, ({ rawRumEvent: t, startClocks: s, duration: n }) => {
		if ("action" === t.type) {
			const a = e.getEntries(s.relative).find((e) => e.value.id === t.action.id), i = null != n ? n : 0;
			a ? (a.value.duration = i, a.close(g(s.relative, i))) : e.add({
				id: t.action.id,
				label: "",
				startClocks: s,
				duration: n
			}, s.relative).close(g(s.relative, i));
		}
	}), e;
}
const T = a;
function M(t) {
	const e = s({ expireDelay: T });
	return t.subscribe(16, (t) => {
		e.add({
			id: t.id,
			startClocks: t.startClocks,
			duration: void 0,
			label: t.name
		}, t.startClocks.relative);
	}), t.subscribe(12, ({ rawRumEvent: t, startClocks: s, duration: n }) => {
		if ("vital" === t.type) {
			const a = e.getEntries(s.relative).find((e) => e.value.id === t.vital.id);
			a ? (a.value.duration = n, a.close(g(s.relative, n))) : e.add({
				id: t.vital.id,
				startClocks: s,
				duration: n,
				label: t.vital.name
			}, s.relative).close(g(s.relative, n));
		}
	}), e;
}
const E = {
	sampleIntervalMs: 10,
	collectIntervalMs: 6e4,
	minProfileDurationMs: 5e3,
	minNumberOfSamples: 50
};
function N(s, n, a, o, d, p, g = E) {
	const k = i(s, n, d, 6);
	let w;
	const C = [], _ = e(I)(n), T = e(S)(n), N = e(M)(n);
	let D = {
		state: "stopped",
		stateReason: "initializing"
	};
	function R() {
		if ("running" === D.state) return;
		const e = p.findView();
		w = e ? {
			startClocks: e.startClocks,
			viewId: e.id,
			viewName: h(e.name, document.location.pathname)
		} : void 0, C.push(t(s, window, "visibilitychange", L).stop, t(s, window, "beforeunload", G).stop), O();
	}
	function A(t) {
		(function(t) {
			if ("running" !== D.state) return void (("paused" === D.state || "stopped" === D.state && "stopped-by-user" === t) && (D = {
				state: "stopped",
				stateReason: t
			}));
			const e = D;
			D = {
				state: "stopped",
				stateReason: t
			}, e.cleanupTasks.forEach((t) => t()), j(e);
		})(t), C.forEach((t) => t()), C.length = 0, o.set({
			status: "stopped",
			error_reason: void 0
		});
	}
	function O() {
		const t = u().Profiler;
		if (!t) throw o.set({
			status: "error",
			error_reason: "not-supported-by-browser"
		}), /* @__PURE__ */ new Error("RUM Profiler is not supported in this browser.");
		"running" === D.state && j(D);
		const { cleanupTasks: e } = function(t) {
			if ("running" === t.state) return { cleanupTasks: t.cleanupTasks };
			const e = [], s = n.subscribe(2, (t) => {
				const e = {
					viewId: t.id,
					viewName: h(t.name, document.location.pathname),
					startClocks: t.startClocks
				};
				x(e), w = e;
			});
			return e.push(s.unsubscribe), { cleanupTasks: e };
		}(D);
		let s;
		try {
			s = new t({
				sampleInterval: g.sampleIntervalMs,
				maxBufferSize: Math.round(1.5 * g.collectIntervalMs / g.sampleIntervalMs)
			});
		} catch (a) {
			a instanceof Error && a.message.includes("disabled by Document Policy") ? (m.warn("[DD_RUM] Profiler startup failed. Ensure your server includes the `Document-Policy: js-profiling` response header when serving HTML pages.", a), o.set({
				status: "error",
				error_reason: "missing-document-policy-header"
			})) : o.set({
				status: "error",
				error_reason: "unexpected-exception"
			});
			return;
		}
		o.set({
			status: "running",
			error_reason: void 0
		}), D = {
			state: "running",
			startClocks: v(),
			profiler: s,
			timeoutId: f(O, g.collectIntervalMs),
			views: [],
			cleanupTasks: e,
			longTasks: []
		}, x(w), s.addEventListener("samplebufferfull", P);
	}
	function j(t) {
		l(t.timeoutId), t.profiler.removeEventListener("samplebufferfull", P);
		const { startClocks: e, views: n } = t;
		t.profiler.stop().then((t) => {
			const i = v(), o = r(e.relative, i.relative), l = _.findAll(e.relative, o), c = T.findAll(e.relative, o), u = N.findAll(e.relative, o), d = o < g.minProfileDurationMs, p = function(t) {
				let e = 0;
				for (const s of t) void 0 !== s.stackId && e++;
				return e;
			}(t.samples) < g.minNumberOfSamples;
			0 === l.length && (d || p) || function(t) {
				var e;
				const n = function(t, e, s) {
					return {
						event: y(t, e, s),
						"wall-time.json": t
					};
				}(t, s, null === (e = a.findTrackedSession()) || void 0 === e ? void 0 : e.id);
				k.send(n);
			}(Object.assign(t, {
				startClocks: e,
				endClocks: i,
				clocksOrigin: b(),
				longTasks: l,
				actions: c,
				vitals: u,
				views: n,
				sampleInterval: g.sampleIntervalMs
			}));
		}).catch(c);
	}
	function x(t) {
		"running" === D.state && t && D.views.push(t);
	}
	function P() {
		O();
	}
	function L() {
		"hidden" === document.visibilityState && "running" === D.state ? function() {
			if ("running" !== D.state) return;
			const t = D;
			D = { state: "paused" }, t.cleanupTasks.forEach((t) => t()), j(t);
		}() : "visible" === document.visibilityState && "paused" === D.state && O();
	}
	function G() {
		O();
	}
	return n.subscribe(9, () => {
		A("session-expired");
	}), n.subscribe(10, () => {
		"stopped" === D.state && "session-expired" === D.stateReason && R();
	}), {
		start: R,
		stop: function() {
			A("stopped-by-user");
		},
		isStopped: function() {
			return "stopped" === D.state;
		},
		isRunning: function() {
			return "running" === D.state;
		},
		isPaused: function() {
			return "paused" === D.state;
		}
	};
}
export { N as createRumProfiler };
