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,t)=>{let i={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)?(i.prompt,i.prompt_2=e.prompt):i.prompt=e.prompt,Array.isArray(e.negativePrompt)?(i.negative_prompt,i.negative_prompt_2=e.negativePrompt):i.negative_prompt=e.negativePrompt,e.classname){case"ImageEditorPromptNodeView":break;case"ImageEditorScribbleNodeView":i.control_images=[{image:e.src,process:!1,invert:!0,controlnet:"scribble"}];break;case"ImageEditorImageNodeView":i.fit=e.fit,i.anchor=e.anchor,!e.infer&&!e.inpaint&&(e.infer||e.inpaint||e.imagePrompt||e.control)||(i.image=e.src),e.infer&&(i.strength=e.strength),e.inpaint&&(i.mask=e.scribbleSrc,i.invert_mask=!0,i.crop_inpaint=e.cropInpaint,i.inpaint_feather=e.inpaintFeather),e.imagePromptPlus&&(i.ip_adapter_plus=!0,e.imagePromptFace&&(i.ip_adapter_face=!0)),e.imagePrompt&&(i.ip_adapter_images=[{image:e.src,scale:e.imagePromptScale,fit:e.fit,anchor:e.anchor}]),e.control&&(i.control_images=[{image:e.src,process:e.processControlImage,invert:!0===e.invertControlImage,controlnet:e.controlnet,scale:e.conditioningScale,fit:e.fit,anchor:e.anchor,start:e.conditioningStart,end:e.conditioningEnd}]);break;case"ImageEditorCompoundImageNodeView":let t;for(let a=0;a<e.children.length;a++){let n=e.children[a];(n.infer||n.inpaint)&&(isEmpty(t)?(t=a,i.image=n.src,i.anchor=n.anchor,i.fit=n.fit):messages.push("Node {i+1}: Base image set in image {imageNodeIndex+1}, ignoring additional set in {j+1}")),n.infer&&t==a&&(i.strength=n.strength),n.inpaint&&t==a&&(i.mask=n.scribbleSrc,i.invert_mask=!0,i.crop_inpaint=n.cropInpaint,i.inpaint_feather=n.inpaintFeather),n.imagePrompt&&(isEmpty(i.ip_adapter_images)&&(i.ip_adapter_images=[]),n.imagePromptPlus&&(i.ip_adapter_plus=!0,n.imagePromptFace&&(i.ip_adapter_face=!0)),i.ip_adapter_images.push({image:n.src,scale:n.imagePromptScale,fit:n.fit,anchor:n.anchor})),n.control&&(isEmpty(i.control_images)&&(i.control_images=[]),i.control_images.push({image:n.src,process:n.processControlImage,invert:"invert"==n.colorSpace,controlnet:n.controlnet,scale:n.conditioningScale,fit:n.fit,anchor:n.anchor}))}break;default:throw`Unknown classname ${e.classname}`}return i}))}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};
