import{isEmpty}from"../../base/helpers.mjs";import{ImageView,BackgroundImageView}from"../../view/image.mjs";import{ImageEditorImageNodeOptionsFormView}from"../../forms/enfugue/image-editor.mjs";import{ImageEditorScribbleNodeView}from"./scribble.mjs";class ImageEditorImageNodeView extends ImageEditorScribbleNodeView{static nodeTypeName="Image";static allFitModes=["actual","stretch","cover","contain"];static allAnchorModes=["top-left","top-center","top-right","center-left","center-center","center-right","bottom-left","bottom-center","bottom-right"];static scribbleButtons=["erase","shape","clear","increase","decrease"];static className="image-editor-image-node-view";static nodeButtons={...ImageEditorScribbleNodeView.nodeButtons,"mirror-x":{icon:"fa-solid fa-left-right",tooltip:"Mirror Horizontally",shortcut:"h",callback:function(){this.mirrorHorizontally()}},"mirror-y":{icon:"fa-solid fa-up-down",tooltip:"Mirror Vertically",shortcut:"y",callback:function(){this.mirrorVertically()}},"rotate-clockwise":{icon:"fa-solid fa-rotate-right",tooltip:"Rotate Clockwise",shortcut:"r",callback:function(){this.rotateClockwise()}},"rotate-counter-clockwise":{icon:"fa-solid fa-rotate-left",tooltip:"Rotate Counter-Clockwise",shortcut:"w",callback:function(){this.rotateCounterClockwise()}}};static optionsFormView=ImageEditorImageNodeOptionsFormView;constructor(t,e,i,o,n,r,s){i instanceof ImageView&&(i=new BackgroundImageView(i.config,i.src)),super(t,e,null,o,n,r,s),this.scribbleView=this.content,this.content=i,this.scribbleView.hide()}async updateOptions(t){if(super.updateOptions(t),this.updateFit(t.fit),this.updateAnchor(t.anchor),this.infer=t.infer,this.control=t.control,this.inpaint=t.inpaint,this.strength=t.strength,this.controlnet=t.controlnet,this.conditioningScale=t.conditioningScale,this.processControlImage=t.processControlImage,this.cropInpaint=t.cropInpaint,this.inpaintFeather=t.inpaintFeather,void 0!==this.node){let t=this.node.find("enfugue-node-header");if(this.inpaint){this.scribbleView.show(),this.scribbleView.resizeCanvas(this.w,this.h);for(let e of this.constructor.scribbleButtons)t.find(`.node-button-${e}`).show()}else{this.scribbleView.hide();for(let e of this.constructor.scribbleButtons)t.find(`.node-button-${e}`).hide()}}}async updateFit(t){this.fit=t;let e=await this.getContent();e.fit=t;for(let t of this.constructor.allFitModes)e.removeClass(`fit-${t}`);isEmpty(t)||e.addClass(`fit-${t}`)}async updateAnchor(t){this.anchor=t;let e=await this.getContent();for(let t of this.constructor.allAnchorModes)e.removeClass(`anchor-${t}`);isEmpty(t)||e.addClass(`anchor-${t}`)}async build(){let t=await super.build();t.find("enfugue-node-contents").append(await this.scribbleView.getNode());let e=t.find("enfugue-node-header");for(let t of this.constructor.scribbleButtons)e.find(`.node-button-${t}`).hide();return t}mirrorHorizontally(){return this.content.mirrorHorizontally()}mirrorVertically(){return this.content.mirrorVertically()}rotateClockwise(){return this.content.rotateClockwise()}rotateCounterClockwise(){return this.content.rotateCounterClockwise()}getState(t=!0){let e=super.getState(t);return e.scribbleSrc=t?this.scribbleView.src:null,e.src=t?this.content.src:null,e.anchor=this.anchor||null,e.fit=this.fit||null,e.infer=this.infer||!1,e.control=this.control||!1,e.inpaint=this.inpaint||!1,e.strength=this.strength||.8,e.controlnet=this.controlnet||null,e.colorSpace=this.colorSpace||"invert",e.cropInpaint=!1!==this.cropInpaint,e.inpaintFeather=this.inpaintFeather||32,e.conditioningScale=this.conditioningScale||1,e.processControlImage=!1!==this.processControlImage,e.removeBackground=!0===this.removeBackground,e.scaleToModelSize=!0===this.scaleToModelSize,e}async setState(t){if(await this.setContent(new BackgroundImageView(this.config,t.src)),await this.updateAnchor(t.anchor),await this.updateFit(t.fit),t.inpaint){let e=new Image;e.onload=()=>{if(this.scribbleView.setMemory(e),this.scribbleView.resizeCanvas(this.w,this.h),this.scribbleView.show(),void 0!==this.node){let t=this.node.find("enfugue-node-header");for(let e of this.constructor.scribbleButtons)t.find(`.node-button-${e}`).show()}},e.src=t.scribbleSrc}else if(this.scribbleView.clearMemory(),this.scribbleView.hide(),void 0!==this.node){let t=this.node.find("enfugue-node-header");for(let e of this.constructor.scribbleButtons)t.find(`.node-button-${e}`).hide()}await super.setState({...t,src:null})}static getDefaultState(){return{classname:this.name,inpaint:!1,control:!1,inpaint:!1,cropInpaint:!0,inpaintFeather:32,inferenceSteps:null,guidanceScale:null,strength:.8,processControlImage:!0,colorSpace:"invert",conditioningScale:1,removeBackground:!1,scaleToModelSize:!1}}}export{ImageEditorImageNodeView};
