import{isEmpty}from"../../base/helpers.mjs";import{ElementBuilder}from"../../base/builder.mjs";import{ButtonInputView}from"../../forms/input.mjs";import{Controller}from"../base.mjs";import{View}from"../../view/base.mjs";const E=new ElementBuilder;class InvokeLoadingBarView extends View{static className="invoke-loader";static loaderClassName="loading-bar"}class EnfugueButton extends ButtonInputView{static className="invoke";static defaultValue="ENFUGUE"}class InvokeButtonController extends Controller{getLayers(){let e=this.application.layers.getState(),i=[],t=e.layers.map(((e,t)=>{let n={x:e.x,y:e.y,w:e.w,h:e.h,remove_background:e.removeBackground,image:e.src};switch(e.classname){case"ImageEditorScribbleNodeView":n.control_units=[{process:!1,controlnet:"scribble"}];break;case"ImageEditorImageNodeView":case"ImageEditorVideoNodeView":n.fit=e.fit,n.anchor=e.anchor,n.opacity=e.opacity,n.visibility=e.visibility,e.imagePrompt&&(n.ip_adapter_scale=e.imagePromptScale),e.control&&(n.control_units=e.controlnetUnits.map((e=>({process:e.processControlImage,start:e.conditioningStart,end:e.conditioningEnd,scale:e.conditioningScale,controlnet:e.controlnet})))),isEmpty(e.skipFrames)||(n.skip_frames=e.skipFrames),isEmpty(e.divideFrames)||(n.divide_frames=e.divideFrames),-1===["visible","denoised"].indexOf(e.visibility)&&isEmpty(n.ip_adapter_scale)&&isEmpty(n.control_units)&&i.push(t);break;default:throw`Unknown classname ${e.classname}`}return n}));if(!isEmpty(i)){let e=1===i.length?"":"s",n=1===i.length?"s":"ve",s=1===i.length?"this":"these",o=1===i.length?"was":"were",a=1===i.length?"it":"them";return this.notify("warn",`Unused Layer${e}`,`${i.length} layer${e} ha${n} no role assigned, ${s} layer${e} ${o} not sent to the backend. Add a role to ${s} layer${e} to use ${a}, like selecting "Visible" or "Denoised" visibility mode, using it with IP Adapter, and/or assigning one or more control units.`),t.filter(((e,t)=>-1===i.indexOf(t)))}return t}async tryInvoke(){this.isInvoking=!0,this.loadingBar.loading(),this.invokeButton.disable().addClass("sliding-gradient");try{this.application.autosave(),await this.application.invoke({layers:this.getLayers()})}catch(e){console.error(e);let i=`${e}`;if(isEmpty(e.detail)?isEmpty(e.title)||(i=e.title):i=e.detail,-1!==i.toLowerCase().indexOf("engine process died"))return this.notify("warn","Engine Didn't Start","The diffusion engine process exited before it started responding to requests. Waiting a moment and trying again."),await this.tryInvoke();this.notify("error","Couldn't Start",i)}this.invokeButton.enable().removeClass("sliding-gradient"),this.loadingBar.doneLoading(),this.application.autosave(),this.isInvoking=!1}async initialize(){this.invokeButton=new EnfugueButton(this.config),this.invokeButton.onChange((()=>this.tryInvoke())),this.loadingBar=new InvokeLoadingBarView,await this.application.sidebar.addChild(this.invokeButton),await this.application.sidebar.addChild(this.loadingBar),this.subscribe("tryInvoke",(()=>{!0!==this.isInvoking&&this.tryInvoke()}))}}export{InvokeButtonController as SidebarController};
