import { LogSource, RestateLogLevel } from "../../logging/logger_transport.js";
import { defaultLoggerTransport } from "../../logging/console_logger_transport.js";
import { LogLevel } from "./vm/sdk_shared_core_wasm_bindings.js";

//#region src/endpoint/handlers/core_logging.ts
/**
* This file contains the shared state used to install a logger that the shared-core uses for logging.
*/
const invocationLoggers = /* @__PURE__ */ new Map();
const logsTextDecoder = new TextDecoder("utf-8", { fatal: false });
/**
* The shared core propagates logs to the SDK invoking this method.
* When possible, it provides an invocationId, which is used to access the registered invocationLoggers, that should contain the logger per invocation id.
*/
function vm_log(level, strBytes, loggerId) {
	try {
		const logger = loggerId && invocationLoggers.get(loggerId) || void 0;
		const str = logsTextDecoder.decode(strBytes);
		if (logger !== void 0) logger.logForLevel(wasmLogLevelToRestateLogLevel(level), str);
		else defaultLoggerTransport({
			level: wasmLogLevelToRestateLogLevel(level),
			replaying: false,
			source: LogSource.JOURNAL
		}, str);
	} catch (e) {
		defaultLoggerTransport({
			level: RestateLogLevel.ERROR,
			replaying: false,
			source: LogSource.SYSTEM
		}, "Unexpected error thrown while trying to log: " + e?.toString());
	}
}
function registerLogger(loggerId, logger) {
	invocationLoggers.set(loggerId, logger);
}
function destroyLogger(loggerId) {
	invocationLoggers.delete(loggerId);
}
function wasmLogLevelToRestateLogLevel(level) {
	switch (level) {
		case LogLevel.TRACE: return RestateLogLevel.TRACE;
		case LogLevel.DEBUG: return RestateLogLevel.DEBUG;
		case LogLevel.INFO: return RestateLogLevel.INFO;
		case LogLevel.WARN: return RestateLogLevel.WARN;
		case LogLevel.ERROR: return RestateLogLevel.ERROR;
	}
}

//#endregion
export { destroyLogger, registerLogger, vm_log };
//# sourceMappingURL=core_logging.js.map