import { init_context, runWithLogContext } from "../../../utils/logger/context.js";
import { randomUUID } from "node:crypto";
import { createMiddleware } from "hono/factory";
//#region src/gateway/hono/middleware/log-context.ts
init_context();
/**
* Binds request (and optional session) correlation into AsyncLocalStorage so
* all pino logs in the request inherit requestId / sessionId without a child logger.
*/
function logContextMiddleware() {
	return createMiddleware(async (c, next) => {
		const headerRequestId = c.req.header("x-request-id")?.trim();
		const requestId = headerRequestId && headerRequestId.length > 0 ? headerRequestId : randomUUID();
		const sessionHeader = c.req.header("x-session-id")?.trim();
		c.header("X-Request-Id", requestId);
		return runWithLogContext({
			requestId,
			...sessionHeader ? { sessionId: sessionHeader } : {}
		}, async () => {
			await next();
		});
	});
}
//#endregion
export { logContextMiddleware };

//# sourceMappingURL=log-context.js.map