"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_math = require('../primordials/math.js');
const require_primordials_object = require('../primordials/object.js');
const require_debug_output = require('../debug/output.js');
const require_perf__internal = require('./_internal.js');

//#region src/perf/metrics.ts
/**
* @file Read-side helpers — `getPerformanceMetrics` returns a shallow copy of
*   the recorded rows, `getPerformanceSummary` rolls them up per operation
*   (count / total / avg / min / max), and `clearPerformanceMetrics` empties
*   the array in place.
*/
/**
* Clear all collected performance metrics.
*
* @example
*   import { clearPerformanceMetrics } from '@socketsecurity/lib/perf/metrics'
*
*   clearPerformanceMetrics()
*/
function clearPerformanceMetrics() {
	require_perf__internal.performanceMetrics.length = 0;
	require_debug_output.debugLog("[perf] Cleared performance metrics");
}
/**
* Get all collected performance metrics. Only available when DEBUG=perf is
* enabled.
*
* @example
*   import { getPerformanceMetrics } from '@socketsecurity/lib/perf/metrics'
*
*   const metrics = getPerformanceMetrics()
*   console.log(metrics)
*
* @returns Array of performance metrics
*/
function getPerformanceMetrics() {
	return [...require_perf__internal.performanceMetrics];
}
/**
* Get performance summary statistics.
*
* @example
*   import { getPerformanceSummary } from '@socketsecurity/lib/perf/metrics'
*
*   const summary = getPerformanceSummary()
*   console.log(summary)
*   // {
*   //   'api-call': { count: 5, total: 1234, avg: 246.8, min: 100, max: 500 },
*   //   'file-read': { count: 10, total: 50, avg: 5, min: 2, max: 15 }
*   // }
*
* @returns Summary of metrics grouped by operation
*/
function getPerformanceSummary() {
	const summary = { __proto__: null };
	for (const metric of require_perf__internal.performanceMetrics) {
		const { duration, operation } = metric;
		if (!summary[operation]) summary[operation] = {
			count: 0,
			total: 0,
			min: Number.POSITIVE_INFINITY,
			max: Number.NEGATIVE_INFINITY
		};
		const stats = summary[operation];
		stats.count++;
		stats.total += duration;
		stats.min = require_primordials_math.MathMin(stats.min, duration);
		stats.max = require_primordials_math.MathMax(stats.max, duration);
	}
	const result = { __proto__: null };
	for (const { 0: operation, 1: stats } of require_primordials_object.ObjectEntries(summary)) result[operation] = {
		count: stats.count,
		total: require_primordials_math.MathRound(stats.total * 100) / 100,
		avg: require_primordials_math.MathRound(stats.total / stats.count * 100) / 100,
		min: require_primordials_math.MathRound(stats.min * 100) / 100,
		max: require_primordials_math.MathRound(stats.max * 100) / 100
	};
	return result;
}

//#endregion
exports.clearPerformanceMetrics = clearPerformanceMetrics;
exports.getPerformanceMetrics = getPerformanceMetrics;
exports.getPerformanceSummary = getPerformanceSummary;