import{MenuController}from"../menu.mjs";import{ParentView}from"../../view/base.mjs";import{TableView}from"../../view/table.mjs";import{StringInputView}from"../../view/forms/input.mjs";import{ImageView,ImageInspectorView}from"../../view/image.mjs";import{isEmpty,humanDuration,sleep}from"../../base/helpers.mjs";import{ElementBuilder}from"../../base/builder.mjs";const imageWidthPadding=2,imageHeightPadding=100,E=new ElementBuilder({invocationOutputs:"enfugue-invocation-outputs",invocationOutput:"enfugue-invocation-output"});class HistoryTableView extends TableView{static buttons=[{icon:"fa-solid fa-repeat",label:"Reload",click:async function(t){await HistoryTableView.reloadHistory(t)}},{icon:"fa-solid fa-trash",label:"Delete",click:async function(t){await HistoryTableView.deleteHistory(t)}}];static sort=[["timestamp",!0]];static columnFormatters={timestamp:t=>new Date(t).toLocaleString()};static columns={timestamp:"Timestamp",summary:"Summary"};static className="history-table-view"}class HistoryController extends MenuController{static historyTableWidth=800;static historyTableHeight=500;static historyPreviewHeight=80;static menuName="History";static menuIcon="fa-solid fa-clock-rotate-left";static searchHistoryConfig={placeholder:"Start typing to search…"};static searchHistoryDebounceDelay=250;async initialize(){await super.initialize(),HistoryTableView.reloadHistory=t=>{this.application.setState(t.state,!0),this.application.history.deleteByID(t.id);let e=new Date(t.timestamp).toLocaleString();this.notify("info","Session Reloaded",`The session from ${e} has been successfully restored.`),setTimeout((()=>this.resetHistory()),150)},HistoryTableView.deleteHistory=t=>{this.application.history.deleteByID(t.id),setTimeout((()=>this.resetHistory()),150)},this.searchText=""}resetHistory(){return new Promise(((t,e)=>{this.getHistory().then((e=>{this.historyTable.setData(e,!1),t()})).catch(e)}))}async getHistory(){let t;return t=isEmpty(this.searchText)?await this.application.history.getHistoryItems():await this.application.history.searchHistoryItems(this.searchText),t.map((t=>{let e={};if(isEmpty(t.state.model)||(e.Model=t.state.model.model),isEmpty(t.state.canvas)||(e.Dimensions=`${t.state.canvas.width}px × ${t.state.canvas.height}px`),isEmpty(t.state.prompts)||(e.Prompt=t.state.prompts.prompt,isEmpty(t.state.prompts.negativePrompt)||(e["Negative Prompt"]=t.state.prompts.negativePrompt)),!isEmpty(t.state.images)){let i=1;for(let s of t.state.images){let t={};switch(s.classname){case"ImageEditorImageNodeView":t.Type="Image";let e=[];s.inpaint&&e.push("Inpainting"),s.infer&&e.push("Inference"),s.control&&e.push("ControlNet"),s.removeBackground&&e.push("Background Removal"),e.length>0&&(t.Features=e.join(", "));break;case"ImageEditorPromptNodeView":t.Type="Prompt";break;case"ImageEditorScribbleNodeView":t.Type="Scribble"}t.Dimensions=`${s.w}px × ${s.h}px`,t.Position=`(${s.x}px, ${s.y}px)`,isEmpty(s.prompt)||(t.Prompt=s.prompt);let o=Object.getOwnPropertyNames(t).map((e=>`<u>${e}</u>: ${t[e]}`)).join(", ");e["Node "+i++]=`<span style='font-size: 0.8em'>${o}</span>`}}let i=Object.getOwnPropertyNames(e).map((t=>`<strong>${t}</strong>: ${e[t]}`)).join(", ");return{id:t.id,state:t.state,timestamp:t.timestamp,summary:i}}))}async createHistoryWindow(){this.searchHistory=new StringInputView(this.config,"search",this.constructor.searchHistoryConfig),this.searchHistory.onInput((t=>{clearTimeout(this.searchTimer),this.searchTimer=setTimeout((()=>{this.searchText=t,this.resetHistory()}),this.constructor.searchHistoryDebounceDelay)})),this.historyTable=new HistoryTableView(this.config,await this.getHistory());let t=new ParentView(this.config);return t.addClass("history-view"),await t.addChild(this.searchHistory),await t.addChild(this.historyTable),await this.spawnWindow("History",t,this.constructor.historyTableWidth,this.constructor.historyTableHeight)}async onClick(){isEmpty(this.historyWindow)?(this.historyWindow=await this.createHistoryWindow(),this.historyWindow.onClose((()=>{delete this.historyWindow}))):this.historyWindow.focus()}}export{HistoryController as MenuController};
