import{isEmpty,deepClone}from"../../base/helpers.mjs";import{MenuController}from"../menu.mjs";import{FormView}from"../../view/forms/base.mjs";import{TableView}from"../../view/table.mjs";import{ParentView}from"../../view/base.mjs";import{StringInputView,ListMultiInputView}from"../../view/forms/input.mjs";class LogLevelSelectInputView extends ListMultiInputView{static defaultOptions=["DEBUG","INFO","WARN","ERROR","CRITICAL"]}class LogTableView extends TableView{static className="log-table-view";static canSort=!1;static applyDefaultSort=!1;static columns={timestamp:"Timestamp",logger:"Logger",level:"Level",content:"Content"}}class LogFilterFormView extends FormView{static autoSubmit=!0;static fieldSets={Filters:{level:{label:"Log Levels",class:LogLevelSelectInputView,config:{value:deepClone(LogLevelSelectInputView.defaultOptions)}},search:{label:"Search",class:StringInputView}}}}class SystemLogsController extends MenuController{static menuName="Engine Logs";static menuIcon="fa-solid fa-clipboard-list";static logsWindowWidth=600;static logsWindowHeight=700;static maximumLogs=100;static logTailInterval=5e3;async getLogs(){let t={};if(!isEmpty(this.lastLog)){let s=`${this.lastLog.getFullYear()}-${(this.lastLog.getMonth()+1).toString().padStart(2,"0")}-${this.lastLog.getDate().toString().padStart(2,"0")} ${this.lastLog.getHours().toString().padStart(2,"0")}:${this.lastLog.getMinutes().toString().padStart(2,"0")}:${this.lastLog.getSeconds().toString().padStart(2,"0")}`;t.since=s}isEmpty(this.levels)||(t.level=this.levels),isEmpty(this.search)||(t.search=this.search);let s=await this.model.get("/logs",null,t);return this.lastLog=new Date,isEmpty(this.logs)?this.logs=s:this.logs=s.concat(this.logs),this.logs=this.logs.slice(0,this.constructor.maximumLogs),this.logs}async getLogsView(){if(isEmpty(this.logsView)){let t=await this.getLogs();this.logsView=new ParentView(this.config),this.logsTable=await this.logsView.addChild(LogTableView,t)}return this.logsView}async startLogTailer(){this.timer=setInterval((async()=>{let t=await this.getLogs();isEmpty(this.logsView)||this.logsTable.setData(t,!1)}),this.constructor.logTailInterval)}async stopLogTailer(){clearInterval(this.timer),this.logTailer=null}async showLogTailer(){isEmpty(this.logTailer)?(this.logTailer=await this.spawnWindow("Engine Logs",await this.getLogsView(),this.constructor.logsWindowWidth,this.constructor.logsWindowHeight),this.startLogTailer(),this.logTailer.onClose((()=>{this.stopLogTailer()}))):this.logTailer.focus()}async onClick(){this.showLogTailer()}}export{SystemLogsController as MenuController};
