import{isEmpty,filterEmpty}from"../base/helpers.mjs";import{ElementBuilder}from"../base/builder.mjs";import{NodeEditorView}from"./editor.mjs";import{ImageView,BackgroundImageView}from"../view/image.mjs";import{VideoView}from"../view/video.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{ImageEditorVideoNodeView}from"./image-editor/video.mjs";import{NoImageView,NoVideoView}from"./image-editor/common.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 bringToFrontOnFocus=!1;static nodeClasses=[ImageEditorScribbleNodeView,ImageEditorImageNodeView,ImageEditorVideoNodeView];async focusNode(e){super.focusNode(e),this.focusedNode=e,this.application.menu.removeCategory("Element");let i=e.getButtons();if(!isEmpty(i)){let t=await this.application.menu.addCategory("Element","e");for(let o in i){let a=i[o];(await t.addItem(a.tooltip,a.icon,a.shortcut)).onClick((()=>a.callback.call(e)))}}}async removeNode(e){super.removeNode(e),this.focusedNode===e&&(this.focusedNode=null,this.application.menu.removeCategory("Element"))}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=null,[o,a]=this.getNextNodePoint();return t=e instanceof ImageView?e:isEmpty(e)?new NoImageView(this.config):new BackgroundImageView(this.config,e,!1),t instanceof ImageView&&await t.waitForLoad(),await this.addNode(ImageEditorImageNodeView,this.getUniqueNodeName(i),t,o,a,t.width,t.height)}async addVideoNode(e,i="Video"){let t=null,[o,a]=this.getNextNodePoint();return t=e instanceof VideoView?e:isEmpty(e)?new NoVideoView(this.config):new VideoView(this.config,e,!1),t instanceof VideoView&&await t.waitForLoad(),await this.addNode(ImageEditorVideoNodeView,this.getUniqueNodeName(i),t,o,a,t.width,t.height)}async addScribbleNode(e="Scribble"){let[i,t]=this.getNextNodePoint();return await this.addNode(ImageEditorScribbleNodeView,this.getUniqueNodeName(e),null,i,t,256,256)}async build(){let e=await super.build(),i=E.createElement("enfugue-image-editor-overlay"),t=E.createElement("enfugue-image-editor-grid");return e.find("enfugue-node-canvas").append(i.content(t)),e}static getNodeDataForImage(e){let i={x:0,y:0,w:e.width,h:e.height,src:e.src,name:"Initial Image"};return{...ImageEditorImageNodeView.getDefaultState(),...i}}}export{ImageEditorView,ImageEditorNodeView,ImageEditorImageNodeView,ImageEditorScribbleNodeView};
