"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');
const require_perf_enabled = require('./enabled.js');
const require_perf_metrics = require('./metrics.js');

//#region src/perf/report.ts
/**
* @file Report-side helpers — `generatePerformanceReport` returns a multi-line
*   ASCII-bordered report string; `printPerformanceSummary` writes a
*   one-line-per-op summary through `debugLog`. Both gate on `isPerfEnabled()`
*   and use the same `getPerformanceSummary` rollup.
*/
/**
* Create a performance report for the current execution. Only available when
* DEBUG=perf is enabled.
*
* @example
*   import { generatePerformanceReport } from '@socketsecurity/lib/perf/report'
*
*   console.log(generatePerformanceReport())
*   // ╔═══════════════════════════════════════════════╗
*   // ║         Performance Report                    ║
*   // ╚═══════════════════════════════════════════════╝
*   //
*   // api-call:
*   //   Calls: 5
*   //   Avg:   246.8ms
*   //   Min:   100ms
*   //   Max:   500ms
*   //   Total: 1234ms
*
* @returns Formatted performance report
*/
function generatePerformanceReport() {
	if (!require_perf_enabled.isPerfEnabled() || require_perf__internal.performanceMetrics.length === 0) return "(no performance data collected - enable with DEBUG=perf)";
	const summary = require_perf_metrics.getPerformanceSummary();
	const operations = require_primordials_object.ObjectKeys(summary).sort();
	let report = "\n╔═══════════════════════════════════════════════╗\n";
	report += "║         Performance Report                    ║\n";
	report += "╚═══════════════════════════════════════════════╝\n\n";
	for (const operation of operations) {
		const stats = summary[operation];
		report += `${operation}:\n`;
		report += `  Calls: ${stats.count}\n`;
		report += `  Avg:   ${stats.avg}ms\n`;
		report += `  Min:   ${stats.min}ms\n`;
		report += `  Max:   ${stats.max}ms\n`;
		report += `  Total: ${stats.total}ms\n\n`;
	}
	const totalDuration = require_primordials_object.ObjectValues(summary).reduce((sum, s) => sum + s.total, 0);
	report += `Total measured time: ${require_primordials_math.MathRound(totalDuration * 100) / 100}ms\n`;
	return report;
}
/**
* Print performance summary to console. Only prints when DEBUG=perf is enabled.
*
* @example
*   import { printPerformanceSummary } from '@socketsecurity/lib/perf/report'
*
*   printPerformanceSummary()
*   // Performance Summary:
*   // api-call: 5 calls, avg 246.8ms (min 100ms, max 500ms, total 1234ms)
*   // file-read: 10 calls, avg 5ms (min 2ms, max 15ms, total 50ms)
*/
function printPerformanceSummary() {
	if (!require_perf_enabled.isPerfEnabled() || require_perf__internal.performanceMetrics.length === 0) return;
	const summary = require_perf_metrics.getPerformanceSummary();
	const operations = require_primordials_object.ObjectKeys(summary).sort();
	require_debug_output.debugLog("[perf]\n=== Performance Summary ===");
	for (const operation of operations) {
		const stats = summary[operation];
		require_debug_output.debugLog(`[perf] ${operation}: ${stats.count} calls, avg ${stats.avg}ms (min ${stats.min}ms, max ${stats.max}ms, total ${stats.total}ms)`);
	}
	require_debug_output.debugLog("[perf] =========================\n");
}

//#endregion
exports.generatePerformanceReport = generatePerformanceReport;
exports.printPerformanceSummary = printPerformanceSummary;