import { r as ConsoleLogger, t as LogLevelPolicyFactory } from "./LogLevelPolicyFactory-DCUzIN6G.js";
import { n as __decorateMetadata, t as __decorateParam } from "./decorateParam-BWxkAUSj.js";
import { t as __decorate } from "./decorate-CXWmflG_.js";
import { inject, injectable } from "@codemation/core";
import path from "node:path";
import { promises } from "node:fs";

//#region src/bootstrap/perf/BootTimer.ts
var BootTimer = class {
	static enabled = false;
	static root = null;
	static current = null;
	/** Enable boot tracing for the lifetime of the current process. Idempotent. */
	static enable() {
		if (this.enabled) return;
		this.enabled = true;
		this.root = {
			name: "boot",
			startNs: process.hrtime.bigint(),
			endNs: null,
			children: [],
			parent: null
		};
		this.current = this.root;
	}
	static isEnabled() {
		return this.enabled;
	}
	/** Wrap an async phase. Pass-through when disabled. */
	static async measureAsync(name, fn) {
		if (!this.enabled || !this.current) return await fn();
		const node = this.pushPhase(name);
		try {
			return await fn();
		} finally {
			this.popPhase(node);
		}
	}
	/** Wrap a sync phase. Pass-through when disabled. */
	static measure(name, fn) {
		if (!this.enabled || !this.current) return fn();
		const node = this.pushPhase(name);
		try {
			return fn();
		} finally {
			this.popPhase(node);
		}
	}
	/**
	* Manual start/stop for fire-and-forget phases (e.g. a spawned child process you only
	* stop once a readiness probe succeeds). Returns the stop function.
	*/
	static start(name) {
		if (!this.enabled || !this.current) return () => {};
		const node = this.pushPhase(name);
		return () => this.popPhase(node);
	}
	/** Print the tree to stderr and write JSON to the given path. Finalizes the root span. */
	static async finish(outputJsonPath) {
		if (!this.enabled || !this.root) return;
		if (this.root.endNs === null) this.root.endNs = process.hrtime.bigint();
		const totalMs = nodeMs(this.root);
		process.stderr.write(`\n=== Boot trace (total ${totalMs.toFixed(0)}ms) ===\n`);
		writeTree(this.root, 0, totalMs);
		process.stderr.write("=========================================\n\n");
		if (outputJsonPath) try {
			await promises.mkdir(path.dirname(outputJsonPath), { recursive: true });
			await promises.writeFile(outputJsonPath, JSON.stringify(this.snapshot(), null, 2), "utf8");
		} catch (error) {
			process.stderr.write(`[boot-timer] failed to write JSON trace: ${String(error)}\n`);
		}
	}
	/** Return a plain-object snapshot of the current tree (for tests / diffing). */
	static snapshot() {
		if (!this.root) return {
			name: "boot",
			ms: 0,
			pct: 100,
			children: []
		};
		const totalMs = nodeMs(this.root);
		return toReport(this.root, totalMs);
	}
	/** Test helper: reset all state. Do not call in production code. */
	static reset() {
		this.enabled = false;
		this.root = null;
		this.current = null;
	}
	static pushPhase(name) {
		const node = {
			name,
			startNs: process.hrtime.bigint(),
			endNs: null,
			children: [],
			parent: this.current
		};
		this.current.children.push(node);
		this.current = node;
		return node;
	}
	static popPhase(node) {
		node.endNs = process.hrtime.bigint();
		this.current = node.parent ?? this.root;
	}
};
function nodeMs(node) {
	const end = node.endNs ?? node.startNs;
	return Number(end - node.startNs) / 1e6;
}
function toReport(node, totalMs) {
	const ms = nodeMs(node);
	return {
		name: node.name,
		ms,
		pct: totalMs > 0 ? ms / totalMs * 100 : 0,
		children: node.children.map((child) => toReport(child, totalMs))
	};
}
function writeTree(node, depth, totalMs) {
	const ms = nodeMs(node);
	const pct = totalMs > 0 ? (ms / totalMs * 100).toFixed(1) : "—";
	const indent = "  ".repeat(depth);
	const msStr = ms.toFixed(0).padStart(6);
	const pctStr = pct.padStart(5);
	process.stderr.write(`${indent}${msStr}ms  ${pctStr}%  ${node.name}\n`);
	for (const child of node.children) writeTree(child, depth + 1, totalMs);
}

//#endregion
//#region src/infrastructure/logging/FilteringLogger.ts
var FilteringLogger = class {
	constructor(inner, scope, filter) {
		this.inner = inner;
		this.scope = scope;
		this.filter = filter;
	}
	info(message, exception) {
		if (!this.filter({
			scope: this.scope,
			level: "info",
			message
		})) return;
		this.inner.info(message, exception);
	}
	warn(message, exception) {
		if (!this.filter({
			scope: this.scope,
			level: "warn",
			message
		})) return;
		this.inner.warn(message, exception);
	}
	error(message, exception) {
		if (!this.filter({
			scope: this.scope,
			level: "error",
			message
		})) return;
		this.inner.error(message, exception);
	}
	debug(message, exception) {
		if (!this.filter({
			scope: this.scope,
			level: "debug",
			message
		})) return;
		this.inner.debug(message, exception);
	}
};

//#endregion
//#region src/infrastructure/logging/PerformanceLogPolicy.ts
/**
* Opt-in detailed timing / diagnostics lines (used with `ServerLoggerFactory#createPerformanceDiagnostics`).
*
* Set `CODEMATION_PERFORMANCE_LOGGING=true` to enable. Otherwise those loggers are silent regardless of level.
*/
var PerformanceLogPolicy = class {
	shouldEmitDetailedTiming() {
		return process.env.CODEMATION_PERFORMANCE_LOGGING === "true";
	}
};

//#endregion
//#region src/infrastructure/logging/PerformanceLogPolicyFactory.ts
/**
* Process-wide {@link PerformanceLogPolicy} singleton (same pattern as {@link LogLevelPolicyFactory}).
*/
var PerformanceLogPolicyFactory = class {
	policy = new PerformanceLogPolicy();
	create() {
		return this.policy;
	}
};
const performanceLogPolicyFactory = new PerformanceLogPolicyFactory();

//#endregion
//#region src/infrastructure/logging/ServerLoggerFactory.ts
var _ref;
let ServerLoggerFactory = class ServerLoggerFactory$1 {
	constructor(logLevelPolicyFactory) {
		this.logLevelPolicyFactory = logLevelPolicyFactory;
	}
	create(scope) {
		return new ConsoleLogger(scope, this.logLevelPolicyFactory.create());
	}
	createFiltered(scope, filter) {
		return new FilteringLogger(this.create(scope), scope, filter);
	}
	createPerformanceDiagnostics(scope) {
		return this.createFiltered(scope, (_entry) => performanceLogPolicyFactory.create().shouldEmitDetailedTiming());
	}
};
ServerLoggerFactory = __decorate([
	injectable(),
	__decorateParam(0, inject(LogLevelPolicyFactory)),
	__decorateMetadata("design:paramtypes", [typeof (_ref = typeof LogLevelPolicyFactory !== "undefined" && LogLevelPolicyFactory) === "function" ? _ref : Object])
], ServerLoggerFactory);

//#endregion
export { FilteringLogger as a, PerformanceLogPolicy as i, PerformanceLogPolicyFactory as n, BootTimer as o, performanceLogPolicyFactory as r, ServerLoggerFactory as t };
//# sourceMappingURL=ServerLoggerFactory-Ckk52S3w.js.map