/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
require("../../../_virtual/_rolldown/runtime.cjs");
let node_perf_hooks = require("node:perf_hooks");
//#region ../css/dist/css/perf.mjs
/*! © 2026 Yahoo, Inc. UDS CSS v0.0.0-development */
const PERF_PREFIX = "uds:css:";
const isPerfEnabled = () => process.env.UDS_PERF === "1";
const memorySnapshots = [];
const captureMemory = (label) => {
	if (!isPerfEnabled()) return;
	const mem = process.memoryUsage();
	memorySnapshots.push({
		label,
		rss: mem.rss,
		heapUsed: mem.heapUsed,
		heapTotal: mem.heapTotal,
		external: mem.external,
		timestamp: node_perf_hooks.performance.now()
	});
};
const measureAsync = async (name, fn, detail) => {
	if (!isPerfEnabled()) return fn();
	const markName = `${PERF_PREFIX}${name}`;
	const startMark = `${markName}:start`;
	const endMark = `${markName}:end`;
	node_perf_hooks.performance.mark(startMark, { detail });
	const result = await fn();
	node_perf_hooks.performance.mark(endMark);
	node_perf_hooks.performance.measure(markName, startMark, endMark);
	return result;
};
const collectTimings = () => {
	return node_perf_hooks.performance.getEntriesByType("measure").filter((entry) => entry.name.startsWith(PERF_PREFIX)).map((entry) => ({
		name: entry.name.slice(8),
		durationMs: Math.round(entry.duration * 100) / 100,
		startTime: Math.round(entry.startTime * 100) / 100,
		detail: entry.detail
	}));
};
const collectPerfData = () => ({
	timings: collectTimings(),
	memory: [...memorySnapshots]
});
const clearTimings = () => {
	node_perf_hooks.performance.clearMarks();
	node_perf_hooks.performance.clearMeasures();
	memorySnapshots.length = 0;
};
const formatMs = (ms) => {
	if (ms < 1e3) return `${Math.round(ms)}ms`;
	return `${(ms / 1e3).toFixed(1)}s`;
};
const formatMemoryMB = (bytes) => `${(bytes / 1024 / 1024).toFixed(0)} MB`;
const formatTimingTree = (timings, totalMs) => {
	const lines = [];
	const grouped = /* @__PURE__ */ new Map();
	for (const entry of timings) {
		const group = entry.name.split(":")[0] ?? "other";
		const existing = grouped.get(group) ?? [];
		existing.push(entry);
		grouped.set(group, existing);
	}
	for (const [group, entries] of grouped) {
		const topLevel = entries.filter((e) => e.name === group || e.name.split(":").length === 1).sort((a, b) => b.durationMs - a.durationMs)[0];
		const subEntries = entries.filter((e) => e.name !== group && e.name.startsWith(`${group}:`));
		if (topLevel) {
			const pct = totalMs > 0 ? (topLevel.durationMs / totalMs * 100).toFixed(1) : "0.0";
			lines.push(`  ${group}: ${formatMs(topLevel.durationMs)} (${pct}%)`);
		}
		for (const sub of subEntries.sort((a, b) => b.durationMs - a.durationMs)) {
			const subName = sub.name.slice(group.length + 1);
			lines.push(`    ├─ ${subName}: ${formatMs(sub.durationMs)}`);
		}
	}
	return lines.join("\n");
};
const printPerfSummary = (totalMs) => {
	if (!isPerfEnabled()) return;
	const data = collectPerfData();
	console.log("\n\x1B[35mPhase breakdown:\x1B[0m");
	console.log(formatTimingTree(data.timings, totalMs));
	if (data.memory.length > 0) {
		const peak = data.memory.reduce((max, snap) => snap.rss > max.rss ? snap : max, data.memory[0]);
		console.log(`  Peak memory: ${formatMemoryMB(peak.rss)} RSS, ${formatMemoryMB(peak.heapUsed)} heap`);
	}
	console.log("");
};
//#endregion
exports.captureMemory = captureMemory;
exports.clearTimings = clearTimings;
exports.isPerfEnabled = isPerfEnabled;
exports.measureAsync = measureAsync;
exports.printPerfSummary = printPerfSummary;
