import{MenuController}from"../menu.mjs";import{ModelTableView}from"../../view/table.mjs";import{ImageView}from"../../view/image.mjs";import{sleep,isEmpty,humanDuration,downloadAsDataURL}from"../../base/helpers.mjs";import{ElementBuilder}from"../../base/builder.mjs";const E=new ElementBuilder({invocationOutputs:"enfugue-invocation-outputs",invocationOutput:"enfugue-invocation-output"});class InvocationTableView extends ModelTableView{static className="invocation-history";static searchFields=["plan"];static buttons=[{icon:"fa-solid fa-trash",label:"Delete",click:async function(t){await InvocationTableView.deleteInvocation(t.id),await sleep(250),await this.parent.requery()}}];static sortGroups=[{column:"started",direction:"desc"}];static printKwargs={prompt:"Prompt",negative_prompt:"Negative Prompt",width:"Width",height:"Height",image_width:"Engine Width",image_height:"Engine Height",model:"Checkpoint",inversion:"Textual Inversion(s)",lora:"LoRA(s)"};static columnFormatters={duration:t=>humanDuration(parseFloat(t),!0,!0),plan:t=>(t.layers=isEmpty(t.layers)?"(none)":`(${t.layers.length} layer${1==t.layers.length?"":"s"})`,JSON.stringify(t)),prompt:(t,i)=>i.plan.prompt,seed:(t,i)=>`${i.plan.seed}`,outputs:async function(t,i){if(t>0){let a=E.invocationOutputs();if(!isEmpty(i.plan.animation_frames)&&i.plan.animation_frames>0){let t=`/api/invocation/animation/images/${i.id}.mp4`,o=`/api/invocation/animation/images/${i.id}.gif`,e=`/api/invocation/animation/thumbnails/${i.id}.mp4`,n=E.invocationOutput().content(E.video().content(E.source().src(e)).autoplay(!0).muted(!0).loop(!0),E.div().class("buttons").content(E.button().content(E.i().class("fa-solid fa-arrow-up-right-from-square")).on("click",(i=>{window.open(t,"_blank")})).data("tooltip","Click to View in Separate Tab"),E.button().content(E.i().class("fa-solid fa-film")).on("click",(t=>{t.stopPropagation();let a=new Array(i.plan.animation_frames).fill(null).map(((t,a)=>`/api/invocation/images/${i.id}_${a}.png`));InvocationTableView.showAnimationFrames(a)})).data("tooltip","Click to View Frames"),E.button().content(E.i().class("fa-solid fa-file-video")).on("click",(t=>{t.stopPropagation(),window.open(o,"_blank")})).data("tooltip","Click to View as .GIF"),E.button().content(E.i().class("fa-solid fa-edit")).on("click",(async i=>{i.stopPropagation(),InvocationTableView.initializeStateFromImage(await downloadAsDataURL(t),!0)})).data("tooltip","Click to Edit"))).data("tooltip","Click to View").on("click",(()=>{InvocationTableView.spawnVideoPlayer(t)}));a.append(n)}else for(let o=0;o<t;o++){let t=`${i.id}_${o}.png`,e=`/api/invocation/images/${t}`,n=`/api/invocation/thumbnails/${t}`,s=new ImageView(this.config,n,!1),l=E.invocationOutput().content(await s.getNode(),E.div().class("buttons").content(E.button().content(E.i().class("fa-solid fa-arrow-up-right-from-square")).on("click",(t=>{window.open(e,"_blank")})).data("tooltip","Click to View in Separate Tab"),E.button().content(E.i().class("fa-solid fa-edit")).on("click",(async t=>{t.stopPropagation(),InvocationTableView.initializeStateFromImage(await downloadAsDataURL(e),!1)})).data("tooltip","Click to Edit"))).data("tooltip","Click to View").on("click",(async()=>{InvocationTableView.showImage(e)}));a.append(l)}return a}return isEmpty(i.error)?"None":`Error: ${i.error}`}};static columns={started:"Started",duration:"Duration",seed:"Seed",prompt:"Prompt",plan:"Parameters",outputs:"Output"}}class ResultsController extends MenuController{static historyTableWidth=1e3;static historyTableHeight=500;static menuName="Results";static menuIcon="fa-solid fa-images";static menuShortcut="r";async initialize(){await super.initialize(),InvocationTableView.deleteInvocation=t=>{this.model.delete(`/invocation/${t}`)},InvocationTableView.initializeStateFromImage=(t,i)=>this.application.initializeStateFromImage(t,!0,null,null,i),InvocationTableView.spawnVideoPlayer=t=>{this.application.spawnVideoPlayer(t)},InvocationTableView.showImage=async t=>{this.application.samples.activeIndex=0,await this.application.samples.setSamples([t],!1)},InvocationTableView.showAnimationFrames=async t=>{await this.application.samples.setSamples(t,!0),setTimeout((()=>this.application.samples.setPlay(!0)),250)}}async createResultsWindow(){return await this.spawnWindow("Results",new InvocationTableView(this.config,this.model.DiffusionInvocation),this.constructor.historyTableWidth,this.constructor.historyTableHeight)}async onClick(){isEmpty(this.historyWindow)?(this.historyWindow=await this.createResultsWindow(),this.historyWindow.onClose((()=>{delete this.historyWindow}))):this.historyWindow.focus()}}export{ResultsController as MenuController};
