import{isEmpty,filterEmpty}from"../base/helpers.mjs";import{ElementBuilder}from"../base/builder.mjs";import{NodeEditorView}from"./editor.mjs";import{ImageView}from"../view/image.mjs";import{ImageEditorNodeView}from"./image-editor/base.mjs";import{ImageEditorScribbleNodeView}from"./image-editor/scribble.mjs";import{ImageEditorPromptNodeView}from"./image-editor/prompt.mjs";import{ImageEditorImageNodeView}from"./image-editor/image.mjs";import{CurrentInvocationImageView}from"./image-editor/invocation.mjs";const E=new ElementBuilder;class ImageEditorView extends NodeEditorView{constructor(e){super(e.config,window.innerWidth-300,window.innerHeight-70),this.application=e,this.currentInvocation=new CurrentInvocationImageView(this),this.currentInvocation.hide()}static canvasWidth=512;static canvasHeight=512;static centered=!0;static className="image-editor";static maximumZoom=10;static nodeClasses=[ImageEditorScribbleNodeView,ImageEditorImageNodeView,ImageEditorPromptNodeView];async focusNode(e){if(super.focusNode(e),this.focusedNode=e,this.application.menu.removeCategory("Node"),!isEmpty(e.buttons)){let t=await this.application.menu.addCategory("Node","n");for(let i in e.buttons){let o=e.buttons[i];(await t.addItem(o.tooltip,o.icon,o.shortcut)).onClick((()=>o.callback.call(e)))}}}async removeNode(e){super.removeNode(e),this.focusedNode===e&&(this.focusedNode=null,this.application.menu.removeCategory("Node"))}hideCurrentInvocation(){this.currentInvocation.hide(),this.hasClass("has-image")&&(this.removeClass("has-image"),this.application.menu.removeCategory("Image")),this.resetDimension(!1)}resetDimension(e=!0){isEmpty(this.configuredWidth)||isEmpty(this.configuredHeight)||(this.setDimension(this.configuredWidth,this.configuredHeight,e),this.configuredHeight=null,this.configuredWidth=null)}async setCurrentInvocationImage(e){if(this.currentInvocation.setImage(e),await this.currentInvocation.waitForLoad(),this.currentInvocation.width==this.width&&this.currentInvocation.height==this.height||(isEmpty(this.configuredWidth)&&(this.configuredWidth=this.width),isEmpty(this.configuredHeight)&&(this.configuredHeight=this.height),this.setDimension(this.currentInvocation.width,this.currentInvocation.height,!1)),this.currentInvocation.show(),!this.hasClass("has-image")){this.addClass("has-image");let e=await this.application.menu.addCategory("Image","e");await this.currentInvocation.prepareMenu(e)}}getNextNodePoint(){let e=this.nodes.map((e=>e.left+ImageEditorNodeView.padding)),t=this.nodes.map((e=>e.top+ImageEditorNodeView.padding)),[i,o]=[0,0];for(;-1!==e.indexOf(i);)i+=ImageEditorNodeView.snapSize;for(;-1!==t.indexOf(o);)o+=ImageEditorNodeView.snapSize;return[i,o]}async addImageNode(e,t="Image"){let i=new ImageView(this.config,e),[o,a]=this.getNextNodePoint();return await i.waitForLoad(),await this.addNode(ImageEditorImageNodeView,t,i,o,a,i.width,i.height)}async addScribbleNode(e="Scribble"){let[t,i]=this.getNextNodePoint();return await this.addNode(ImageEditorScribbleNodeView,e,null,t,i,256,256)}async addPromptNode(e="Prompt"){let[t,i]=this.getNextNodePoint();return await this.addNode(ImageEditorPromptNodeView,e,null,t,i,256,256)}async build(){let e=await super.build(),t=E.createElement("enfugue-image-editor-grid");return e.find("enfugue-node-canvas").append(t,await this.currentInvocation.getNode()),e}getState(e=!0){let t=super.getState(e);return this.hasClass("has-image")&&e?{image:this.currentInvocation.src,nodes:t}:t}setState(e){return Array.isArray(e)?(this.hideCurrentInvocation(),super.setState(e)):(isEmpty(e.image)?this.hideCurrentInvocation():this.setCurrentInvocationImage(e.image),super.setState(e.nodes))}static getNodeDataForImage(e){let t={x:0,y:0,w:e.width,h:e.height,src:e.src,name:"Initial Image"};return{...ImageEditorImageNodeView.getDefaultState(),...t}}}export{ImageEditorView,ImageEditorNodeView,ImageEditorImageNodeView,ImageEditorScribbleNodeView,ImageEditorPromptNodeView};
