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 RealTimeButton extends ButtonInputView{static className="real-time";static defaultValue=""}class InvokeButtonController extends Controller{get realTime(){let e=this.application.session.getItem("realTime");return!isEmpty(e)&&e}set realTime(e){this.application.session.setItem("realTime",e)}async getLayers(){let e=this.application.layers.getState(),t=[],i=e.layers.map(((e,i)=>{let a={x:e.x,y:e.y,w:e.w,h:e.h,remove_background:e.removeBackground,image:e.src};switch(e.classname){case"ImageEditorScribbleNodeView":a.control_units=[{process:!1,controlnet:"scribble",start:e.conditioningStart,end:e.conditioningEnd,scale:e.conditioningScale}];break;case"ImageEditorImageNodeView":case"ImageEditorVideoNodeView":a.fit=e.fit,a.anchor=e.anchor,a.offset_x=e.offsetX,a.offset_y=e.offsetY,a.opacity=e.opacity,a.visibility=e.visibility,a.frame=isEmpty(e.startFrame)?0:e.startFrame-1,e.imagePrompt&&(a.ip_adapter_scale=e.imagePromptScale,a.face_only=e.faceOnly),e.control&&(a.control_units=e.controlnetUnits.map((e=>({process:e.processControlImage,start:e.conditioningStart,end:e.conditioningEnd,scale:e.conditioningScale,controlnet:e.controlnet})))),isEmpty(e.skipFrames)||(a.skip_frames=e.skipFrames),isEmpty(e.divideFrames)||(a.divide_frames=e.divideFrames),-1===["visible","denoised"].indexOf(e.visibility)&&isEmpty(a.ip_adapter_scale)&&isEmpty(a.control_units)&&t.push(i);break;default:throw`Unknown classname ${e.classname}`}return a}));if(!isEmpty(t)){let e=1===t.length?"":"s",a=1===t.length?"s":"ve",n=1===t.length?"this":"these",s=(t.length,1===t.length?"it":"them");if(!await this.confirm(`${t.length} layer${e} ha${a} no role assigned, ${n} layer${e} will not be not sent to the backend. Add a role to ${n} layer${e} to use ${s}, like selecting "Visible" or "Denoised" visibility mode, using it with IP Adapter, and/or assigning one or more control units.<br /><br />Continue anyway?`))throw"Invocation canceled.";return i.filter(((e,i)=>-1===t.indexOf(i)))}return i}async tryInvoke(){this.isInvoking=!0,this.loadingBar.loading(),this.invokeButton.disable().addClass("sliding-gradient");try{this.application.autosave(),await this.application.invoke({layers:await this.getLayers()})}catch(e){console.error(e);let t=`${e}`;if(isEmpty(e.detail)?isEmpty(e.title)||(t=e.title):t=e.detail,-1!==t.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",t)}this.invokeButton.enable().removeClass("sliding-gradient"),this.loadingBar.doneLoading(),this.application.autosave(),this.isInvoking=!1,this.invokeAgain&&(this.invokeAgain=!1,await this.tryInvoke())}async initialize(){this.realTimeButton=new RealTimeButton(this.config),this.realTimeButton.onChange((()=>{this.realTime=!this.realTimeButton.hasClass("active"),this.engine.realTime=this.realTime,this.realTimeButton.toggleClass("active")})),this.realTime&&(this.realTimeButton.addClass("active"),setTimeout((()=>{this.engine.realTime=!0}),1e3)),this.invokeButton=new EnfugueButton(this.config),this.invokeButton.onChange((()=>this.tryInvoke())),this.loadingBar=new InvokeLoadingBarView,await this.application.sidebar.addChild(this.realTimeButton),await this.application.sidebar.addChild(this.invokeButton),await this.application.sidebar.addChild(this.loadingBar),this.subscribe("tryInvoke",(()=>{!0!==this.isInvoking?this.tryInvoke():this.realTime&&(this.invokeAgain=!0)}))}}export{InvokeButtonController as SidebarController};
