import{isRegisteredLanguageResource as e,translate as o}from"../locale/language.js";import{checkArg as r}from"./checkArg.js";import{format as s}from"./format/string.js";export var LogLevel;!function(e){e[e.ALL=0]="ALL",e[e.TRACE=1]="TRACE",e[e.DEBUG=2]="DEBUG",e[e.INFO=3]="INFO",e[e.WARN=4]="WARN",e[e.ERROR=5]="ERROR",e[e.FATAL=6]="FATAL",e[e.OFF=7]="OFF"}(LogLevel||(LogLevel={}));export class LogManager{factory;_loggers=new Map;_logLevels=new Map;getLogger(e){if(r("name",e).isNotMissing(),this._loggers.has(e))return this._loggers.get(e);{const o=this.factory?this.factory(e,this):new ConsoleLogger(e,this);return this._loggers.set(e,o),o}}getLogLevel(e){let o=Array.from(this._logLevels.keys());return o=o.filter((o=>e.startsWith(o))),o=o.sort(((e,o)=>o.length-e.length)),o.length?this._logLevels.get(o[0]):this._logLevels.has("root")?this._logLevels.get("root"):LogLevel.DEBUG}setLogLevel(e,o){this._logLevels.set(e,o)}}let g=new LogManager;export function getLogger(e){return g.getLogger(e)}export function setLogLevel(e,o){r("name",e).isNotMissing(),r("logLevel",o).isNotMissing(),g.setLogLevel(e,o)}export function setLoggerFactory(e){g.factory=e}export function reset(){g=new LogManager}export class LoggerBase{name;get level(){return this._logManager.getLogLevel(this.name)}set level(e){this._logManager.setLogLevel(this.name,e)}_logManager;constructor(e,o){this.name=e,this._logManager=o}log(g,t,...l){if(r("level",g).satisfies((e=>e!==LogLevel.ALL&&e!==LogLevel.OFF),"Invalid log level."),!this._shouldLog(g))return;let a,L,n,i=g;try{"string"==typeof t?e(t)?(a=o({key:t},...l),L=t):a=s(t,...l):(n=t.error,"messageResource"in t?(L=t.messageResource,a=L,e(L)?a=o({key:L,locale:t.locale},...l):null!=t.message&&(a=s({...t,format:t.message},...l))):a=s({...t,format:t.message},...l))}catch(e){const o="string"==typeof t?t:"message"in t?t.message:t.messageResource;let r="";try{r=JSON.stringify(l)}catch(e){r=e.message}a=`Error occurred while formatting log message: "${e.message}". Original log message "${o}"${l.length>0?` with parameters: ${r}`:""}`,i=LogLevel.ERROR}this._log({level:i,time:new Date,message:a,messageResource:L,messageArgs:l,error:n})}trace(e,...o){this.log(LogLevel.TRACE,e,...o)}debug(e,...o){this.log(LogLevel.DEBUG,e,...o)}info(e,...o){this.log(LogLevel.INFO,e,...o)}warn(e,...o){this.log(LogLevel.WARN,e,...o)}error(e,...o){this.log(LogLevel.ERROR,e,...o)}fatal(e,...o){this.log(LogLevel.FATAL,e,...o)}_shouldLog(e){return e>=this.level}}export class ConsoleLogger extends LoggerBase{_log(e){const o=`[${this.name}]: ${e.message}`;switch(e.level){case LogLevel.ERROR:case LogLevel.FATAL:console.error(o),e.error&&(console.error(e.error),e.error.hasOwnProperty("errors")&&console.error(e.error.errors));break;case LogLevel.INFO:console.info(o),e.error&&console.info(e.error);break;case LogLevel.WARN:console.warn(o),e.error&&console.warn(e.error);break;case LogLevel.OFF:return;case LogLevel.ALL:case LogLevel.TRACE:case LogLevel.DEBUG:default:console.log(o),e.error&&console.log(e.error)}}}