const require_logger_transport = require('../../logging/logger_transport.cjs');
const require_console_logger_transport = require('../../logging/console_logger_transport.cjs');
const require_sdk_shared_core_wasm_bindings = require('./vm/sdk_shared_core_wasm_bindings.cjs');

//#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 require_console_logger_transport.defaultLoggerTransport({
			level: wasmLogLevelToRestateLogLevel(level),
			replaying: false,
			source: require_logger_transport.LogSource.JOURNAL
		}, str);
	} catch (e) {
		require_console_logger_transport.defaultLoggerTransport({
			level: require_logger_transport.RestateLogLevel.ERROR,
			replaying: false,
			source: require_logger_transport.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 require_sdk_shared_core_wasm_bindings.LogLevel.TRACE: return require_logger_transport.RestateLogLevel.TRACE;
		case require_sdk_shared_core_wasm_bindings.LogLevel.DEBUG: return require_logger_transport.RestateLogLevel.DEBUG;
		case require_sdk_shared_core_wasm_bindings.LogLevel.INFO: return require_logger_transport.RestateLogLevel.INFO;
		case require_sdk_shared_core_wasm_bindings.LogLevel.WARN: return require_logger_transport.RestateLogLevel.WARN;
		case require_sdk_shared_core_wasm_bindings.LogLevel.ERROR: return require_logger_transport.RestateLogLevel.ERROR;
	}
}

//#endregion
exports.destroyLogger = destroyLogger;
exports.registerLogger = registerLogger;
exports.vm_log = vm_log;