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{getNodes(){let e=this.images.getState();return(Array.isArray(e)?e:e.nodes||[]).map((e=>{let t={x:e.x,y:e.y,w:e.w,h:e.h,inference_steps:e.inferenceSteps,guidance_scale:e.guidanceScale,scale_to_model_size:e.scaleToModelSize,remove_background:e.removeBackground};switch(Array.isArray(e.prompt)?(t.prompt,t.prompt_2=e.prompt):t.prompt=e.prompt,Array.isArray(e.negativePrompt)?(t.negative_prompt,t.negative_prompt_2=e.negativePrompt):t.negative_prompt=e.negativePrompt,e.classname){case"ImageEditorPromptNodeView":t.type="prompt",t.infer=!0;break;case"ImageEditorScribbleNodeView":t.type="scribble",t.image=e.src,t.control=!0,t.controlnet="scribble",t.invert=!0,t.process_control_image=!1;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.inpaint?e.scribbleSrc:null,t.strength=e.strength,t.conditioning_scale=e.conditioningScale,t.crop_inpaint=e.cropInpaint,t.inpaint_feather=e.inpaintFeather,t.inpaint_controlnet=e.inpaintControlnet,t.controlnet=e.controlnet,t.invert="invert"==e.colorSpace,t.invert_mask=!0,t.process_control_image=e.processControlImage;break;default:throw`Unknown classname ${e.classname}`}return t}))}async tryInvoke(){this.isInvoking=!0,this.loadingBar.loading(),this.invokeButton.disable().addClass("sliding-gradient");try{this.application.autosave(),await this.application.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(),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};
