import{isEmpty}from"../../base/helpers.mjs";import{ElementBuilder}from"../../base/builder.mjs";import{ButtonInputView}from"../../view/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{getNodes(){return this.images.getState().map((e=>{let t={x:e.x,y:e.y,w:e.w,h:e.h};switch(e.classname){case"ImageEditorPromptNodeView":t.type="prompt",t.infer=!0,t.prompt=e.prompt,t.negative_prompt=e.negativePrompt,t.guidance_scale=e.guidanceScale,t.inference_steps=e.inferenceSteps,t.remove_background=e.removeBackground;break;case"ImageEditorScribbleNodeView":t.type="scribble",t.image=e.src,t.control=!0,t.controlnet="scribble",t.prompt=e.prompt,t.negative_prompt=e.negativePrompt,t.guidance_scale=e.guidanceScale,t.inference_steps=e.inferenceSteps,t.remove_background=e.removeBackground;break;case"ImageEditorImageNodeView":t.type="image",t.fit=e.fit,t.anchor=e.anchor,t.infer=e.infer,t.control=e.control,t.inpaint=e.inpaint,t.image=e.src,t.mask=e.scribbleSrc,t.strength=e.strength,t.conditioning_scale=e.conditioningScale,t.controlnet=e.controlnet,t.prompt=e.prompt,t.negative_prompt=e.negativePrompt,t.guidance_scale=e.guidanceScale,t.inference_steps=e.inferenceSteps,t.remove_background=e.removeBackground,t.process_control_image=e.processControlImage;break;default:throw`Unknown classname ${e.classname}`}return t}))}async tryInvoke(){this.loadingBar.loading(),this.invokeButton.disable().addClass("sliding-gradient");try{this.application.autosave(),await this.engine.invoke({nodes:this.getNodes()})}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()}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)}}export{InvokeButtonController as SidebarController};
