import{isEmpty}from"../../base/helpers.mjs";import{Controller}from"../base.mjs";import{ScribbleView}from"../../view/scribble.mjs";import{ToolbarView}from"../../view/menu.mjs";import{InpaintingFormView}from"../../forms/enfugue/inpainting.mjs";class InpaintingController extends Controller{getState(i=!0){return{inpainting:{options:this.inpaintForm.values,mask:i?this.scribbleView.src:null}}}getDefaultState(){return{inpainting:{options:{outpaint:!0,inpaint:!1,croppedInpaint:!0,croppedInpaintFeather:32}}}}setState(i){if(!isEmpty(i.inpainting)){if(isEmpty(i.inpainting.options)||this.inpaintForm.setValues(i.inpainting.options).then((()=>this.inpaintForm.submit())),isEmpty(i.inpainting.mask))this.scribbleView.clearMemory();else{let e=new Image;e.onload=()=>{this.scribbleView.setMemory(e)},e.src=i.inpainting.mask}!isEmpty(i.inpainting.options)&&i.inpainting.options.inpaint?this.engine.mask=this.scribbleView.src:this.engine.mask=null}}async prepareMenu(i){let e=await i.addItem("Toggle Pencil Shape","fa-regular fa-square","q"),s=await i.addItem("Toggle Eraser","fa-solid fa-eraser","s"),t=await i.addItem("Clear Canvas","fa-solid fa-delete-left","l"),a=await i.addItem("Fill Canvas","fa-solid fa-fill-drip","v"),n=await i.addItem("Increase Pencil Size","fa-solid fa-plus","i"),r=await i.addItem("Decrease Pencil Size","fa-solid fa-minus","d"),l=await i.addItem("Toggle Mask Visibility","fa-solid fa-eye","y"),o=await i.addItem("Toggle Mask Locked/Unlocked","fa-solid fa-lock","k");e.onClick((()=>{"circle"===this.scribbleView.shape?(this.scribbleView.shape="square",e.setIcon("fa-regular fa-circle")):(this.scribbleView.shape="circle",e.setIcon("fa-regular fa-square"))})),s.onClick((()=>{this.scribbleView.isEraser?(this.scribbleView.isEraser=!1,s.setIcon("fa-solid fa-eraser")):(this.scribbleView.isEraser=!0,s.setIcon("fa-solid fa-pencil"))})),t.onClick((()=>{this.scribbleView.clearMemory()})),a.onClick((()=>{this.scribbleView.fillMemory()})),n.onClick((()=>{this.scribbleView.increaseSize()})),r.onClick((()=>{this.scribbleView.decreaseSize()})),l.onClick((()=>{this.scribbleView.hidden?(this.scribbleView.show(),l.setIcon("fa-solid fa-eye")):(this.scribbleView.hide(),l.setIcon("fa-solid fa-eye-slash"))})),o.onClick((()=>{this.scribbleView.hasClass("locked")?(this.scribbleView.removeClass("locked"),o.setIcon("fa-solid fa-lock")):(this.scribbleView.addClass("locked"),o.setIcon("fa-solid fa-unlock"))}))}resize(i=null,e=null){this.scribbleView.resizeCanvas(i||this.engine.width,e||this.engine.height)}async initialize(){let i;this.scribbleView=new ScribbleView(this.config,this.engine.width,this.engine.height),this.scribbleView.hide(),this.scribbleView.onDraw((()=>{clearTimeout(i),i=setTimeout((()=>{!1!==this.inpaintForm.values.inpaint&&(this.engine.mask=this.scribbleView.src)}),100)})),this.scribbleToolbar=new ToolbarView(this.config),this.scribbleToolbar.addClass("inpainting"),this.scribbleToolbar.hide(),await this.prepareMenu(this.scribbleToolbar),this.inpaintForm=new InpaintingFormView(this.config),this.inpaintForm.hide(),this.inpaintForm.onSubmit((i=>{i.inpaint?(this.publish("inpaintEnabled"),this.scribbleView.show(),this.scribbleToolbar.show(),this.engine.mask=this.scribbleView.src):(this.publish("inpaintDisabled"),this.scribbleView.hide(),this.scribbleToolbar.hide(),this.engine.mask=null),this.engine.outpaint=i.outpaint,this.engine.cropInpaint=i.cropInpaint,this.engine.inpaintFeather=i.inpaintFeather})),this.subscribe("engineWidthChange",(i=>this.resize(i))),this.subscribe("engineHeightChange",(i=>this.resize(null,i))),this.subscribe("layersChanged",(i=>{isEmpty(i)?(this.inpaintForm.hide(),this.scribbleView.hide(),this.scribbleToolbar.hide(),this.engine.mask=null):(this.inpaintForm.show(),this.inpaintForm.values.inpaint&&(this.engine.mask=this.scribbleView.src,this.scribbleView.show(),this.scribbleToolbar.show()))})),this.application.sidebar.addChild(this.inpaintForm),this.application.container.appendChild(await this.scribbleToolbar.render()),(await this.application.images.getNode()).find("enfugue-image-editor-overlay").append(await this.scribbleView.getNode())}}export{InpaintingController as SidebarController};
