import{isEmpty,waitFor,deepClone}from"../../base/helpers.mjs";import{ElementBuilder}from"../../base/builder.mjs";import{Controller}from"../base.mjs";import{ButtonInputView}from"../../forms/input/misc.mjs";import{TableView}from"../../view/table.mjs";import{View}from"../../view/base.mjs";const E=new ElementBuilder;class LogGlanceView extends View{static tagName="enfugue-log-glance-view";static maximumLogs=15;constructor(t){super(t),this.start=(new Date).getTime()}async showMore(){isEmpty(this.onShowMore)||await this.onShowMore()}async setData(t){if(void 0!==this.node){let s=t.filter((t=>new Date(t.timestamp).getTime()>this.start));if(s.length>0){(new Date).getTime();let t=s.slice(0,this.constructor.maximumLogs).map((t=>t.content)).join("\n");this.show(),this.node.find(".logs").content(t)}}}async build(){let t=await super.build();return t.append(E.div().class("log-container").content(E.button().content(E.i().class("fa-regular fa-square-caret-right")).data("tooltip","Show More").on("click",(t=>{t.preventDefault(),t.stopPropagation(),this.showMore()})),E.div().class("logs").content("Welcome to ENFUGUE! When the diffusion engine logs to file, the most recent lines will appear here."))),t}}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"};static columnFormatters={timestamp:t=>t.replace("T"," ").split(".")[0]}}class LogsView extends View{static tagName="enfugue-logs-view";constructor(t,s){super(t),this.paused=!1,this.logsTable=new LogTableView(t,s),this.pauseLogs=new ButtonInputView(t,"pause",{value:"Pause Logs"}),this.pauseLogs.onChange((()=>{this.paused=!this.paused,this.paused?this.pauseLogs.setValue("Unpause Logs",!1):this.pauseLogs.setValue("Pause Logs",!1)}))}setData(t){this.paused||this.logsTable.setData(t,!1)}async build(){let t=await super.build();return t.append(await this.pauseLogs.getNode(),await this.logsTable.getNode()),t}}class LogsController extends Controller{static logsWindowWidth=600;static logsWindowHeight=700;static maximumDetailLogs=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.maximumDetailLogs),this.logs}async getLogsView(){return isEmpty(this.logsView)&&(await waitFor((()=>null!==this.logs&&void 0!==this.logs)),this.logsView=new LogsView(this.config,this.logs)),this.logsView}async startLogTailer(){this.timer=setInterval((async()=>{let t=await this.getLogs();isEmpty(this.logsView)||this.logsView.setData(t),isEmpty(this.glanceView)||this.glanceView.setData(t)}),this.constructor.logTailInterval)}async showLogDetails(){isEmpty(this.logWindow)?(this.logWindow=await this.spawnWindow("Engine Logs",await this.getLogsView(),this.constructor.logsWindowWidth,this.constructor.logsWindowHeight),this.logWindow.onClose((()=>{this.logWindow=null}))):this.logWindow.focus()}async initialize(){this.glanceView=new LogGlanceView(this.config),this.glanceView.onShowMore=()=>this.showLogDetails(),this.application.sidebar.addChild(this.glanceView),this.startLogTailer()}}export{LogsController as SidebarController};
