import { getMessage, toMeta, withSanitizer } from "./utils.js";
import { once, reset } from "./once.js";
import { getRemappedLevel } from "./remap.js";
import { isString } from "@sindresorhus/is";

//#region lib/logger/renovate-logger.ts
const loggerLevels = [
	"trace",
	"debug",
	"info",
	"warn",
	"error",
	"fatal"
];
var RenovateLogger = class RenovateLogger {
	logger = { once: { reset } };
	once = this.logger.once;
	bunyanLogger;
	context;
	meta;
	constructor(bunyanLogger, context, meta) {
		this.bunyanLogger = bunyanLogger;
		this.context = context;
		this.meta = meta;
		for (const level of loggerLevels) {
			this.logger[level] = this.logFactory(level);
			this.logger.once[level] = this.logOnceFn(level);
		}
	}
	trace(p1, p2) {
		this.log("trace", p1, p2);
	}
	debug(p1, p2) {
		this.log("debug", p1, p2);
	}
	info(p1, p2) {
		this.log("info", p1, p2);
	}
	warn(p1, p2) {
		this.log("warn", p1, p2);
	}
	error(p1, p2) {
		this.log("error", p1, p2);
	}
	fatal(p1, p2) {
		this.log("fatal", p1, p2);
	}
	addSerializers(serializers) {
		this.bunyanLogger.addSerializers(serializers);
	}
	addStream(stream) {
		this.bunyanLogger.addStream(withSanitizer(stream));
	}
	childLogger() {
		return new RenovateLogger(this.bunyanLogger.child({}), this.context, this.meta);
	}
	get logContext() {
		return this.context;
	}
	set logContext(context) {
		this.context = context;
	}
	setMeta(obj) {
		this.meta = { ...obj };
	}
	addMeta(obj) {
		this.meta = {
			...this.meta,
			...obj
		};
	}
	removeMeta(fields) {
		for (const key of Object.keys(this.meta)) if (fields.includes(key)) delete this.meta[key];
	}
	logFactory(_level) {
		return (p1, p2) => {
			const meta = {
				logContext: this.context,
				...this.meta,
				...toMeta(p1)
			};
			const msg = getMessage(p1, p2);
			let level = _level;
			if (isString(msg)) {
				const remappedLevel = getRemappedLevel(msg);
				/* v8 ignore next 4 -- not easily testable */
				if (remappedLevel) {
					meta.oldLevel = level;
					level = remappedLevel;
				}
				this.bunyanLogger[level](meta, msg);
			} else this.bunyanLogger[level](meta);
		};
	}
	logOnceFn(level) {
		const logOnceFn = (p1, p2) => {
			once(() => {
				const logFn = this[level].bind(this);
				if (isString(p1)) logFn(p1);
				else logFn(p1, p2);
			}, logOnceFn, p1, p2);
		};
		return logOnceFn;
	}
	log(level, p1, p2) {
		const logFn = this.logger[level];
		if (isString(p1)) logFn(p1);
		else logFn(p1, p2);
	}
};

//#endregion
export { RenovateLogger };
//# sourceMappingURL=renovate-logger.js.map