import { regEx } from "../util/regex.js";
import { Stream } from "node:stream";
import * as util$1 from "node:util";
import stringify from "json-stringify-pretty-compact";

//#region lib/logger/pretty-stdout.ts
const bunyanFields = [
	"name",
	"hostname",
	"pid",
	"level",
	"v",
	"time",
	"msg",
	"start_time"
];
const metaFields = [
	"repository",
	"baseBranch",
	"packageFile",
	"depType",
	"dependency",
	"dependencies",
	"branch"
];
const levels = {
	10: util$1.styleText("gray", "TRACE"),
	20: util$1.styleText("blue", "DEBUG"),
	30: util$1.styleText("green", " INFO"),
	40: util$1.styleText("magenta", " WARN"),
	50: util$1.styleText("red", "ERROR"),
	60: util$1.styleText("bgRed", "FATAL")
};
function indent(str, leading = false) {
	return (leading ? "       " : "") + str.split(regEx(/\r?\n/)).join("\n       ");
}
function getMeta(rec) {
	if (!rec) return "";
	let res = rec.module ? ` [${rec.module}]` : ``;
	const filteredMeta = metaFields.filter((elem) => rec[elem]);
	if (!filteredMeta.length) return res;
	res = ` (${filteredMeta.map((field) => `${field}=${String(rec[field])}`).join(", ")})${res}`;
	return util$1.styleText("gray", res);
}
function getDetails(rec) {
	if (!rec) return "";
	const recFiltered = { ...rec };
	delete recFiltered.module;
	Object.keys(recFiltered).forEach((key) => {
		if (key === "logContext" || bunyanFields.includes(key) || metaFields.includes(key)) delete recFiltered[key];
	});
	const remainingKeys = Object.keys(recFiltered);
	if (remainingKeys.length === 0) return "";
	return `${remainingKeys.map((key) => `${indent(`"${key}": ${stringify(recFiltered[key])}`, true)}`).join(",\n")}\n`;
}
function formatRecord(rec) {
	const level = levels[rec.level];
	const msg = `${indent(rec.msg)}`;
	const meta = getMeta(rec);
	const details = getDetails(rec);
	return util$1.format("%s: %s%s\n%s", level, msg, meta, details);
}
var RenovateStream = class extends Stream {
	readable;
	writable;
	constructor() {
		super();
		this.readable = true;
		this.writable = true;
	}
	write(data) {
		this.emit("data", formatRecord(data));
		return true;
	}
};

//#endregion
export { RenovateStream };
//# sourceMappingURL=pretty-stdout.js.map