import{isEmpty,filterEmpty}from"../../base/helpers.mjs";import{ElementBuilder}from"../../base/builder.mjs";import{NodeEditorView}from"./editor.mjs";import{ImageView}from"../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}static canvasWidth=512;static canvasHeight=512;static centered=!0;static className="image-editor";static maximumZoom=10;static nodeClasses=[ImageEditorScribbleNodeView,ImageEditorImageNodeView,ImageEditorPromptNodeView];hideCurrentInvocation(){this.currentInvocation.hide(),this.removeClass("has-image"),isEmpty(this.configuredWidth)||isEmpty(this.configuredHeight)||(this.width=this.configuredWidth,this.height=this.configuredHeight,this.configuredHeight=null,this.configuredWidth=null)}async setCurrentInvocationImage(e){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.width=this.currentInvocation.width,this.height=this.currentInvocation.height),this.currentInvocation.show(),this.addClass("has-image")}getNextNodePoint(){let e=this.nodes.map((e=>e.left+ImageEditorNodeView.padding)),i=this.nodes.map((e=>e.top+ImageEditorNodeView.padding)),[t,o]=[0,0];for(;-1!==e.indexOf(t);)t+=ImageEditorNodeView.snapSize;for(;-1!==i.indexOf(o);)o+=ImageEditorNodeView.snapSize;return[t,o]}async addImageNode(e,i="Image"){let t=new ImageView(this.config,e),[o,r]=this.getNextNodePoint();return await t.waitForLoad(),await this.addNode(ImageEditorImageNodeView,i,t,o,r,t.width,t.height)}async addScribbleNode(e="Scribble"){let[i,t]=this.getNextNodePoint();return await this.addNode(ImageEditorScribbleNodeView,e,null,i,t,256,256)}async addPromptNode(e="Prompt"){let[i,t]=this.getNextNodePoint();return await this.addNode(ImageEditorPromptNodeView,e,null,i,t,256,256)}async build(){let e=await super.build(),i=E.createElement("enfugue-image-editor-grid");return this.currentInvocation=new CurrentInvocationImageView(this),this.currentInvocation.hide(),e.find("enfugue-node-canvas").append(i,await this.currentInvocation.getNode()),e}static getNodeDataForImage(e){return{...{x:0,y:0,w:e.width,h:e.height,src:e.src,name:"Initial Image"},...ImageEditorImageNodeView.getDefaultState()}}}export{ImageEditorView,ImageEditorNodeView,ImageEditorImageNodeView,ImageEditorScribbleNodeView,ImageEditorPromptNodeView};
