@html-graph/html-graph 8.19.0 → 8.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- (function(A,T){typeof exports=="object"&&typeof module<"u"?T(exports):typeof define=="function"&&define.amd?define(["exports"],T):(A=typeof globalThis<"u"?globalThis:A||self,T(A.HtmlGraph={}))})(this,function(A){"use strict";var Zr=Object.defineProperty;var _r=(A,T,U)=>T in A?Zr(A,T,{enumerable:!0,configurable:!0,writable:!0,value:U}):A[T]=U;var i=(A,T,U)=>_r(A,typeof T!="symbol"?T+"":T,U);var T=(r=>(r.Line="line",r.NodeCycle="node-cycle",r.PortCycle="port-cycle",r))(T||{});const U=()=>{const r=document.createElement("div");return r.style.width="100%",r.style.height="100%",r.style.position="relative",r.style.overflow="hidden",r},pt=()=>{const r=document.createElement("div");return r.style.position="absolute",r.style.top="0",r.style.left="0",r.style.width="0",r.style.height="0",r},wt=r=>{r.style.position="absolute",r.style.top="0",r.style.left="0",r.style.visibility="hidden"},ft=r=>{r.style.removeProperty("position"),r.style.removeProperty("top"),r.style.removeProperty("left"),r.style.removeProperty("visibility"),r.style.removeProperty("transform")};class Se{constructor(e,t,o){i(this,"host",U());i(this,"container",pt());i(this,"edgeIdToElementMap",new Map);i(this,"attachedNodeIds",new Set);i(this,"applyTransform",()=>{const e=this.viewportStore.getContentMatrix();this.container.style.transform=`matrix(${e.scale}, 0, 0, ${e.scale}, ${e.x}, ${e.y})`});this.graphStore=e,this.viewportStore=t,this.element=o,this.element.appendChild(this.host),this.host.appendChild(this.container),this.viewportStore.onAfterUpdated.subscribe(this.applyTransform)}attachNode(e){const t=this.graphStore.getNode(e);wt(t.element),this.attachedNodeIds.add(e),this.container.appendChild(t.element),this.updateNodePosition(e),this.updateNodePriority(e),t.element.style.visibility="visible"}detachNode(e){const t=this.graphStore.getNode(e);ft(t.element),this.container.removeChild(t.element),this.attachedNodeIds.delete(e)}attachEdge(e){const t=this.graphStore.getEdge(e).payload.shape.svg;this.edgeIdToElementMap.set(e,t),this.container.appendChild(t),this.renderEdge(e),this.updateEdgePriority(e)}detachEdge(e){const t=this.edgeIdToElementMap.get(e);this.container.removeChild(t),this.edgeIdToElementMap.delete(e)}clear(){this.edgeIdToElementMap.forEach((e,t)=>{this.detachEdge(t)}),this.attachedNodeIds.forEach(e=>{this.detachNode(e)})}destroy(){this.clear(),this.viewportStore.onAfterUpdated.unsubscribe(this.applyTransform),this.element.removeChild(this.host),this.host.removeChild(this.container)}updateNodePosition(e){const t=this.graphStore.getNode(e),{width:o,height:s}=t.element.getBoundingClientRect(),n=this.viewportStore.getViewportMatrix().scale,{payload:a}=t,h=a.centerFn(o,s),d=a.x-n*h.x,c=a.y-n*h.y;t.element.style.transform=`translate(${d}px, ${c}px)`}updateNodePriority(e){const t=this.graphStore.getNode(e);t.element.style.zIndex=`${t.payload.priority}`}updateEdgeShape(e){const t=this.edgeIdToElementMap.get(e);this.container.removeChild(t);const s=this.graphStore.getEdge(e).payload.shape.svg;this.edgeIdToElementMap.set(e,s),this.container.appendChild(s)}renderEdge(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from),s=this.graphStore.getPort(t.to),n=o.element.getBoundingClientRect(),a=s.element.getBoundingClientRect(),h=this.host.getBoundingClientRect(),d=this.viewportStore.getViewportMatrix().scale,c=this.createEdgeRenderPort(o,n,h,d),l=this.createEdgeRenderPort(s,a,h,d);let u=T.Line;o.element===s.element?u=T.PortCycle:o.nodeId===s.nodeId&&(u=T.NodeCycle),t.payload.shape.render({from:c,to:l,category:u})}updateEdgePriority(e){const t=this.graphStore.getEdge(e);t.payload.shape.svg.style.zIndex=`${t.payload.priority}`}createEdgeRenderPort(e,t,o,s){const n=this.viewportStore.createContentCoords({x:t.left-o.left,y:t.top-o.top});return{x:n.x,y:n.y,width:t.width*s,height:t.height*s,direction:e.payload.direction}}}class yt{constructor(e){i(this,"xFrom",1/0);i(this,"yFrom",1/0);i(this,"xTo",1/0);i(this,"yTo",1/0);this.graphStore=e}setRenderingBox(e){this.xFrom=e.x,this.xTo=e.x+e.width,this.yFrom=e.y,this.yTo=e.y+e.height}hasNode(e){const t=this.graphStore.getNode(e).payload,{x:o,y:s}=t;return o>=this.xFrom&&o<=this.xTo&&s>=this.yFrom&&s<=this.yTo}hasEdge(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from).nodeId,s=this.graphStore.getPort(t.to).nodeId,n=this.graphStore.getNode(o).payload,a=this.graphStore.getNode(s).payload,h=Math.min(n.x,a.x),d=Math.max(n.x,a.x),c=Math.min(n.y,a.y),l=Math.max(n.y,a.y);return h<=this.xTo&&d>=this.xFrom&&c<=this.yTo&&l>=this.yFrom}}class vt{constructor(e,t,o,s){i(this,"attachedNodes",new Set);i(this,"attachedEdges",new Set);i(this,"renderingBox");i(this,"updateViewport",e=>{this.renderingBox.setRenderingBox(e);const t=new Set,o=new Set,s=new Set,n=new Set;this.graphStore.getAllNodeIds().forEach(a=>{const h=this.renderingBox.hasNode(a),d=this.attachedNodes.has(a);h&&!d?t.add(a):!h&&d&&o.add(a)}),this.graphStore.getAllEdgeIds().forEach(a=>{const h=this.renderingBox.hasEdge(a),d=this.attachedEdges.has(a),c=this.graphStore.getEdge(a),l=this.graphStore.getPort(c.from).nodeId,u=this.graphStore.getPort(c.to).nodeId;h&&(this.renderingBox.hasNode(l)||(t.add(l),o.delete(l)),this.renderingBox.hasNode(u)||(t.add(u),o.delete(u))),h&&!d?s.add(a):!h&&d&&n.add(a)}),n.forEach(a=>{this.handleDetachEdge(a)}),o.forEach(a=>{this.handleDetachNode(a)}),t.forEach(a=>{this.attachedNodes.has(a)||this.handleAttachNode(a)}),s.forEach(a=>{this.handleAttachEdge(a)})});this.htmlView=e,this.graphStore=t,this.trigger=o,this.params=s,this.renderingBox=new yt(this.graphStore),this.trigger.subscribe(this.updateViewport)}attachNode(e){this.renderingBox.hasNode(e)&&this.handleAttachNode(e)}detachNode(e){this.attachedNodes.has(e)&&this.handleDetachNode(e)}attachEdge(e){this.renderingBox.hasEdge(e)&&this.attachEdgeEntities(e)}detachEdge(e){this.attachedEdges.has(e)&&this.handleDetachEdge(e)}updateNodePosition(e){this.attachedNodes.has(e)?this.htmlView.updateNodePosition(e):this.renderingBox.hasNode(e)&&(this.handleAttachNode(e),this.graphStore.getNodeAdjacentEdgeIds(e).forEach(t=>{this.attachEdgeEntities(t)}))}updateNodePriority(e){this.attachedNodes.has(e)&&this.htmlView.updateNodePriority(e)}updateEdgeShape(e){this.attachedEdges.has(e)&&this.htmlView.updateEdgeShape(e)}renderEdge(e){this.attachedEdges.has(e)&&this.htmlView.renderEdge(e)}updateEdgePriority(e){this.attachedEdges.has(e)&&this.htmlView.updateEdgePriority(e)}clear(){this.htmlView.clear(),this.attachedNodes.clear(),this.attachedEdges.clear()}destroy(){this.clear(),this.htmlView.destroy(),this.trigger.unsubscribe(this.updateViewport)}attachEdgeEntities(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from).nodeId,s=this.graphStore.getPort(t.to).nodeId;this.attachedNodes.has(o)||this.handleAttachNode(o),this.attachedNodes.has(s)||this.handleAttachNode(s),this.handleAttachEdge(e)}handleAttachNode(e){this.params.onBeforeNodeAttached(e),this.attachedNodes.add(e),this.htmlView.attachNode(e)}handleDetachNode(e){this.htmlView.detachNode(e),this.attachedNodes.delete(e),this.params.onAfterNodeDetached(e)}handleAttachEdge(e){this.attachedEdges.add(e),this.htmlView.attachEdge(e)}handleDetachEdge(e){this.htmlView.detachEdge(e),this.attachedEdges.delete(e)}}class mt{constructor(e,t){i(this,"deferredNodes",new Set);i(this,"deferredEdges",new Set);this.htmlView=e,this.graphStore=t}attachNode(e){this.isNodeValid(e)?this.htmlView.attachNode(e):this.deferredNodes.add(e)}detachNode(e){this.deferredNodes.has(e)?this.deferredNodes.delete(e):this.htmlView.detachNode(e)}attachEdge(e){this.isEdgeValid(e)?this.htmlView.attachEdge(e):this.deferredEdges.add(e)}detachEdge(e){this.deferredEdges.has(e)?this.deferredEdges.delete(e):this.htmlView.detachEdge(e)}updateNodePosition(e){this.deferredNodes.has(e)?this.tryAttachNode(e):this.htmlView.updateNodePosition(e)}updateNodePriority(e){this.deferredNodes.has(e)?this.tryAttachNode(e):this.htmlView.updateNodePriority(e)}updateEdgeShape(e){this.deferredEdges.has(e)?this.tryAttachEdge(e):this.htmlView.updateEdgeShape(e)}renderEdge(e){this.deferredEdges.has(e)?this.tryAttachEdge(e):this.htmlView.renderEdge(e)}updateEdgePriority(e){this.deferredEdges.has(e)?this.tryAttachEdge(e):this.htmlView.updateEdgePriority(e)}clear(){this.deferredNodes.clear(),this.deferredEdges.clear(),this.htmlView.clear()}destroy(){this.htmlView.destroy()}isNodeValid(e){const t=this.graphStore.getNode(e);return!(t.payload.x===null||t.payload.y===null)}tryAttachNode(e){this.isNodeValid(e)&&(this.deferredNodes.delete(e),this.htmlView.attachNode(e))}isEdgeValid(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from),s=this.graphStore.getPort(t.to);return!(this.deferredNodes.has(o.nodeId)||this.deferredNodes.has(s.nodeId))}tryAttachEdge(e){this.isEdgeValid(e)&&(this.deferredEdges.delete(e),this.htmlView.attachEdge(e))}}class k{constructor(){i(this,"callbacks",new Set)}subscribe(e){this.callbacks.add(e)}unsubscribe(e){this.callbacks.delete(e)}emit(e){this.callbacks.forEach(t=>{t(e)})}}const M=()=>{const r=new k;return[r,r]};class be{constructor(e,t,o,s){i(this,"beforeDestroyEmitter");i(this,"destroyed",!1);i(this,"onBeforeDestroy");this.graph=e,this.viewport=t,this.graphController=o,this.viewportController=s,[this.beforeDestroyEmitter,this.onBeforeDestroy]=M()}addNode(e){return this.graphController.addNode(e),this}updateNode(e,t){return this.graphController.updateNode(e,t),this}removeNode(e){return this.graphController.removeNode(e),this}markPort(e){return this.graphController.markPort(e),this}updatePort(e,t){return this.graphController.updatePort(e,t),this}unmarkPort(e){return this.graphController.unmarkPort(e),this}addEdge(e){return this.graphController.addEdge(e),this}updateEdge(e,t){return this.graphController.updateEdge(e,t),this}removeEdge(e){return this.graphController.removeEdge(e),this}clear(){return this.graphController.clear(),this}focus(e){return this.viewportController.focus(e),this}center(e,t){return this.viewportController.center(e,t),this}patchViewportMatrix(e){return this.viewportController.patchViewportMatrix(e),this}patchContentMatrix(e){return this.viewportController.patchContentMatrix(e),this}destroy(){this.destroyed||(this.destroyed=!0,this.beforeDestroyEmitter.emit(),this.graphController.destroy(),this.viewportController.destroy())}}class Et{constructor(){i(this,"singleToMultiMap",new Map);i(this,"multiToSingleMap",new Map)}addRecord(e,t){const o=this.singleToMultiMap.get(e);o===void 0?this.singleToMultiMap.set(e,new Set([t])):o.add(t),this.multiToSingleMap.set(t,e)}getMultiBySingle(e){const t=this.singleToMultiMap.get(e)??new Set;return Array.from(t.values())}removeByMulti(e){const t=this.multiToSingleMap.get(e),o=this.singleToMultiMap.get(t);o.delete(e),o.size===0&&this.singleToMultiMap.delete(t),this.multiToSingleMap.delete(e)}getByMulti(e){return this.multiToSingleMap.get(e)}removeBySingle(e){this.singleToMultiMap.get(e).forEach(o=>{this.multiToSingleMap.delete(o)}),this.singleToMultiMap.delete(e)}clear(){this.singleToMultiMap.clear(),this.multiToSingleMap.clear()}forEachSingle(e){this.singleToMultiMap.forEach((t,o)=>{e(o)})}hasSingle(e){return this.singleToMultiMap.get(e)!==void 0}hasMulti(e){return this.multiToSingleMap.get(e)!==void 0}}class S extends Error{constructor(){super(...arguments);i(this,"name","CanvasError")}}const b=Object.freeze({accessNonexistingNode:r=>`Failed to access node with ID ${JSON.stringify(r)} because it does not exist`,addNodeWithExistingId:r=>`Failed to add node with ID ${JSON.stringify(r)} because a node with this ID already exists`,addNodeWithElementInUse:(r,e)=>`Failed to add node with ID ${JSON.stringify(r)} because its HTML element is already attached to node with ID ${JSON.stringify(e)}`,updateNonexistentNode:r=>`Failed to update node with ID ${JSON.stringify(r)} because it does not exist`,removeNonexistentNode:r=>`Failed to remove node with ID ${JSON.stringify(r)} because it does not exist`,accessNonexistentPort:r=>`Failed to access port with ID ${JSON.stringify(r)} because it does not exist`,addPortWithExistingId:r=>`Failed to add port with ID ${JSON.stringify(r)} because a port with this ID already exists`,addPortToNonexistentNode:(r,e)=>`Failed to add port with ID ${JSON.stringify(r)} to node with ID ${JSON.stringify(e)} because the node does not exist`,updateNonexistentPort:r=>`Failed to update port with ID ${JSON.stringify(r)} because it does not exist`,accessPortsOfNonexistentNode:r=>`Failed to access ports of node with ID ${JSON.stringify(r)} because the node does not exist`,removeNonexistentPort:r=>`Failed to remove port with ID ${JSON.stringify(r)} because it does not exist`,accessNonexistentEdge:r=>`Failed to access edge with ID ${JSON.stringify(r)} because it does not exist`,addEdgeWithExistingId:r=>`Failed to add edge with ID ${JSON.stringify(r)} because an edge with this ID already exists`,addEdgeWithElementInUse:(r,e)=>`Failed to add edge with ID ${JSON.stringify(r)} because its element is already attached to edge with ID ${JSON.stringify(e)}`,addEdgeFromNonexistentPort:(r,e)=>`Failed to add edge with ID ${JSON.stringify(r)} from port with ID ${JSON.stringify(e)} because the port does not exist`,addEdgeToNonexistentPort:(r,e)=>`Failed to add edge with ID ${JSON.stringify(r)} to port with ID ${JSON.stringify(e)} because the port does not exist`,updateNonexistentEdge:r=>`Failed to update edge with ID ${JSON.stringify(r)} because it does not exist`,updateNonexistentEdgeSource:(r,e)=>`Failed to update source of edge with ID ${JSON.stringify(r)} because source port with ID ${JSON.stringify(e)} does not exist`,updateNonexistentEdgeTarget:(r,e)=>`Failed to update target of edge with ID ${JSON.stringify(r)} because target port with ID ${JSON.stringify(e)} does not exist`,removeNonexistentEdge:r=>`Failed to remove edge with ID ${JSON.stringify(r)} because it does not exist`,accessEdgesForNonexistentPort:r=>`Failed to access edges for port with ID ${JSON.stringify(r)} because the port does not exist`});class Pe{constructor(){i(this,"nodes",new Map);i(this,"ports",new Map);i(this,"edges",new Map);i(this,"nodesElementsMap",new Map);i(this,"portIncomingEdges",new Map);i(this,"portOutgoingEdges",new Map);i(this,"portCycleEdges",new Map);i(this,"elementPorts",new Et);i(this,"edgesElementsMap",new Map);i(this,"afterNodeAddedEmitter");i(this,"onAfterNodeAdded");i(this,"afterNodeUpdatedEmitter");i(this,"onAfterNodeUpdated");i(this,"afterNodePriorityUpdatedEmitter");i(this,"onAfterNodePriorityUpdated");i(this,"beforeNodeRemovedEmitter");i(this,"onBeforeNodeRemoved");i(this,"afterPortAddedEmitter");i(this,"onAfterPortAdded");i(this,"afterPortUpdatedEmitter");i(this,"onAfterPortUpdated");i(this,"beforePortRemovedEmitter");i(this,"onBeforePortRemoved");i(this,"afterEdgeAddedEmitter");i(this,"onAfterEdgeAdded");i(this,"afterEdgeShapeUpdatedEmitter");i(this,"onAfterEdgeShapeUpdated");i(this,"afterEdgeUpdatedEmitter");i(this,"onAfterEdgeUpdated");i(this,"afterEdgePriorityUpdatedEmitter");i(this,"onAfterEdgePriorityUpdated");i(this,"beforeEdgeRemovedEmitter");i(this,"onBeforeEdgeRemoved");i(this,"beforeClearEmitter");i(this,"onBeforeClear");[this.afterNodeAddedEmitter,this.onAfterNodeAdded]=M(),[this.afterNodeUpdatedEmitter,this.onAfterNodeUpdated]=M(),[this.afterNodePriorityUpdatedEmitter,this.onAfterNodePriorityUpdated]=M(),[this.beforeNodeRemovedEmitter,this.onBeforeNodeRemoved]=M(),[this.afterPortAddedEmitter,this.onAfterPortAdded]=M(),[this.afterPortUpdatedEmitter,this.onAfterPortUpdated]=M(),[this.beforePortRemovedEmitter,this.onBeforePortRemoved]=M(),[this.afterEdgeAddedEmitter,this.onAfterEdgeAdded]=M(),[this.afterEdgeShapeUpdatedEmitter,this.onAfterEdgeShapeUpdated]=M(),[this.afterEdgeUpdatedEmitter,this.onAfterEdgeUpdated]=M(),[this.afterEdgePriorityUpdatedEmitter,this.onAfterEdgePriorityUpdated]=M(),[this.beforeEdgeRemovedEmitter,this.onBeforeEdgeRemoved]=M(),[this.beforeClearEmitter,this.onBeforeClear]=M()}hasNode(e){return this.nodes.has(e)}getNode(e){const t=this.nodes.get(e);if(t===void 0)throw new S(b.accessNonexistingNode(e));return t}addNode(e){if(this.hasNode(e.id))throw new S(b.addNodeWithExistingId(e.id));const t=this.findNodeIdByElement(e.element);if(t!==void 0)throw new S(b.addNodeWithElementInUse(e.id,t));const o=new Map,s={element:e.element,payload:{x:e.x,y:e.y,centerFn:e.centerFn,priority:e.priority},ports:o};this.nodes.set(e.id,s),this.nodesElementsMap.set(e.element,e.id),this.afterNodeAddedEmitter.emit(e.id)}getAllNodeIds(){return Array.from(this.nodes.keys())}findNodeIdByElement(e){return this.nodesElementsMap.get(e)}updateNode(e,t){if(!this.hasNode(e))throw new S(b.updateNonexistentNode(e));const{payload:o}=this.nodes.get(e);o.x=t.x??o.x,o.y=t.y??o.y,o.centerFn=t.centerFn??o.centerFn,t.priority!==void 0&&(o.priority=t.priority,this.afterNodePriorityUpdatedEmitter.emit(e)),this.afterNodeUpdatedEmitter.emit(e)}removeNode(e){if(!this.hasNode(e))throw new S(b.removeNonexistentNode(e));this.beforeNodeRemovedEmitter.emit(e);const t=this.getNode(e);this.nodesElementsMap.delete(t.element),this.nodes.delete(e)}hasPort(e){return this.ports.has(e)}getPort(e){const t=this.ports.get(e);if(t===void 0)throw new S(b.accessNonexistentPort(e));return t}addPort(e){if(this.hasPort(e.id))throw new S(b.addPortWithExistingId(e.id));if(!this.hasNode(e.nodeId))throw new S(b.addPortToNonexistentNode(e.id,e.nodeId));this.ports.set(e.id,{element:e.element,payload:{direction:e.direction},nodeId:e.nodeId}),this.elementPorts.addRecord(e.element,e.id),this.portCycleEdges.set(e.id,new Set),this.portIncomingEdges.set(e.id,new Set),this.portOutgoingEdges.set(e.id,new Set),this.getNode(e.nodeId).ports.set(e.id,e.element),this.afterPortAddedEmitter.emit(e.id)}updatePort(e,t){if(!this.hasPort(e))throw new S(b.updateNonexistentPort(e));const o=this.getPort(e).payload;o.direction=t.direction??o.direction,this.afterPortUpdatedEmitter.emit(e)}getAllPortIds(){return Array.from(this.ports.keys())}findPortIdsByElement(e){return this.elementPorts.getMultiBySingle(e)}getNodePortIds(e){const t=this.nodes.get(e);if(t===void 0)throw new S(b.accessPortsOfNonexistentNode(e));return Array.from(t.ports.keys())}removePort(e){if(!this.hasPort(e))throw new S(b.removeNonexistentPort(e));const t=this.getPort(e).nodeId;this.beforePortRemovedEmitter.emit(e),this.getNode(t).ports.delete(e),this.ports.delete(e),this.elementPorts.removeByMulti(e)}hasEdge(e){return this.edges.has(e)}getEdge(e){const t=this.edges.get(e);if(t===void 0)throw new S(b.accessNonexistentEdge(e));return t}findEdgeIdByElement(e){return this.edgesElementsMap.get(e)}addEdge(e){if(this.hasEdge(e.id))throw new S(b.addEdgeWithExistingId(e.id));if(!this.hasPort(e.from))throw new S(b.addEdgeFromNonexistentPort(e.id,e.from));if(!this.hasPort(e.to))throw new S(b.addEdgeToNonexistentPort(e.id,e.to));const t=this.findEdgeIdByElement(e.shape.svg);if(t!==void 0)throw new S(b.addEdgeWithElementInUse(e.id,t));this.addEdgeInternal(e),this.afterEdgeAddedEmitter.emit(e.id)}updateEdge(e,t){if(!this.hasEdge(e))throw new S(b.updateNonexistentEdge(e));if(t.from!==void 0||t.to!==void 0){if(t.from!==void 0&&!this.hasPort(t.from))throw new S(b.updateNonexistentEdgeSource(e,t.from));if(t.to!==void 0&&!this.hasPort(t.to))throw new S(b.updateNonexistentEdgeTarget(e,t.to));const s=this.getEdge(e),n=s.payload;this.removeEdgeInternal(e),this.addEdgeInternal({id:e,from:t.from??s.from,to:t.to??s.to,shape:n.shape,priority:n.priority})}const o=this.edges.get(e);t.shape!==void 0&&(o.payload.shape=t.shape,this.afterEdgeShapeUpdatedEmitter.emit(e)),t.priority!==void 0&&(o.payload.priority=t.priority,this.afterEdgePriorityUpdatedEmitter.emit(e)),this.afterEdgeUpdatedEmitter.emit(e)}getAllEdgeIds(){return Array.from(this.edges.keys())}removeEdge(e){if(!this.hasEdge(e))throw new S(b.removeNonexistentEdge(e));this.beforeEdgeRemovedEmitter.emit(e),this.removeEdgeInternal(e)}clear(){this.beforeClearEmitter.emit(),this.portIncomingEdges.clear(),this.portOutgoingEdges.clear(),this.portCycleEdges.clear(),this.elementPorts.clear(),this.nodesElementsMap.clear(),this.edgesElementsMap.clear(),this.edges.clear(),this.ports.clear(),this.nodes.clear()}getPortIncomingEdgeIds(e){const t=this.portIncomingEdges.get(e);if(t===void 0)throw new S(b.accessEdgesForNonexistentPort(e));return Array.from(t)}getPortOutgoingEdgeIds(e){const t=this.portOutgoingEdges.get(e);if(t===void 0)throw new S(b.accessEdgesForNonexistentPort(e));return Array.from(t)}getPortCycleEdgeIds(e){const t=this.portCycleEdges.get(e);if(t===void 0)throw new S(b.accessEdgesForNonexistentPort(e));return Array.from(t)}getPortAdjacentEdgeIds(e){return[...this.getPortIncomingEdgeIds(e),...this.getPortOutgoingEdgeIds(e),...this.getPortCycleEdgeIds(e)]}getNodeIncomingEdgeIds(e){const t=Array.from(this.getNode(e).ports.keys()),o=[];return t.forEach(s=>{this.getPortIncomingEdgeIds(s).filter(n=>{const a=this.getEdge(n);return this.getPort(a.from).nodeId!==e}).forEach(n=>{o.push(n)})}),o}getNodeOutgoingEdgeIds(e){const t=Array.from(this.getNode(e).ports.keys()),o=[];return t.forEach(s=>{this.getPortOutgoingEdgeIds(s).filter(n=>{const a=this.getEdge(n);return this.getPort(a.to).nodeId!==e}).forEach(n=>{o.push(n)})}),o}getNodeCycleEdgeIds(e){const t=Array.from(this.getNode(e).ports.keys()),o=[];return t.forEach(s=>{this.getPortCycleEdgeIds(s).forEach(n=>{o.push(n)}),this.getPortIncomingEdgeIds(s).filter(n=>{const a=this.getEdge(n);return this.getPort(a.to).nodeId===e}).forEach(n=>{o.push(n)})}),o}getNodeAdjacentEdgeIds(e){const t=Array.from(this.getNode(e).ports.keys()),o=[];return t.forEach(s=>{this.getPortIncomingEdgeIds(s).forEach(n=>{o.push(n)}),this.getPortOutgoingEdgeIds(s).forEach(n=>{o.push(n)}),this.getPortCycleEdgeIds(s).forEach(n=>{o.push(n)})}),o}addEdgeInternal(e){this.edges.set(e.id,{from:e.from,to:e.to,payload:{shape:e.shape,priority:e.priority}}),this.edgesElementsMap.set(e.shape.svg,e.id),e.from!==e.to?(this.portOutgoingEdges.get(e.from).add(e.id),this.portIncomingEdges.get(e.to).add(e.id)):this.portCycleEdges.get(e.from).add(e.id)}removeEdgeInternal(e){const{from:t,to:o,payload:s}=this.getEdge(e);this.portCycleEdges.get(t).delete(e),this.portCycleEdges.get(o).delete(e),this.portIncomingEdges.get(t).delete(e),this.portIncomingEdges.get(o).delete(e),this.portOutgoingEdges.get(t).delete(e),this.portOutgoingEdges.get(o).delete(e),this.edges.delete(e),this.edgesElementsMap.delete(s.shape.svg)}}const Ne=r=>({scale:1/r.scale,x:-r.x/r.scale,y:-r.y/r.scale}),Ce={scale:1,x:0,y:0},Te=(r,e)=>({x:r.scale*e.x+r.x,y:r.scale*e.y+r.y});class xt{constructor(e){i(this,"viewportMatrix",Ce);i(this,"contentMatrix",Ce);i(this,"beforeUpdateEmitter");i(this,"onBeforeUpdated");i(this,"afterUpdateEmitter");i(this,"onAfterUpdated");i(this,"afterResizeEmitter");i(this,"onAfterResize");i(this,"observer",new ResizeObserver(()=>{this.afterResizeEmitter.emit()}));this.host=e,[this.afterUpdateEmitter,this.onAfterUpdated]=M(),[this.beforeUpdateEmitter,this.onBeforeUpdated]=M(),[this.afterResizeEmitter,this.onAfterResize]=M(),this.observer.observe(this.host)}getViewportMatrix(){return this.viewportMatrix}getContentMatrix(){return this.contentMatrix}patchViewportMatrix(e){this.viewportMatrix={scale:e.scale??this.viewportMatrix.scale,x:e.x??this.viewportMatrix.x,y:e.y??this.viewportMatrix.y},this.beforeUpdateEmitter.emit(),this.contentMatrix=Ne(this.viewportMatrix),this.afterUpdateEmitter.emit()}patchContentMatrix(e){this.contentMatrix={scale:e.scale??this.contentMatrix.scale,x:e.x??this.contentMatrix.x,y:e.y??this.contentMatrix.y},this.beforeUpdateEmitter.emit(),this.viewportMatrix=Ne(this.contentMatrix),this.afterUpdateEmitter.emit()}getDimensions(){const{width:e,height:t}=this.host.getBoundingClientRect();return{width:e,height:t}}createContentCoords(e){return Te(this.viewportMatrix,e)}createViewportCoords(e){return Te(this.contentMatrix,e)}destroy(){this.observer.disconnect()}}class _{constructor(e){i(this,"elementToNodeId",new Map);i(this,"nodesResizeObserver");i(this,"onAfterNodeAdded",e=>{const t=this.canvas.graph.getNode(e);this.elementToNodeId.set(t.element,e),this.nodesResizeObserver.observe(t.element)});i(this,"onBeforeNodeRemoved",e=>{const t=this.canvas.graph.getNode(e);this.elementToNodeId.delete(t.element),this.nodesResizeObserver.unobserve(t.element)});i(this,"reset",()=>{this.nodesResizeObserver.disconnect(),this.elementToNodeId.clear()});i(this,"revert",()=>{this.reset()});this.canvas=e,this.nodesResizeObserver=new ResizeObserver(t=>{t.forEach(o=>{const s=o.target;this.handleNodeResize(s)})}),this.canvas.graph.onAfterNodeAdded.subscribe(this.onAfterNodeAdded),this.canvas.graph.onBeforeNodeRemoved.subscribe(this.onBeforeNodeRemoved),this.canvas.graph.onBeforeClear.subscribe(this.reset),this.canvas.onBeforeDestroy.subscribe(this.revert)}static configure(e){new _(e)}handleNodeResize(e){const t=this.elementToNodeId.get(e);this.canvas.updateNode(t)}}const At=(r,e,t)=>{const{x:o,y:s,width:n,height:a}=r.getBoundingClientRect();return e>=o&&e<=o+n&&t>=s&&t<=s+a},St=(r,e,t)=>e>=0&&e<=r.innerWidth&&t>=0&&t<=r.innerHeight;class bt{constructor(e,t){this.element=e,this.win=t}verify(e,t){return At(this.element,e,t)&&St(this.win,e,t)}}const H=(r,e)=>{e!==null?r.style.cursor=e:r.style.removeProperty("cursor")},Y=r=>{const e=document.createElement("div");return{id:r.overlayNodeId,element:e,x:r.portCoords.x,y:r.portCoords.y,ports:[{id:r.overlayNodeId,element:e,direction:r.portDirection}]}},Pt=(r,e)=>{let t=e;for(;t!==null;){const o=r.findPortIdsByElement(t)[0]??null;if(o!==null)return{status:"portFound",portId:o};if(r.findNodeIdByElement(t)!==void 0)return{status:"nodeEncountered"};t=t.parentElement}return{status:"notFound"}};function*Me(r,e){const t=r.elementsFromPoint(e.x,e.y);for(const o of t){if(o.shadowRoot!==null){const s=Me(o.shadowRoot,e);for(const n of s)yield n}yield o}}const De=(r,e)=>{const t=Me(document,e);for(const o of t){const s=Pt(r,o);if(s.status==="portFound")return s.portId;if(s.status==="nodeEncountered")return null}return null};var R=(r=>(r.StaticNodeId="static",r.DraggingNodeId="dragging",r.EdgeId="edge",r))(R||{});const Re=(r,e)=>({x:r/2,y:e/2}),m=(r,e,t)=>({x:e.x*r.x-e.y*r.y+((1-e.x)*t.x+e.y*t.y),y:e.y*r.x+e.x*r.y+((1-e.x)*t.y-e.y*t.x)}),Le=(r,e,t)=>{const o={x:r.x+r.width/2,y:r.y+r.height/2},s={x:e.x+e.width/2,y:e.y+e.height/2},n=Math.min(o.x,s.x)-t,a=Math.min(o.y,s.y)-t,h=2*t,d=Math.abs(s.x-o.x)+h,c=Math.abs(s.y-o.y)+h;return{x:n,y:a,width:d,height:c,from:{x:o.x-n,y:o.y-a},to:{x:s.x-n,y:s.y-a}}};class Nt{constructor(e){i(this,"path");i(this,"midpoint");const{from:t,to:o,arrowLength:s,fromDir:n,toDir:a,curvature:h,hasSourceArrow:d,hasTargetArrow:c}=e,l=(t.x+o.x)/2,u=(t.y+o.y)/2;this.midpoint={x:l,y:u};const g=m({x:t.x+s,y:t.y},n,t),p=m({x:o.x-s,y:o.y},a,o),w={x:g.x+n.x*h,y:g.y+n.y*h},f={x:p.x-a.x*h,y:p.y-a.y*h},y=`M ${g.x} ${g.y} C ${w.x} ${w.y}, ${f.x} ${f.y}, ${p.x} ${p.y}`,E=d?"":`M ${t.x} ${t.y} L ${g.x} ${g.y} `,x=c?"":` M ${p.x} ${p.y} L ${o.x} ${o.y}`;this.path=`${E}${y}${x}`}}class Ct{constructor(e){i(this,"path");i(this,"midpoint");const{hasSourceArrow:t,hasTargetArrow:o,curvature:s,fromDir:n,toDir:a,detourDir:h,from:d,to:c,arrowLength:l,detourDistance:u}=e,g=t?m({x:d.x+l,y:d.y},n,d):d,p=o?m({x:c.x-l,y:c.y},a,c):c,w=Math.cos(h)*u,f=Math.sin(h)*u,y=m({x:d.x+l,y:d.y},n,d),E={x:y.x+w,y:y.y+f},x=m({x:c.x-l,y:c.y},a,c),C={x:x.x+w,y:x.y+f},D={x:(E.x+C.x)/2,y:(E.y+C.y)/2},L={x:y.x+s*n.x,y:y.y+s*n.y},W={x:x.x-s*a.x,y:x.y-s*a.y},z={x:y.x+w,y:y.y+f},V={x:x.x+w,y:x.y+f};this.path=[`M ${g.x} ${g.y}`,`L ${y.x} ${y.y}`,`C ${L.x} ${L.y} ${z.x} ${z.y} ${D.x} ${D.y}`,`C ${V.x} ${V.y} ${W.x} ${W.y} ${x.x} ${x.y}`,`L ${p.x} ${p.y}`].join(" "),this.midpoint=D}}const q=Object.freeze({edgeColor:"--edge-color"}),Ve=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","svg");return e.style.pointerEvents="none",e.style.position="absolute",e.style.top="0",e.style.left="0",e.style.overflow="visible",e.style.setProperty(q.edgeColor,r),e},Ie=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","path");return e.setAttribute("stroke",`var(${q.edgeColor})`),e.setAttribute("stroke-width",`${r}`),e.setAttribute("fill","none"),e},X=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","path");return r.setAttribute("fill",`var(${q.edgeColor})`),r},Fe=(r,e)=>{r.style.transform=`translate(${e.x}px, ${e.y}px)`,r.style.width=`${e.width}px`,r.style.height=`${e.height}px`},F=(r,e)=>{const t=[];if(r.length>0&&t.push(`M ${r[0].x} ${r[0].y}`),r.length===2&&t.push(`L ${r[1].x} ${r[1].y}`),r.length>2){const o=r.length-1;let s=0,n=0,a=0;r.forEach((h,d)=>{let c=0,l=0,u=0;const g=d>0,p=d<o,w=g&&p;if(g&&(c=-s,l=-n,u=a),p){const L=r[d+1];s=L.x-h.x,n=L.y-h.y,a=Math.sqrt(s*s+n*n)}const y=a!==0?Math.min((w?e:0)/a,d<o-1?.5:1):0,E=w?{x:h.x+s*y,y:h.y+n*y}:h,C=u!==0?Math.min((w?e:0)/u,d>1?.5:1):0,D=w?{x:h.x+c*C,y:h.y+l*C}:h;d>0&&t.push(`L ${D.x} ${D.y}`),w&&t.push(`C ${h.x} ${h.y} ${h.x} ${h.y} ${E.x} ${E.y}`)})}return t.join(" ")},Tt=(r,e)=>{const t=r.linePoint,o=e.linePoint,s=o.x-t.x>=0,n=r.dirX>=0,a=e.dirX>=0;if(n===a){const u=n===s,g=(t.x+o.x)/2,p=(t.y+o.y)/2,w={x:g,y:p};return u?{points:[r.arrowPoint,{x:w.x,y:t.y},{x:w.x,y:o.y},e.arrowPoint],midpoint:w}:{points:[r.arrowPoint,t,{x:t.x,y:w.y},{x:o.x,y:w.y},o,e.arrowPoint],midpoint:w}}const d=n===s,c=(t.y+o.y)/2;if(d){const u={x:o.x,y:t.y};return{points:[r.arrowPoint,u,o,e.arrowPoint],midpoint:{x:u.x,y:c}}}const l={x:t.x,y:o.y};return{points:[r.arrowPoint,t,l,e.arrowPoint],midpoint:{x:l.x,y:c}}};class Mt{constructor(e){i(this,"path");i(this,"midpoint");const{from:t,to:o,fromDir:s,toDir:n,arrowLength:a,arrowOffset:h,roundness:d,hasSourceArrow:c,hasTargetArrow:l}=e,u=c?m({x:t.x+a,y:t.y},s,t):t,g=l?m({x:o.x-a,y:o.y},n,o):o,p=a+h,w=m({x:t.x+p,y:t.y},s,t),f=m({x:o.x-p,y:o.y},n,o),y=Tt({arrowPoint:u,linePoint:w,dirX:s.x},{arrowPoint:g,linePoint:f,dirX:n.x});this.path=F(y.points,d),this.midpoint=y.midpoint}}class Dt{constructor(e){i(this,"path");i(this,"midpoint");const{hasSourceArrow:t,hasTargetArrow:o,from:s,to:n,arrowLength:a,fromDir:h,toDir:d,arrowOffset:c,detourDir:l,detourDistance:u,roundness:g}=e,p=t?m({x:s.x+a,y:s.y},h,s):s,w=o?m({x:n.x-a,y:n.y},d,n):n,f=a+c,y=Math.cos(l)*u,E=Math.sin(l)*u,x=m({x:s.x+f,y:s.y},h,s),C={x:x.x+y,y:x.y+E},D=m({x:n.x-f,y:n.y},d,n),L={x:D.x+y,y:D.y+E};this.midpoint={x:(C.x+L.x)/2,y:(C.y+L.y)/2},this.path=F([p,x,C,L,D,w],g)}}class Rt{constructor(e){i(this,"path");i(this,"midpoint");const{from:t,to:o,hasSourceArrow:s,hasTargetArrow:n,arrowLength:a,fromDir:h,toDir:d,arrowOffset:c,roundness:l}=e,u=this.createArrowPoint(s,h,t,a),g=this.createArrowPoint(n,d,o,-a),p=a+c,w={x:t.x+p,y:t.y},f=m(w,h,t),y={x:o.x-p,y:o.y},E=m(y,d,o);this.path=F([u,f,E,g],l);const x=(f.x+E.x)/2,C=(f.y+E.y)/2;this.midpoint={x,y:C}}createArrowPoint(e,t,o,s){if(!e)return o;const n={x:o.x+s,y:o.y};return m(n,t,o)}}const Lt=(r,e)=>{const t=r.linePoint,o=e.linePoint,s=o.y-t.y>=0,n=r.dirY>=0,a=e.dirY>=0;if(n===a){const u=n===s,g=(t.x+o.x)/2,p=(t.y+o.y)/2,w={x:g,y:p};return u?{points:[r.arrowPoint,{x:t.x,y:w.y},{x:o.x,y:w.y},e.arrowPoint],midpoint:w}:{points:[r.arrowPoint,t,{x:w.x,y:t.y},{x:w.x,y:o.y},o,e.arrowPoint],midpoint:w}}const d=n===s,c=(t.x+o.x)/2;if(d){const u={x:t.x,y:o.y};return{points:[r.arrowPoint,u,o,e.arrowPoint],midpoint:{x:c,y:u.y}}}const l={x:o.x,y:t.y};return{points:[r.arrowPoint,t,l,e.arrowPoint],midpoint:{x:c,y:l.y}}};class Vt{constructor(e){i(this,"path");i(this,"midpoint");const{from:t,to:o,hasSourceArrow:s,hasTargetArrow:n,arrowLength:a,arrowOffset:h,fromDir:d,toDir:c,roundness:l}=e,u=s?m({x:t.x+a,y:t.y},d,t):t,g=n?m({x:o.x-a,y:o.y},c,o):o,p=a+h,w=m({x:t.x+p,y:t.y},d,t),f=m({x:o.x-p,y:o.y},c,o),y=Lt({arrowPoint:u,linePoint:w,dirY:d.y},{arrowPoint:g,linePoint:f,dirY:c.y});this.path=F(y.points,l),this.midpoint=y.midpoint}}class ee{constructor(e){i(this,"path");i(this,"midpoint");const{side:t,arrowLength:o,arrowOffset:s,dir:n,origin:a,hasArrow:h}=e,d=o+s,c=d+2*t,u=[{x:o,y:0},{x:d,y:0},{x:d,y:t},{x:c,y:t},{x:c,y:-t},{x:d,y:-t},{x:d,y:0},{x:o,y:0}].map(p=>m(p,n,{x:0,y:0})).map(p=>({x:p.x+a.x,y:p.y+a.y})),g=`M ${a.x} ${a.y} L ${u[0].x} ${u[0].y} `;this.path=`${h?"":g}${F(u,e.roundness)}`,this.midpoint={x:(u[3].x+u[4].x)/2,y:(u[3].y+u[4].y)/2}}}class It{constructor(e){i(this,"path");i(this,"midpoint");const{arrowLength:t,radius:o,smallRadius:s,dir:n,origin:a,hasArrow:h}=e,d=s+o,c=s*o/d,l=Math.sqrt(d*d-s*s),u=l*s/d,g=l+o+t,p=t+u,f=[{x:t,y:0},{x:p,y:c},{x:p,y:-c},{x:g,y:0}].map(x=>m(x,n,{x:0,y:0})).map(x=>({x:x.x+a.x,y:x.y+a.y})),y=[`M ${f[0].x} ${f[0].y}`,`A ${s} ${s} 0 0 1 ${f[1].x} ${f[1].y}`,`A ${o} ${o} 0 1 0 ${f[2].x} ${f[2].y}`,`A ${s} ${s} 0 0 1 ${f[0].x} ${f[0].y}`].join(" "),E=`M ${a.x} ${a.y} L ${f[0].x} ${f[0].y} `;this.path=`${h?"":E}${y}`,this.midpoint=f[3]}}class Ft{constructor(e){i(this,"path");i(this,"midpoint");i(this,"diagonalDistance");const{from:t,to:o,sourceOffset:s,targetOffset:n,hasSourceArrow:a,hasTargetArrow:h,arrowLength:d}=e;this.midpoint={x:(t.x+o.x)/2,y:(t.y+o.y)/2};const c=o.x-t.x,l=o.y-t.y;if(this.diagonalDistance=Math.sqrt(c*c+l*l),this.diagonalDistance===0){this.path="";return}const u={x:c,y:l},g=this.createDirectLinePoint({offset:s,hasArrow:a,flip:1,shift:t,arrowLength:d,dir:u}),p=this.createDirectLinePoint({offset:n,hasArrow:h,flip:-1,shift:o,arrowLength:d,dir:u});this.path=`M ${g.x} ${g.y} L ${p.x} ${p.y}`}createDirectLinePoint(e){const t=e.hasArrow?e.arrowLength:0,o=e.offset+t,s=e.flip*o/this.diagonalDistance,{dir:n,shift:a}=e;return{x:n.x*s+a.x,y:n.y*s+a.y}}}const Ut=(r,e,t)=>{const o=Math.max(r.y,e.y),s=Math.min(r.y,e.y);return(t>=0?o:s)+t};class $t{constructor(e){i(this,"path");i(this,"midpoint");const{hasSourceArrow:t,hasTargetArrow:o,arrowLength:s,from:n,to:a,fromDir:h,toDir:d,arrowOffset:c,roundness:l,detourDistance:u}=e,g=t?m({x:n.x+s,y:n.y},h,n):n,p=o?m({x:a.x-s,y:a.y},d,a):a,w=s+c,f=m({x:n.x+w,y:n.y},h,n),y=m({x:a.x-w,y:a.y},d,a),E=Ut(f,y,u);this.midpoint={x:(f.x+y.x)/2,y:E},this.path=F([g,f,{x:f.x,y:E},{x:y.x,y:E},y,p],l)}}const Bt=(r,e,t)=>{const o=Math.max(r.x,e.x),s=Math.min(r.x,e.x);return(t>=0?o:s)+t};class Ot{constructor(e){i(this,"path");i(this,"midpoint");const{hasSourceArrow:t,hasTargetArrow:o,arrowLength:s,fromDir:n,toDir:a,from:h,to:d,arrowOffset:c,detourDistance:l,roundness:u}=e,g=t?m({x:h.x+s,y:h.y},n,h):h,p=o?m({x:d.x-s,y:d.y},a,d):d,w=s+c,f=m({x:h.x+w,y:h.y},n,h),y=m({x:d.x-w,y:d.y},a,d),E=Bt(f,y,l);this.midpoint={x:E,y:(f.y+y.y)/2},this.path=F([g,f,{x:E,y:f.y},{x:E,y:y.y},y,p],u)}}const v=Object.freeze({color:"#777777",width:1,arrowLength:20,polygonArrowRadius:4,circleArrowRadius:8,wedgeArrowSmallRadius:20,wedgeArrowRadius:100,wedgeArrowAngle:Math.PI/12,arrowOffset:15,hasSourceArrow:!1,hasTargetArrow:!1,cycleRadius:30,cycleSquareSide:30,roundness:10,detourDistance:100,detourDirection:-Math.PI/2,detourDirectionVertical:0,smallCycleRadius:15,curvature:90,interactiveWidth:10,preOffset:0}),Ue=r=>({x:Math.cos(r),y:Math.sin(r)});class G{constructor(e){i(this,"svg");i(this,"group",document.createElementNS("http://www.w3.org/2000/svg","g"));i(this,"line");i(this,"sourceArrow",null);i(this,"targetArrow",null);i(this,"onAfterRender");i(this,"afterRenderEmitter");i(this,"arrowRenderer");this.params=e,[this.afterRenderEmitter,this.onAfterRender]=M(),this.arrowRenderer=this.params.arrowRenderer,this.svg=Ve(e.color),this.svg.appendChild(this.group),this.line=Ie(e.width),this.group.appendChild(this.line),e.hasSourceArrow&&(this.sourceArrow=X(),this.group.appendChild(this.sourceArrow)),e.hasTargetArrow&&(this.targetArrow=X(),this.group.appendChild(this.targetArrow))}render(e){const{x:t,y:o,width:s,height:n,from:a,to:h}=Le(e.from,e.to,this.params.padding);Fe(this.svg,{x:t,y:o,width:s,height:n});const d=Ue(e.from.direction),c=Ue(e.to.direction);let l={x:-c.x,y:-c.y},u;e.category===T.PortCycle?(u=this.params.createCyclePath,l=d):e.category===T.NodeCycle?u=this.params.createDetourPath:u=this.params.createLinePath;const g=u(a,h,d,c);this.line.setAttribute("d",g.path);let p=null;this.sourceArrow&&(p=this.arrowRenderer({direction:d,shift:a,arrowLength:this.params.arrowLength}),this.sourceArrow.setAttribute("d",p));let w=null;this.targetArrow&&(w=this.arrowRenderer({direction:l,shift:h,arrowLength:this.params.arrowLength}),this.targetArrow.setAttribute("d",w)),this.afterRenderEmitter.emit({edgePath:g,sourceArrowPath:p,targetArrowPath:w})}}const Wt=r=>e=>{const o=[{x:0,y:0},{x:e.arrowLength,y:r.radius},{x:e.arrowLength,y:-r.radius}].map(h=>m(h,e.direction,{x:0,y:0})).map(h=>({x:h.x+e.shift.x,y:h.y+e.shift.y})),s=`M ${o[0].x} ${o[0].y}`,n=`L ${o[1].x} ${o[1].y}`,a=`L ${o[2].x} ${o[2].y}`;return`${s} ${n} ${a} Z`},zt=r=>e=>{const t=r.radius,o=e.arrowLength,s=(o*o+2*o*t)/(2*t),n=s+t,a=o+t-t*(o+t)/n,h=t*s/n,c=[{x:0,y:0},{x:a,y:-h},{x:a,y:h}].map(w=>m(w,e.direction,{x:0,y:0})).map(w=>({x:w.x+e.shift.x,y:w.y+e.shift.y})),l=`M ${c[0].x} ${c[0].y}`,u=`A ${s} ${s} 0 0 0 ${c[1].x} ${c[1].y}`,g=`A ${t} ${t} 0 0 0 ${c[2].x} ${c[2].y}`,p=`A ${s} ${s} 0 0 0 ${c[0].x} ${c[0].y}`;return`${l} ${u} ${g} ${p}`},kt=r=>e=>{const t=r.smallRadius,o=r.radius,s=m({x:e.arrowLength,y:0},{x:Math.cos(r.angle),y:Math.sin(r.angle)},{x:e.arrowLength+r.smallRadius,y:0}),a=[{x:0,y:0},{x:s.x,y:-s.y},s].map(u=>m(u,e.direction,{x:0,y:0})).map(u=>({x:u.x+e.shift.x,y:u.y+e.shift.y})),h=`M ${a[0].x} ${a[0].y}`,d=`A ${o} ${o} 0 0 1 ${a[1].x} ${a[1].y}`,c=`A ${t} ${t} 0 0 1 ${a[2].x} ${a[2].y}`,l=`A ${o} ${o} 0 0 1 ${a[0].x} ${a[0].y}`;return`${h} ${d} ${c} ${l}`},$=r=>{if(typeof r=="function")return r;switch(r.type){case"triangle":return Wt({radius:r.radius??v.polygonArrowRadius});case"arc":return zt({radius:r.radius??v.circleArrowRadius});default:return kt({smallRadius:r.smallRadius??v.wedgeArrowSmallRadius,angle:r.angle??v.wedgeArrowAngle,radius:r.radius??v.wedgeArrowRadius})}},B=50;class $e{constructor(e){i(this,"svg");i(this,"group");i(this,"line");i(this,"sourceArrow");i(this,"targetArrow");i(this,"onAfterRender");i(this,"arrowLength");i(this,"curvature");i(this,"portCycleRadius");i(this,"portCycleSmallRadius");i(this,"detourDirection");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");i(this,"pathShape");i(this,"createCyclePath",(e,t,o)=>new It({origin:e,dir:o,radius:this.portCycleRadius,smallRadius:this.portCycleSmallRadius,arrowLength:this.arrowLength,hasArrow:this.hasSourceArrow||this.hasTargetArrow}));i(this,"createDetourPath",(e,t,o,s)=>new Ct({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,detourDir:this.detourDirection,detourDistance:this.detourDistance,curvature:this.curvature,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));i(this,"createLinePath",(e,t,o,s)=>new Nt({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,curvature:this.curvature,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??v.arrowLength,this.curvature=(e==null?void 0:e.curvature)??v.curvature,this.portCycleRadius=(e==null?void 0:e.cycleRadius)??v.cycleRadius,this.portCycleSmallRadius=(e==null?void 0:e.smallCycleRadius)??v.smallCycleRadius,this.detourDirection=(e==null?void 0:e.detourDirection)??v.detourDirection,this.detourDistance=(e==null?void 0:e.detourDistance)??v.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??v.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??v.hasTargetArrow,this.pathShape=new G({color:(e==null?void 0:e.color)??v.color,width:(e==null?void 0:e.width)??v.width,arrowRenderer:$((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath,padding:B}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){this.pathShape.render(e)}}const Be=r=>Math.cos(r)>0?0:Math.PI;class Oe{constructor(e){i(this,"svg");i(this,"group");i(this,"line");i(this,"sourceArrow");i(this,"targetArrow");i(this,"onAfterRender");i(this,"arrowLength");i(this,"arrowOffset");i(this,"roundness");i(this,"cycleSquareSide");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");i(this,"pathShape");i(this,"createCyclePath",(e,t,o)=>new ee({origin:e,dir:o,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasArrow:this.hasSourceArrow||this.hasTargetArrow}));i(this,"createDetourPath",(e,t,o,s)=>new $t({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));i(this,"createLinePath",(e,t,o,s)=>new Mt({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??v.arrowLength,this.arrowOffset=(e==null?void 0:e.arrowOffset)??v.arrowOffset,this.cycleSquareSide=(e==null?void 0:e.cycleSquareSide)??v.cycleSquareSide;const t=(e==null?void 0:e.roundness)??v.roundness;this.roundness=Math.min(t,this.arrowOffset,this.cycleSquareSide/2),this.detourDistance=(e==null?void 0:e.detourDistance)??v.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??v.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??v.hasTargetArrow,this.pathShape=new G({color:(e==null?void 0:e.color)??v.color,width:(e==null?void 0:e.width)??v.width,arrowRenderer:$((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath,padding:B}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){const{from:t,to:o,category:s}=e;this.pathShape.render({category:s,from:{...t,direction:Be(t.direction)},to:{...o,direction:Be(o.direction)}})}}class We{constructor(e){i(this,"svg");i(this,"group");i(this,"line");i(this,"sourceArrow");i(this,"targetArrow");i(this,"onAfterRender");i(this,"arrowLength");i(this,"arrowOffset");i(this,"roundness");i(this,"cycleSquareSide");i(this,"detourDirection");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");i(this,"pathShape");i(this,"createCyclePath",(e,t,o)=>new ee({origin:e,dir:o,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasArrow:this.hasSourceArrow||this.hasTargetArrow}));i(this,"createDetourPath",(e,t,o,s)=>new Dt({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDir:this.detourDirection,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));i(this,"createLinePath",(e,t,o,s)=>new Rt({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??v.arrowLength,this.arrowOffset=(e==null?void 0:e.arrowOffset)??v.arrowOffset,this.cycleSquareSide=(e==null?void 0:e.cycleSquareSide)??v.cycleSquareSide;const t=(e==null?void 0:e.roundness)??v.roundness;this.roundness=Math.min(t,this.arrowOffset,this.cycleSquareSide/2),this.detourDirection=(e==null?void 0:e.detourDirection)??v.detourDirection,this.detourDistance=(e==null?void 0:e.detourDistance)??v.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??v.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??v.hasTargetArrow,this.pathShape=new G({color:(e==null?void 0:e.color)??v.color,width:(e==null?void 0:e.width)??v.width,arrowRenderer:$((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath,padding:B}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){this.pathShape.render(e)}}const ze=Math.PI/2,ke=r=>Math.sin(r)>0?ze:-ze;class He{constructor(e){i(this,"svg");i(this,"group");i(this,"line");i(this,"sourceArrow");i(this,"targetArrow");i(this,"onAfterRender");i(this,"arrowLength");i(this,"arrowOffset");i(this,"roundness");i(this,"cycleSquareSide");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");i(this,"pathShape");i(this,"createCyclePath",(e,t,o)=>new ee({origin:e,dir:o,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasArrow:this.hasSourceArrow||this.hasTargetArrow}));i(this,"createDetourPath",(e,t,o,s)=>new Ot({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));i(this,"createLinePath",(e,t,o,s)=>new Vt({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??v.arrowLength,this.arrowOffset=(e==null?void 0:e.arrowOffset)??v.arrowOffset,this.cycleSquareSide=(e==null?void 0:e.cycleSquareSide)??v.cycleSquareSide;const t=(e==null?void 0:e.roundness)??v.roundness;this.roundness=Math.min(t,this.arrowOffset,this.cycleSquareSide/2),this.detourDistance=(e==null?void 0:e.detourDistance)??v.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??v.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??v.hasTargetArrow,this.pathShape=new G({color:(e==null?void 0:e.color)??v.color,width:(e==null?void 0:e.width)??v.width,arrowRenderer:$((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath,padding:B}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){const{from:t,to:o,category:s}=e;this.pathShape.render({category:s,from:{...t,direction:ke(t.direction)},to:{...o,direction:ke(o.direction)}})}}class te{constructor(e){i(this,"svg");i(this,"group",document.createElementNS("http://www.w3.org/2000/svg","g"));i(this,"line");i(this,"sourceArrow",null);i(this,"targetArrow",null);i(this,"color");i(this,"width");i(this,"arrowLength");i(this,"sourceOffset");i(this,"targetOffset");i(this,"onAfterRender");i(this,"afterRenderEmitter");i(this,"arrowRenderer");[this.afterRenderEmitter,this.onAfterRender]=M(),this.color=(e==null?void 0:e.color)??v.color,this.width=(e==null?void 0:e.width)??v.width,this.arrowLength=(e==null?void 0:e.arrowLength)??v.arrowLength,this.arrowRenderer=$((e==null?void 0:e.arrowRenderer)??{}),this.sourceOffset=(e==null?void 0:e.sourceOffset)??v.preOffset,this.targetOffset=(e==null?void 0:e.targetOffset)??v.preOffset,this.svg=Ve(this.color),this.svg.appendChild(this.group),this.line=Ie(this.width),this.group.appendChild(this.line),e!=null&&e.hasSourceArrow&&(this.sourceArrow=X(),this.group.appendChild(this.sourceArrow)),e!=null&&e.hasTargetArrow&&(this.targetArrow=X(),this.group.appendChild(this.targetArrow))}render(e){const{x:t,y:o,width:s,height:n,from:a,to:h}=Le(e.from,e.to,B);Fe(this.svg,{x:t,y:o,width:s,height:n});const d=new Ft({from:a,to:h,sourceOffset:this.sourceOffset,targetOffset:this.targetOffset,hasSourceArrow:this.sourceArrow!==null,hasTargetArrow:this.targetArrow!==null,arrowLength:this.arrowLength});this.line.setAttribute("d",d.path);let c=null,l=null;const u=d.diagonalDistance;if(u===0)this.sourceArrow!==null&&(c="",this.sourceArrow.setAttribute("d",c)),this.targetArrow!==null&&(l="",this.targetArrow.setAttribute("d",l));else{const g={x:(h.x-a.x)/u,y:(h.y-a.y)/u};if(this.sourceArrow){const p={x:g.x*this.sourceOffset,y:g.y*this.sourceOffset};c=this.arrowRenderer({direction:g,shift:p,arrowLength:this.arrowLength}),this.sourceArrow.setAttribute("d",c)}if(this.targetArrow){const p={x:g.x*this.targetOffset,y:g.y*this.targetOffset};l=this.arrowRenderer({direction:{x:-g.x,y:-g.y},shift:{x:h.x-p.x,y:h.y-p.y},arrowLength:this.arrowLength}),this.targetArrow.setAttribute("d",l)}}this.afterRenderEmitter.emit({edgePath:d,sourceArrowPath:c,targetArrowPath:l})}}const Ht=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","g");return r.style.pointerEvents="auto",r.style.cursor="pointer",r},Yt=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","path");return e.setAttribute("stroke","transparent"),e.setAttribute("stroke-width",`${r}`),e.setAttribute("fill","none"),e.setAttribute("stroke-linecap","round"),e},Ye=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","path");return e.setAttribute("stroke-linejoin","round"),e.setAttribute("stroke-width",`${r}`),e.setAttribute("fill","transparent"),e.setAttribute("stroke","transparent"),e};class Xe extends Error{constructor(e){super(e),this.name="InteractiveEdgeError"}}class re{constructor(e,t){i(this,"svg");i(this,"group");i(this,"line");i(this,"sourceArrow");i(this,"targetArrow");i(this,"handle",Ht());i(this,"onAfterRender");i(this,"interactiveLine");i(this,"interactiveSourceArrow",null);i(this,"interactiveTargetArrow",null);if(this.baseEdge=e,e instanceof re)throw new Xe("interactive edge can be configured only once");this.svg=this.baseEdge.svg,this.group=this.baseEdge.group,this.line=this.baseEdge.line,this.sourceArrow=this.baseEdge.sourceArrow,this.targetArrow=this.baseEdge.targetArrow,this.onAfterRender=this.baseEdge.onAfterRender;const o=(t==null?void 0:t.distance)??v.interactiveWidth;this.interactiveLine=Yt(o),this.handle.appendChild(this.interactiveLine),this.sourceArrow&&(this.interactiveSourceArrow=Ye(o),this.handle.appendChild(this.interactiveSourceArrow)),this.targetArrow&&(this.interactiveTargetArrow=Ye(o),this.handle.appendChild(this.interactiveTargetArrow)),this.group.appendChild(this.handle),this.baseEdge.onAfterRender.subscribe(s=>{this.interactiveLine.setAttribute("d",s.edgePath.path),this.interactiveSourceArrow&&this.interactiveSourceArrow.setAttribute("d",s.sourceArrowPath),this.interactiveTargetArrow&&this.interactiveTargetArrow.setAttribute("d",s.targetArrowPath)})}render(e){this.baseEdge.render(e)}}class Xt{constructor(e,t){i(this,"group");i(this,"line");i(this,"sourceArrow");i(this,"targetArrow");i(this,"onAfterRender");i(this,"svg");this.baseShape=e,this.midpointElement=t,this.svg=this.baseShape.svg,this.group=this.baseShape.group,this.line=this.baseShape.line,this.sourceArrow=this.baseShape.sourceArrow,this.targetArrow=this.baseShape.targetArrow,this.onAfterRender=this.baseShape.onAfterRender,this.svg.append(this.midpointElement),this.baseShape.onAfterRender.subscribe(o=>{const s=o.edgePath.midpoint,n=`translate(${s.x}px, ${s.y}px)`;this.midpointElement.style.setProperty("transform",n)})}render(e){this.baseShape.render(e)}}class Ge{constructor(e){i(this,"onAfterNodeAdded");i(this,"onAfterNodeUpdated");i(this,"onAfterNodePriorityUpdated");i(this,"onBeforeNodeRemoved");i(this,"onAfterPortMarked");i(this,"onAfterPortUpdated");i(this,"onBeforePortUnmarked");i(this,"onAfterEdgeAdded");i(this,"onAfterEdgeShapeUpdated");i(this,"onAfterEdgeUpdated");i(this,"onAfterEdgePriorityUpdated");i(this,"onBeforeEdgeRemoved");i(this,"onBeforeClear");this.graphStore=e,this.onAfterNodeAdded=this.graphStore.onAfterNodeAdded,this.onAfterNodeUpdated=this.graphStore.onAfterNodeUpdated,this.onAfterNodePriorityUpdated=this.graphStore.onAfterNodePriorityUpdated,this.onBeforeNodeRemoved=this.graphStore.onBeforeNodeRemoved,this.onAfterPortMarked=this.graphStore.onAfterPortAdded,this.onAfterPortUpdated=this.graphStore.onAfterPortUpdated,this.onBeforePortUnmarked=this.graphStore.onBeforePortRemoved,this.onAfterEdgeAdded=this.graphStore.onAfterEdgeAdded,this.onAfterEdgeShapeUpdated=this.graphStore.onAfterEdgeShapeUpdated,this.onAfterEdgeUpdated=this.graphStore.onAfterEdgeUpdated,this.onAfterEdgePriorityUpdated=this.graphStore.onAfterEdgePriorityUpdated,this.onBeforeEdgeRemoved=this.graphStore.onBeforeEdgeRemoved,this.onBeforeClear=this.graphStore.onBeforeClear}hasNode(e){return this.graphStore.hasNode(e)}getNode(e){const t=this.graphStore.getNode(e),{payload:o}=t;return{element:t.element,x:o.x,y:o.y,centerFn:o.centerFn,priority:o.priority}}findNodeIdByElement(e){return this.graphStore.findNodeIdByElement(e)}getAllNodeIds(){return this.graphStore.getAllNodeIds()}hasPort(e){return this.graphStore.hasPort(e)}getPort(e){const t=this.graphStore.getPort(e);return{element:t.element,direction:t.payload.direction,nodeId:t.nodeId}}getAllPortIds(){return this.graphStore.getAllPortIds()}getNodePortIds(e){return this.graphStore.getNodePortIds(e)}findPortIdsByElement(e){return this.graphStore.findPortIdsByElement(e)}getAllEdgeIds(){return this.graphStore.getAllEdgeIds()}hasEdge(e){return this.graphStore.hasEdge(e)}getEdge(e){const t=this.graphStore.getEdge(e),{payload:o}=t;return{from:t.from,to:t.to,priority:o.priority,shape:o.shape}}findEdgeIdByElement(e){return this.graphStore.findEdgeIdByElement(e)}getPortIncomingEdgeIds(e){return this.graphStore.getPortIncomingEdgeIds(e)}getPortOutgoingEdgeIds(e){return this.graphStore.getPortOutgoingEdgeIds(e)}getPortCycleEdgeIds(e){return this.graphStore.getPortCycleEdgeIds(e)}getPortAdjacentEdgeIds(e){return this.graphStore.getPortAdjacentEdgeIds(e)}getNodeIncomingEdgeIds(e){return this.graphStore.getNodeIncomingEdgeIds(e)}getNodeOutgoingEdgeIds(e){return this.graphStore.getNodeOutgoingEdgeIds(e)}getNodeCycleEdgeIds(e){return this.graphStore.getNodeCycleEdgeIds(e)}getNodeAdjacentEdgeIds(e){return this.graphStore.getNodeAdjacentEdgeIds(e)}}class oe{constructor(e){i(this,"counter",0);this.checkExists=e}create(e){if(e!==void 0)return e;for(;this.checkExists(this.counter);)this.counter++;return this.counter}reset(){this.counter=0}}class je{constructor(e,t,o){i(this,"nodeIdGenerator",new oe(e=>this.graphStore.hasNode(e)));i(this,"portIdGenerator",new oe(e=>this.graphStore.hasPort(e)));i(this,"edgeIdGenerator",new oe(e=>this.graphStore.hasEdge(e)));i(this,"onAfterNodeAdded",e=>{this.htmlView.attachNode(e)});i(this,"onAfterNodeUpdated",e=>{this.htmlView.updateNodePosition(e),this.graphStore.getNodeAdjacentEdgeIds(e).forEach(t=>{this.htmlView.renderEdge(t)})});i(this,"onAfterNodePriorityUpdated",e=>{this.htmlView.updateNodePriority(e)});i(this,"onBeforeNodeRemoved",e=>{this.graphStore.getNodePortIds(e).forEach(t=>{this.unmarkPort(t)}),this.htmlView.detachNode(e)});i(this,"onAfterPortUpdated",e=>{this.graphStore.getPortAdjacentEdgeIds(e).forEach(t=>{this.htmlView.renderEdge(t)})});i(this,"onBeforePortUnmarked",e=>{this.graphStore.getPortAdjacentEdgeIds(e).forEach(t=>{this.removeEdge(t)})});i(this,"onAfterEdgeAdded",e=>{this.htmlView.attachEdge(e)});i(this,"onAfterEdgeShapeUpdated",e=>{this.htmlView.updateEdgeShape(e)});i(this,"onAfterEdgeUpdated",e=>{this.htmlView.renderEdge(e)});i(this,"onAfterEdgePriorityUpdated",e=>{this.htmlView.updateEdgePriority(e)});i(this,"onBeforeEdgeRemoved",e=>{this.htmlView.detachEdge(e)});i(this,"onBeforeClear",()=>{this.nodeIdGenerator.reset(),this.portIdGenerator.reset(),this.edgeIdGenerator.reset(),this.htmlView.clear()});this.graphStore=e,this.htmlView=t,this.params=o,this.graphStore.onAfterNodeAdded.subscribe(this.onAfterNodeAdded),this.graphStore.onAfterNodeUpdated.subscribe(this.onAfterNodeUpdated),this.graphStore.onAfterNodePriorityUpdated.subscribe(this.onAfterNodePriorityUpdated),this.graphStore.onBeforeNodeRemoved.subscribe(this.onBeforeNodeRemoved),this.graphStore.onAfterPortUpdated.subscribe(this.onAfterPortUpdated),this.graphStore.onBeforePortRemoved.subscribe(this.onBeforePortUnmarked),this.graphStore.onAfterEdgeAdded.subscribe(this.onAfterEdgeAdded),this.graphStore.onAfterEdgeShapeUpdated.subscribe(this.onAfterEdgeShapeUpdated),this.graphStore.onAfterEdgeUpdated.subscribe(this.onAfterEdgeUpdated),this.graphStore.onAfterEdgePriorityUpdated.subscribe(this.onAfterEdgePriorityUpdated),this.graphStore.onBeforeEdgeRemoved.subscribe(this.onBeforeEdgeRemoved),this.graphStore.onBeforeClear.subscribe(this.onBeforeClear)}addNode(e){const t=this.nodeIdGenerator.create(e.id);if(this.graphStore.addNode({id:t,element:e.element,x:e.x??null,y:e.y??null,centerFn:e.centerFn??this.params.nodes.centerFn,priority:e.priority??this.params.nodes.priorityFn()}),e.ports!==void 0)for(const o of e.ports)this.markPort({id:o.id,element:o.element,nodeId:t,direction:o.direction})}updateNode(e,t){this.graphStore.updateNode(e,t??{})}removeNode(e){this.graphStore.removeNode(e)}markPort(e){const t=this.portIdGenerator.create(e.id);this.graphStore.addPort({id:t,element:e.element,nodeId:e.nodeId,direction:e.direction??this.params.ports.direction})}updatePort(e,t){this.graphStore.updatePort(e,t??{})}unmarkPort(e){this.graphStore.removePort(e)}addEdge(e){const t=this.edgeIdGenerator.create(e.id);this.graphStore.addEdge({id:t,from:e.from,to:e.to,shape:e.shape??this.params.edges.shapeFactory(t),priority:e.priority??this.params.edges.priorityFn()})}updateEdge(e,t){this.graphStore.updateEdge(e,t??{})}removeEdge(e){this.graphStore.removeEdge(e)}clear(){this.graphStore.clear()}destroy(){this.graphStore.onAfterNodeAdded.unsubscribe(this.onAfterNodeAdded),this.graphStore.onAfterNodeUpdated.unsubscribe(this.onAfterNodeUpdated),this.graphStore.onAfterNodePriorityUpdated.unsubscribe(this.onAfterNodePriorityUpdated),this.graphStore.onBeforeNodeRemoved.unsubscribe(this.onBeforeNodeRemoved),this.graphStore.onAfterPortUpdated.unsubscribe(this.onAfterPortUpdated),this.graphStore.onBeforePortRemoved.unsubscribe(this.onBeforePortUnmarked),this.graphStore.onAfterEdgeAdded.unsubscribe(this.onAfterEdgeAdded),this.graphStore.onAfterEdgeShapeUpdated.unsubscribe(this.onAfterEdgeShapeUpdated),this.graphStore.onAfterEdgeUpdated.unsubscribe(this.onAfterEdgeUpdated),this.graphStore.onAfterEdgePriorityUpdated.unsubscribe(this.onAfterEdgePriorityUpdated),this.graphStore.onBeforeEdgeRemoved.unsubscribe(this.onBeforeEdgeRemoved),this.graphStore.onBeforeClear.unsubscribe(this.onBeforeClear),this.htmlView.destroy()}}const Gt=r=>{setTimeout(()=>{r()})},Je=r=>{queueMicrotask(()=>{r()})},se=r=>{r()};class Ke{constructor(e){i(this,"onBeforeUpdated");i(this,"onAfterUpdated");i(this,"onAfterResize");this.viewportStore=e,this.onBeforeUpdated=this.viewportStore.onBeforeUpdated,this.onAfterUpdated=this.viewportStore.onAfterUpdated,this.onAfterResize=this.viewportStore.onAfterResize}getViewportMatrix(){return{...this.viewportStore.getViewportMatrix()}}getContentMatrix(){return{...this.viewportStore.getContentMatrix()}}getDimensions(){return this.viewportStore.getDimensions()}createContentCoords(e){return this.viewportStore.createContentCoords(e)}createViewportCoords(e){return this.viewportStore.createViewportCoords(e)}}const jt=(r,e)=>Symbol.iterator in r?{minContentScale:e.focus.minContentScale,nodes:r,contentPadding:e.focus.contentPadding,animationDuration:e.focus.animationDuration}:{minContentScale:r.minContentScale??e.focus.minContentScale,nodes:r.nodes??[],contentPadding:r.contentPadding??r.contentOffset??e.focus.contentPadding,animationDuration:r.animationDuration??e.focus.animationDuration},Qe=(r,e,t)=>({scale:r.scale,x:r.x+r.scale*e,y:r.y+r.scale*t}),Ze=(r,e,t,o)=>({scale:r.scale*e,x:r.scale*(1-e)*t+r.x,y:r.scale*(1-e)*o+r.y});class _e{constructor(e,t,o,s){this.graphStore=e,this.viewportStore=t,this.params=o,this.win=s}patchViewportMatrix(e){this.viewportStore.patchViewportMatrix(e)}patchContentMatrix(e){this.viewportStore.patchContentMatrix(e)}center(e,t){const{width:o,height:s}=this.viewportStore.getDimensions(),n={x:o/2,y:s/2},a=this.viewportStore.getViewportMatrix(),h=this.viewportStore.createViewportCoords(e),d=h.x-n.x,c=h.y-n.y;let l=Qe(a,d,c);const u=t==null?void 0:t.contentScale;if(u!==void 0){const p=1/u;l=Ze(l,p/a.scale,n.x,n.y)}const g=(t==null?void 0:t.animationDuration)??0;g>0?this.animateNavigation(a,l,g):this.viewportStore.patchViewportMatrix(l)}focus(e){const t=jt(e??{},this.params);this.params.focus.schedule(()=>{this.navigate(t)})}destroy(){this.viewportStore.destroy()}navigate(e){let t=1/0,o=-1/0,s=1/0,n=-1/0,a=0;const h=new Set;for(const d of e.nodes)h.add(d);if(this.graphStore.getAllNodeIds().forEach(d=>{const{payload:c}=this.graphStore.getNode(d);c.x!==null&&c.y!==null&&(h.size===0||h.has(d))&&(t=Math.min(c.x,t),o=Math.max(c.x,o),s=Math.min(c.y,s),n=Math.max(c.y,n),a++)}),a>0){t-=e.contentPadding,s-=e.contentPadding,o+=e.contentPadding,n+=e.contentPadding;const d={x:(t+o)/2,y:(s+n)/2},{scale:c}=this.viewportStore.getContentMatrix(),l=(n-s)*c,u=(o-t)*c,{width:g,height:p}=this.viewportStore.getDimensions(),w=Math.max(u/g,l/p),f=w>1?c/w:c,y=Math.max(f,e.minContentScale);this.center(d,{contentScale:y,animationDuration:e.animationDuration})}}animateNavigation(e,t,o){let s;const n={scale:t.scale-e.scale,x:t.x-e.x,y:t.y-e.y},a=h=>{s===void 0&&(s=h);const d=Math.min((h-s)/o,1);this.viewportStore.patchViewportMatrix({scale:e.scale+d*n.scale,x:e.x+d*n.x,y:e.y+d*n.y}),d<1&&this.win.requestAnimationFrame(a)};this.win.requestAnimationFrame(a)}}const qe=(r,e,t)=>{const o=new Pe,s=new Ge(o),n=new Ke(e),a=new Se(o,e,r),h={nodes:{centerFn:Re,priorityFn:()=>0},edges:{shapeFactory:()=>new te,priorityFn:()=>0},ports:{direction:0}},d=new je(o,a,h),c={focus:{contentPadding:0,minContentScale:0,schedule:se,animationDuration:0}},l=new _e(o,e,c,t);return new be(s,n,d,l)};class j{constructor(e,t,o,s){i(this,"onAfterPortMarked",e=>{const t=this.canvas.graph.getPort(e);this.canvas.graph.findPortIdsByElement(t.element).length===1&&this.hookPortEvents(t.element)});i(this,"onBeforePortUnmarked",e=>{const t=this.canvas.graph.getPort(e);this.canvas.graph.findPortIdsByElement(t.element).length===1&&this.unhookPortEvents(t.element)});i(this,"onPortMouseDown",e=>{const t=e;if(!this.params.mouseDownEventVerifier(t))return;const o=e.currentTarget,s=this.canvas.graph.findPortIdsByElement(o)[0];this.params.onPointerDownVerifier(s,{x:t.clientX,y:t.clientY})&&(e.stopPropagation(),this.window.addEventListener("mousemove",this.onWindowMouseMove,{passive:!0}),this.window.addEventListener("mouseup",this.onWindowMouseUp,{passive:!0}))});i(this,"onWindowMouseMove",e=>{if(!this.pointInsideVerifier.verify(e.clientX,e.clientY)){this.removeWindowMouseListeners(),this.params.onPointerOutside();return}this.params.onPointerMove({x:e.clientX,y:e.clientY})});i(this,"onWindowMouseUp",e=>{this.params.mouseUpEventVerifier(e)&&(this.removeWindowMouseListeners(),this.params.onPointerUp({x:e.clientX,y:e.clientY}))});i(this,"onPortTouchStart",e=>{const t=e;if(t.touches.length!==1)return;const o=t.touches[0],s=e.currentTarget,n=this.canvas.graph.findPortIdsByElement(s)[0];this.params.onPointerDownVerifier(n,{x:o.clientX,y:o.clientY})&&(e.stopPropagation(),this.window.addEventListener("touchmove",this.onWindowTouchMove,{passive:!0}),this.window.addEventListener("touchend",this.onWindowTouchFinish,{passive:!0}),this.window.addEventListener("touchcancel",this.onWindowTouchFinish,{passive:!0}))});i(this,"onWindowTouchMove",e=>{const t=e.touches[0];if(!this.pointInsideVerifier.verify(t.clientX,t.clientY)){this.removeWindowTouchListeners(),this.params.onPointerOutside();return}this.params.onPointerMove({x:t.clientX,y:t.clientY})});i(this,"onWindowTouchFinish",e=>{this.removeWindowTouchListeners();const t=e.changedTouches[0];this.params.onPointerUp({x:t.clientX,y:t.clientY})});i(this,"reset",()=>{this.canvas.graph.getAllPortIds().forEach(e=>{const t=this.canvas.graph.getPort(e);this.unhookPortEvents(t.element)})});i(this,"revert",()=>{this.removeWindowMouseListeners(),this.removeWindowTouchListeners(),this.reset()});this.canvas=e,this.window=t,this.pointInsideVerifier=o,this.params=s,this.canvas.graph.onAfterPortMarked.subscribe(this.onAfterPortMarked),this.canvas.graph.onBeforePortUnmarked.subscribe(this.onBeforePortUnmarked),this.canvas.graph.onBeforeClear.subscribe(this.reset),this.canvas.onBeforeDestroy.subscribe(this.revert)}static configure(e,t,o,s){new j(e,t,o,s)}hookPortEvents(e){e.addEventListener("mousedown",this.onPortMouseDown,{passive:!0}),e.addEventListener("touchstart",this.onPortTouchStart,{passive:!0})}unhookPortEvents(e){e.removeEventListener("mousedown",this.onPortMouseDown),e.removeEventListener("touchstart",this.onPortTouchStart)}removeWindowMouseListeners(){this.window.removeEventListener("mousemove",this.onWindowMouseMove),this.window.removeEventListener("mouseup",this.onWindowMouseUp)}removeWindowTouchListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}}class Jt{constructor(){i(this,"field","_htmlGraphTags")}has(e,t){const s=e[this.field];return s!==void 0&&s.has(t)}tag(e,t){const o=e,s=o[this.field];s===void 0?o[this.field]=new Set([t]):s.add(t)}}const ie=Symbol();class ne{constructor(e,t,o){i(this,"onSelected");i(this,"mouseDownEventVerifier");i(this,"mouseUpEventVerifier");i(this,"movementThreshold");i(this,"selectionCandidate",null);i(this,"previousMouse",null);i(this,"previousTouch",null);i(this,"movedDistance",0);i(this,"onElementMouseDown",e=>{const t=e;this.mouseDownEventVerifier(t)&&(this.selectionCandidate=t.currentTarget,this.previousMouse={x:t.clientX,y:t.clientY},this.movedDistance=0,this.window.addEventListener("mousemove",this.onWindowMouseMove,{passive:!0}),this.window.addEventListener("mouseup",this.onWindowMouseUp,{passive:!0}))});i(this,"onElementTouchStart",e=>{const t=e,o=t.touches;o.length===1&&(this.selectionCandidate=t.currentTarget,this.previousTouch=o[0],this.movedDistance=0,this.window.addEventListener("touchmove",this.onWindowTouchMove,{passive:!0}),this.window.addEventListener("touchend",this.onWindowTouchEnd,{passive:!0}),this.window.addEventListener("touchcancel",this.onWindowTouchCancel,{passive:!0}))});i(this,"onWindowMouseMove",e=>{const t=e;if(!this.pointInsideVerifier.verify(t.clientX,t.clientY)){this.removeWindowMouseListeners();return}const s=this.previousMouse,n=t.clientX-s.x,a=t.clientY-s.y;this.previousMouse={x:t.clientX,y:t.clientY},this.processMoveThresholdVerification(n,a,()=>{this.removeWindowMouseListeners()})});i(this,"onWindowMouseUp",e=>{const t=e;this.mouseUpEventVerifier(t)&&(this.removeWindowMouseListeners(),this.onSelected(this.selectionCandidate,e))});i(this,"onWindowTouchMove",e=>{const o=e.touches;if(o.length!==1){this.removeWindowTouchListeners();return}const s=o[0];if(!this.pointInsideVerifier.verify(s.clientX,s.clientY)){this.removeWindowTouchListeners();return}const a=this.previousTouch,h=s.clientX-a.clientX,d=s.clientY-a.clientY;this.previousTouch=s,this.processMoveThresholdVerification(h,d,()=>{this.removeWindowTouchListeners()})});i(this,"onWindowTouchEnd",e=>{this.removeWindowTouchListeners(),this.onSelected(this.selectionCandidate,e)});i(this,"onWindowTouchCancel",()=>{this.removeWindowTouchListeners()});this.window=e,this.pointInsideVerifier=t,this.mouseDownEventVerifier=o.mouseDownEventVerifier,this.mouseUpEventVerifier=o.mouseUpEventVerifier,this.onSelected=o.onSelected,this.movementThreshold=o.movementThreshold}enable(e){e.addEventListener("mousedown",this.onElementMouseDown),e.addEventListener("touchstart",this.onElementTouchStart)}disable(e){e.removeEventListener("mousedown",this.onElementMouseDown),e.removeEventListener("touchstart",this.onElementTouchStart),e===this.selectionCandidate&&(this.removeWindowMouseListeners(),this.removeWindowTouchListeners())}removeWindowMouseListeners(){this.window.removeEventListener("mousemove",this.onWindowMouseMove),this.window.removeEventListener("mouseup",this.onWindowMouseUp)}removeWindowTouchListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchEnd),this.window.removeEventListener("touchcancel",this.onWindowTouchCancel)}processMoveThresholdVerification(e,t,o){const s=Math.sqrt(e*e+t*t);this.movedDistance+=s,this.movedDistance>this.movementThreshold&&o()}}const ae=(r,e)=>({from:r.isDirect?r.staticPortId:e,to:r.isDirect?e:r.staticPortId});class et{constructor(e,t){this.graph=e,this.connectionAllowedVerifier=t}resolve(e){let t,o=1/0;const{cursor:s}=e;return this.graph.getAllPortIds().forEach(n=>{const{element:a,direction:h,nodeId:d}=this.graph.getPort(n),{x:c,y:l}=this.graph.getNode(d);if(c===null||l===null)return;const u=ae(e,n);if(!this.connectionAllowedVerifier(u))return;const{top:g,left:p,width:w,height:f}=a.getBoundingClientRect(),y={x:p+w/2,y:g+f/2},E=s.x-y.x,x=s.y-y.y,C=Math.sqrt(E*E+x*x);C<o&&(o=C,t=h)}),t}}class Kt{constructor(e){this.direction=e}resolve(){return this.direction}}class he{constructor(e,t,o,s,n){i(this,"grabbedNode",null);i(this,"maxNodePriority",0);i(this,"graph");i(this,"onAfterNodeAdded",e=>{this.updateMaxNodePriority(e);const{element:t}=this.graph.getNode(e);t.addEventListener("mousedown",this.onMouseDown,{passive:!0}),t.addEventListener("touchstart",this.onTouchStart,{passive:!0})});i(this,"onAfterNodeUpdated",e=>{this.updateMaxNodePriority(e)});i(this,"onBeforeNodeRemoved",e=>{const{element:t}=this.graph.getNode(e);t.removeEventListener("mousedown",this.onMouseDown),t.removeEventListener("touchstart",this.onTouchStart)});i(this,"onMouseDown",e=>{const t=e;if(!this.params.mouseDownEventVerifier(t))return;const o=e.currentTarget,s=this.graph.findNodeIdByElement(o),n=this.graph.getNode(s);if(!this.params.nodeDragVerifier(s))return;this.params.onNodeDragStarted(s),e.stopPropagation();const h=this.calculateContentPoint({x:t.clientX,y:t.clientY});this.grabbedNode={nodeId:s,dx:h.x-n.x,dy:h.y-n.y},H(this.element,this.params.dragCursor),this.moveNodeOnTop(s),this.window.addEventListener("mousemove",this.onWindowMouseMove,{passive:!0}),this.window.addEventListener("mouseup",this.onWindowMouseUp,{passive:!0})});i(this,"onTouchStart",e=>{const t=e;if(t.touches.length!==1)return;e.stopPropagation();const o=t.touches[0],s=e.currentTarget,n=this.canvas.graph.findNodeIdByElement(s),a=this.graph.getNode(n);if(!this.params.nodeDragVerifier({nodeId:n,element:a.element,x:a.x,y:a.y}))return;const d=this.calculateContentPoint({x:o.clientX,y:o.clientY});this.grabbedNode={nodeId:n,dx:d.x-a.x,dy:d.y-a.y},this.moveNodeOnTop(n),this.window.addEventListener("touchmove",this.onWindowTouchMove,{passive:!0}),this.window.addEventListener("touchend",this.onWindowTouchFinish,{passive:!0}),this.window.addEventListener("touchcancel",this.onWindowTouchFinish,{passive:!0})});i(this,"onWindowMouseMove",e=>{if(!this.pointInsideVerifier.verify(e.clientX,e.clientY)){this.stopMouseDrag();return}this.grabbedNode!==null&&this.moveNode(this.grabbedNode,{x:e.clientX,y:e.clientY})});i(this,"onWindowMouseUp",e=>{this.params.mouseUpEventVerifier(e)&&this.stopMouseDrag()});i(this,"onWindowTouchMove",e=>{if(e.touches.length!==1)return;const t=e.touches[0];if(!this.pointInsideVerifier.verify(t.clientX,t.clientY)){this.stopTouchDrag();return}this.grabbedNode!==null&&this.moveNode(this.grabbedNode,{x:t.clientX,y:t.clientY})});i(this,"onWindowTouchFinish",()=>{this.stopTouchDrag()});i(this,"reset",()=>{this.canvas.graph.getAllNodeIds().forEach(e=>{const{element:t}=this.canvas.graph.getNode(e);t.removeEventListener("mousedown",this.onMouseDown),t.removeEventListener("touchstart",this.onTouchStart)}),this.maxNodePriority=0});i(this,"revert",()=>{this.reset(),this.removeMouseDragListeners(),this.removeTouchDragListeners()});this.canvas=e,this.element=t,this.window=o,this.pointInsideVerifier=s,this.params=n,this.graph=e.graph,this.graph.onAfterNodeAdded.subscribe(this.onAfterNodeAdded),this.graph.onAfterNodeUpdated.subscribe(this.onAfterNodeUpdated),this.graph.onBeforeNodeRemoved.subscribe(this.onBeforeNodeRemoved),this.graph.onBeforeClear.subscribe(this.reset),this.canvas.onBeforeDestroy.subscribe(this.revert)}static configure(e,t,o,s,n){new he(e,t,o,s,n)}moveNode(e,t){if(!this.graph.hasNode(e.nodeId))return;const o=this.calculateContentPoint(t),s={x:o.x-e.dx,y:o.y-e.dy},n=this.adjustNodeCoords(s);this.canvas.updateNode(e.nodeId,{x:n.x,y:n.y}),this.params.onNodeDrag(e.nodeId)}moveNodeOnTop(e){if(this.params.moveOnTop){if(this.maxNodePriority++,this.params.moveEdgesOnTop){const t=this.maxNodePriority;this.maxNodePriority++,this.graph.getNodeAdjacentEdgeIds(e).forEach(s=>{this.canvas.updateEdge(s,{priority:t})})}this.canvas.updateNode(e,{priority:this.maxNodePriority})}}stopMouseDrag(){this.grabbedNode!==null&&this.graph.hasNode(this.grabbedNode.nodeId)&&this.params.onNodeDragFinished(this.grabbedNode.nodeId),this.grabbedNode=null,H(this.element,null),this.removeMouseDragListeners()}removeMouseDragListeners(){this.window.removeEventListener("mouseup",this.onWindowMouseUp),this.window.removeEventListener("mousemove",this.onWindowMouseMove)}stopTouchDrag(){if(this.grabbedNode!==null&&this.graph.hasNode(this.grabbedNode.nodeId)){const e=this.graph.getNode(this.grabbedNode.nodeId);this.params.onNodeDragFinished({nodeId:this.grabbedNode.nodeId,element:e.element,x:e.x,y:e.y})}this.grabbedNode=null,this.removeTouchDragListeners()}removeTouchDragListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}updateMaxNodePriority(e){const t=this.graph.getNode(e).priority;this.maxNodePriority=Math.max(this.maxNodePriority,t)}calculateContentPoint(e){const t=this.element.getBoundingClientRect();return this.canvas.viewport.createContentCoords({x:e.x-t.x,y:e.y-t.y})}adjustNodeCoords(e){const t=this.params.gridSize;if(t!==null){const o=t/2;return{x:Math.floor((e.x+o)/t)*t,y:Math.floor((e.y+o)/t)*t}}return e}}const J=r=>{const e=[],t=r.touches.length;for(let h=0;h<t;h++)e.push([r.touches[h].clientX,r.touches[h].clientY]);const o=e.reduce((h,d)=>[h[0]+d[0],h[1]+d[1]],[0,0]),s=[o[0]/t,o[1]/t],a=e.map(h=>[h[0]-s[0],h[1]-s[1]]).reduce((h,d)=>h+Math.sqrt(d[0]*d[0]+d[1]*d[1]),0);return{x:s[0],y:s[1],scale:a/t,touchesCnt:t,touches:e}};class K{constructor(e,t,o,s,n){i(this,"viewport");i(this,"prevTouches",null);i(this,"wheelFinishTimer",null);i(this,"transformInProgress",!1);i(this,"revert",()=>{this.removeMouseDragListeners(),this.removeTouchDragListeners()});i(this,"onMouseDown",e=>{this.params.mouseDownEventVerifier(e)&&(H(this.element,this.params.shiftCursor),this.window.addEventListener("mousemove",this.onWindowMouseMove,{passive:!0}),this.window.addEventListener("mouseup",this.onWindowMouseUp,{passive:!0}),this.startRegisteredTransform())});i(this,"onWindowMouseMove",e=>{const t=this.pointInsideVerifier.verify(e.clientX,e.clientY);if(this.element===null||!t){this.stopMouseDrag();return}const o=-e.movementX,s=-e.movementY;this.moveViewport(o,s)});i(this,"onWindowMouseUp",e=>{this.params.mouseUpEventVerifier(e)&&this.stopMouseDrag()});i(this,"onWheelScroll",e=>{if(!this.params.mouseWheelEventVerifier(e))return;const{left:t,top:o}=this.element.getBoundingClientRect(),s=e.clientX-t,n=e.clientY-o,h=1/(e.deltaY<0?this.params.wheelSensitivity:1/this.params.wheelSensitivity);this.wheelFinishTimer===null&&this.params.onTransformStarted(),this.scaleViewport(h,s,n),this.wheelFinishTimer!==null&&clearTimeout(this.wheelFinishTimer),this.wheelFinishTimer=setTimeout(()=>{this.transformInProgress||this.params.onTransformFinished(),this.wheelFinishTimer=null},this.params.scaleWheelFinishTimeout)});i(this,"onTouchStart",e=>{if(this.prevTouches!==null){this.prevTouches=J(e);return}this.prevTouches=J(e),this.window.addEventListener("touchmove",this.onWindowTouchMove,{passive:!0}),this.window.addEventListener("touchend",this.onWindowTouchFinish,{passive:!0}),this.window.addEventListener("touchcancel",this.onWindowTouchFinish,{passive:!0}),this.startRegisteredTransform()});i(this,"onWindowTouchMove",e=>{const t=J(e);if(!t.touches.every(s=>this.pointInsideVerifier.verify(s[0],s[1]))){this.stopTouchDrag();return}if((t.touchesCnt===1||t.touchesCnt===2)&&this.moveViewport(-(t.x-this.prevTouches.x),-(t.y-this.prevTouches.y)),t.touchesCnt===2){const{left:s,top:n}=this.element.getBoundingClientRect(),a=this.prevTouches.x-s,h=this.prevTouches.y-n,c=1/(t.scale/this.prevTouches.scale);this.scaleViewport(c,a,h)}this.prevTouches=t});i(this,"onWindowTouchFinish",e=>{e.touches.length>0?this.prevTouches=J(e):this.stopTouchDrag()});i(this,"preventWheelScaleListener",e=>{e.preventDefault()});this.canvas=e,this.element=t,this.window=o,this.pointInsideVerifier=s,this.params=n,this.element.addEventListener("wheel",this.preventWheelScaleListener,{passive:!1}),this.viewport=e.viewport,this.handleResize(),this.viewport.onAfterResize.subscribe(()=>{this.handleResize()}),this.element.addEventListener("mousedown",this.onMouseDown,{passive:!0}),this.element.addEventListener("wheel",this.onWheelScroll,{passive:!0}),this.element.addEventListener("touchstart",this.onTouchStart,{passive:!0}),e.onBeforeDestroy.subscribe(this.revert)}static configure(e,t,o,s,n){new K(e,t,o,s,n)}moveViewport(e,t){const o=this.viewport.getViewportMatrix(),s=Qe(o,e,t),{width:n,height:a}=this.viewport.getDimensions(),h=this.params.transformPreprocessor({prevTransform:o,nextTransform:s,canvasWidth:n,canvasHeight:a});this.performTransform(h)}scaleViewport(e,t,o){const s=this.canvas.viewport.getViewportMatrix(),n=Ze(s,e,t,o),{width:a,height:h}=this.viewport.getDimensions(),d=this.params.transformPreprocessor({prevTransform:s,nextTransform:n,canvasWidth:a,canvasHeight:h});this.performTransform(d)}stopMouseDrag(){H(this.element,null),this.removeMouseDragListeners(),this.finishRegisteredTransform()}removeMouseDragListeners(){this.window.removeEventListener("mousemove",this.onWindowMouseMove),this.window.removeEventListener("mouseup",this.onWindowMouseUp)}stopTouchDrag(){this.prevTouches=null,this.removeTouchDragListeners(),this.finishRegisteredTransform()}removeTouchDragListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}performTransform(e){this.params.onBeforeTransformChange(),this.canvas.patchViewportMatrix(e),this.params.onTransformChange()}startRegisteredTransform(){this.transformInProgress=!0,this.params.onTransformStarted()}finishRegisteredTransform(){this.transformInProgress=!1,this.params.onTransformFinished()}handleResize(){const e=this.viewport.getViewportMatrix(),{width:t,height:o}=this.viewport.getDimensions(),s=this.params.transformPreprocessor({prevTransform:e,nextTransform:e,canvasWidth:t,canvasHeight:o});this.params.onResizeTransformStarted(),this.canvas.patchViewportMatrix(s),this.params.onResizeTransformFinished()}}class de{constructor(e,t,o,s,n,a,h){i(this,"nodeHorizontal");i(this,"nodeVertical");i(this,"viewport");i(this,"currentScale");i(this,"loadedArea",{xFrom:1/0,xTo:1/0,yFrom:1/0,yTo:1/0});i(this,"updateLoadedArea",e=>{this.loadedArea={xFrom:e.x,xTo:e.x+e.width,yFrom:e.y,yTo:e.y+e.height}});i(this,"onAfterViewportUpdated",()=>{this.userTransformInProgress||this.loadAreaAroundViewport()});i(this,"userTransformInProgress",!1);this.canvas=e,this.element=t,this.window=o,this.trigger=n,this.pointInsideVerifier=a,this.params=h,this.nodeHorizontal=this.params.nodeVerticalRadius,this.nodeVertical=this.params.nodeHorizontalRadius,this.viewport=e.viewport,this.currentScale=this.viewport.getViewportMatrix().scale,this.scheduleLoadAreaAroundViewport(),this.viewport.onAfterResize.subscribe(()=>{this.scheduleLoadAreaAroundViewport()});const d={...s,onResizeTransformStarted:()=>{this.userTransformInProgress=!0,s.onResizeTransformStarted()},onResizeTransformFinished:()=>{this.userTransformInProgress=!1,s.onResizeTransformFinished()},onBeforeTransformChange:()=>{this.userTransformInProgress=!0,s.onBeforeTransformChange()},onTransformChange:()=>{this.userTransformInProgress=!1;const c=this.currentScale;this.currentScale=this.viewport.getViewportMatrix().scale,c!==this.currentScale&&this.scheduleEnsureViewportAreaLoaded(),s.onTransformChange()},onTransformFinished:()=>{this.scheduleLoadAreaAroundViewport(),s.onTransformFinished()}};K.configure(e,this.element,this.window,this.pointInsideVerifier,d),this.trigger.subscribe(this.updateLoadedArea),this.canvas.viewport.onAfterUpdated.subscribe(this.onAfterViewportUpdated)}static configure(e,t,o,s,n,a,h){new de(e,t,o,s,n,a,h)}scheduleLoadAreaAroundViewport(){setTimeout(()=>{this.loadAreaAroundViewport()})}scheduleEnsureViewportAreaLoaded(){setTimeout(()=>{const{width:e,height:t}=this.viewport.getDimensions(),{scale:o,x:s,y:n}=this.viewport.getViewportMatrix(),a=e*o,h=t*o,d=s-this.nodeHorizontal,c=n-this.nodeVertical,l=s+a+this.nodeHorizontal,u=n+h+this.nodeVertical;this.loadedArea.xFrom<d&&this.loadedArea.xTo>l&&this.loadedArea.yFrom<c&&this.loadedArea.yTo>u||this.loadAreaAroundViewport()})}loadAreaAroundViewport(){const{width:e,height:t}=this.viewport.getDimensions(),{scale:o,x:s,y:n}=this.viewport.getViewportMatrix(),a=e*o,h=t*o,d=s-a-this.nodeHorizontal,c=n-h-this.nodeVertical,l=3*a+2*this.nodeHorizontal,u=3*h+2*this.nodeVertical;this.trigger.emit({x:d,y:c,width:l,height:u})}}const Qt=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","svg");return r.style.position="absolute",r.style.inset="0",r},Zt=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","rect");return r.setAttribute("fill","url(#pattern)"),r},_t=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","pattern");return r.setAttribute("id","pattern"),r};class ce{constructor(e,t,o){i(this,"svg",Qt());i(this,"patternRenderingRectangle",Zt());i(this,"pattern",_t());i(this,"patternContent");i(this,"tileWidth");i(this,"tileHeight");i(this,"halfTileWidth");i(this,"halfTileHeight");i(this,"maxViewportScale");i(this,"visible",!1);i(this,"onAfterTransformUpdated",()=>{const e=this.canvas.viewport.getContentMatrix(),t=e.x-this.halfTileWidth*e.scale,o=e.y-this.halfTileHeight*e.scale,s=`matrix(${e.scale}, 0, 0, ${e.scale}, ${t}, ${o})`;this.pattern.setAttribute("patternTransform",s),this.updateVisibility()});this.canvas=e,this.backgroundHost=o,this.tileWidth=t.tileWidth,this.tileHeight=t.tileHeight,this.halfTileWidth=this.tileWidth/2,this.halfTileHeight=this.tileHeight/2,this.patternContent=t.renderer,this.maxViewportScale=t.maxViewportScale;const s=`translate(${this.halfTileWidth}, ${this.halfTileHeight})`;this.patternContent.setAttribute("transform",s),this.pattern.appendChild(this.patternContent);const n=document.createElementNS("http://www.w3.org/2000/svg","defs");n.appendChild(this.pattern),this.svg.appendChild(n),this.svg.appendChild(this.patternRenderingRectangle),this.updateDimensions(),this.canvas.viewport.onAfterResize.subscribe(()=>{this.updateDimensions()}),this.canvas.viewport.onAfterUpdated.subscribe(this.onAfterTransformUpdated),this.onAfterTransformUpdated()}static configure(e,t,o){new ce(e,t,o)}updateVisibility(){const{scale:e}=this.canvas.viewport.getViewportMatrix(),t=e>this.maxViewportScale;t&&this.visible?(this.visible=!1,this.backgroundHost.removeChild(this.svg)):!t&&!this.visible&&(this.visible=!0,this.backgroundHost.appendChild(this.svg))}updateDimensions(){const{width:e,height:t}=this.canvas.viewport.getDimensions();if(this.svg.setAttribute("width",`${e}`),this.svg.setAttribute("height",`${t}`),this.patternRenderingRectangle.setAttribute("width",`${e}`),this.patternRenderingRectangle.setAttribute("height",`${t}`),e>0&&t>0){const o=this.tileWidth/e,s=this.tileHeight/t;this.pattern.setAttribute("width",`${o}`),this.pattern.setAttribute("height",`${s}`)}}}class le{constructor(e,t,o,s,n,a){i(this,"overlayCanvas");i(this,"edgeInProgress",null);i(this,"onEdgeCreated",e=>{this.params.onAfterEdgeCreated(e)});this.canvas=e,this.overlayLayer=t,this.viewportStore=o,this.window=s,this.pointInsideVerifier=n,this.params=a,this.overlayCanvas=qe(this.overlayLayer,this.viewportStore,this.window);const h={mouseDownEventVerifier:this.params.mouseDownEventVerifier,mouseUpEventVerifier:this.params.mouseUpEventVerifier,onPointerDownVerifier:(d,c)=>{const l=this.params.connectionTypeResolver(d);return l===null?!1:(this.grabPort(d,c,l),!0)},onPointerMove:d=>{this.moveDraggingPort(d)},onPointerUp:d=>{this.tryCreateConnection(d),this.resetDragState()},onPointerOutside:()=>{const d=this.edgeInProgress;this.resetDragState(),this.params.onEdgeCreationInterrupted(d)}};j.configure(this.canvas,this.window,this.pointInsideVerifier,h)}static configure(e,t,o,s,n,a){new le(e,t,o,s,n,a)}grabPort(e,t,o){const s=this.canvas.graph.getPort(e),n=s.element.getBoundingClientRect(),a=n.x+n.width/2,h=n.y+n.height/2,d=this.overlayLayer.getBoundingClientRect(),c=this.canvas.viewport.createContentCoords({x:a-d.x,y:h-d.y}),l=this.canvas.viewport.createContentCoords({x:t.x-d.x,y:t.y-d.y}),u={overlayNodeId:R.StaticNodeId,portCoords:c,portDirection:s.direction},g=o==="direct";this.edgeInProgress={staticPortId:e,isDirect:g};const p=this.params.draggingPortDirectionResolver.resolve({cursor:t,...this.edgeInProgress}),w={overlayNodeId:R.DraggingNodeId,portCoords:l,portDirection:p??s.direction},[f,y]=g?[u,w]:[w,u];this.overlayCanvas.addNode(Y(f)),this.overlayCanvas.addNode(Y(y)),this.overlayCanvas.addEdge({from:f.overlayNodeId,to:y.overlayNodeId,shape:this.params.edgeShapeFactory(R.EdgeId)})}resetDragState(){this.edgeInProgress=null,this.overlayCanvas.clear()}tryCreateConnection(e){const t=De(this.canvas.graph,e);if(t===null){this.params.onEdgeCreationInterrupted(this.edgeInProgress);return}const o=ae(this.edgeInProgress,t),s=this.params.connectionAllowedVerifier(o),n=this.params.connectionPreprocessor({from:o.from,to:o.to});s&&n!==null?(this.canvas.graph.onAfterEdgeAdded.subscribe(this.onEdgeCreated),this.canvas.addEdge(n),this.canvas.graph.onAfterEdgeAdded.unsubscribe(this.onEdgeCreated)):this.params.onEdgeCreationPrevented(o)}moveDraggingPort(e){const t=this.overlayLayer.getBoundingClientRect(),o=this.canvas.viewport.createContentCoords({x:e.x-t.x,y:e.y-t.y}),s=this.params.draggingPortDirectionResolver.resolve({cursor:e,...this.edgeInProgress});this.overlayCanvas.updateNode(R.DraggingNodeId,{x:o.x,y:o.y}).updatePort(R.DraggingNodeId,{direction:s})}}class ue{constructor(e,t,o,s,n,a){i(this,"overlayCanvas");i(this,"edgeInProgress",null);i(this,"draggingEdgePayload",null);i(this,"onEdgeReattached",e=>{this.params.onAfterEdgeReattached(e)});this.canvas=e,this.overlayLayer=t,this.viewportStore=o,this.window=s,this.pointInsideVerifier=n,this.params=a,this.overlayCanvas=qe(this.overlayLayer,this.viewportStore,this.window);const h={mouseDownEventVerifier:this.params.mouseDownEventVerifier,mouseUpEventVerifier:this.params.mouseUpEventVerifier,onPointerDownVerifier:(d,c)=>this.tryStartEdgeDragging(d,c),onPointerMove:d=>{this.moveDraggingPort(d)},onPointerUp:d=>{this.tryCreateConnection(d),this.resetDragState()},onPointerOutside:()=>{const d=this.draggingEdgePayload;this.resetDragState(),this.params.onEdgeReattachInterrupted({id:d.id,from:d.from,to:d.to,shape:d.shape,priority:d.priority})}};j.configure(this.canvas,this.window,this.pointInsideVerifier,h)}static configure(e,t,o,s,n,a){new ue(e,t,o,s,n,a)}tryStartEdgeDragging(e,t){const o=this.params.draggingEdgeResolver(e);if(o===null)return!1;const s=this.canvas.graph.getEdge(o),n=e===s.from,a=e===s.to,h=n?s.to:s.from;this.edgeInProgress={staticPortId:h,isDirect:a};const d=this.canvas.graph.getPort(e),c=this.canvas.graph.getPort(h),l=c.element.getBoundingClientRect(),u={x:l.x+l.width/2,y:l.y+l.height/2},g=this.overlayLayer.getBoundingClientRect(),p=this.canvas.viewport.createContentCoords({x:u.x-g.x,y:u.y-g.y}),w=this.canvas.viewport.createContentCoords({x:t.x-g.x,y:t.y-g.y});this.draggingEdgePayload={id:o,from:s.from,to:s.to,shape:s.shape,priority:s.priority},this.canvas.removeEdge(o);const f={overlayNodeId:R.StaticNodeId,portCoords:p,portDirection:c.direction},y=this.params.draggingPortDirectionResolver.resolve({cursor:t,...this.edgeInProgress}),E={overlayNodeId:R.DraggingNodeId,portCoords:w,portDirection:y??d.direction},[x,C]=a?[f,E]:[E,f];this.overlayCanvas.addNode(Y(x)),this.overlayCanvas.addNode(Y(C));const D=this.params.draggingEdgeShapeFactory!==null?this.params.draggingEdgeShapeFactory(R.EdgeId):s.shape;return this.overlayCanvas.addEdge({id:R.EdgeId,from:x.overlayNodeId,to:C.overlayNodeId,shape:D}),!0}resetDragState(){this.draggingEdgePayload=null,this.edgeInProgress=null,this.overlayCanvas.clear()}moveDraggingPort(e){const t=this.overlayLayer.getBoundingClientRect(),o={x:e.x-t.x,y:e.y-t.y},s=this.canvas.viewport.createContentCoords(o),n=this.params.draggingPortDirectionResolver.resolve({cursor:e,...this.edgeInProgress});this.overlayCanvas.updateNode(R.DraggingNodeId,{x:s.x,y:s.y}).updatePort(R.DraggingNodeId,{direction:n})}tryCreateConnection(e){const t=De(this.canvas.graph,e);if(this.overlayCanvas.removeEdge(R.EdgeId),t===null){const c=this.draggingEdgePayload;this.params.onEdgeReattachInterrupted({id:c.id,from:c.from,to:c.to,shape:c.shape,priority:c.priority});return}const{from:o,to:s}=ae(this.edgeInProgress,t),n=this.draggingEdgePayload,a={id:n.id,from:o,to:s,shape:n.shape,priority:n.priority},h=this.params.connectionAllowedVerifier({from:o,to:s}),d=this.params.connectionPreprocessor(a);if(h&&d!==null)this.canvas.graph.onAfterEdgeAdded.subscribe(this.onEdgeReattached),this.canvas.addEdge(d),this.canvas.graph.onAfterEdgeAdded.unsubscribe(this.onEdgeReattached);else{const c=this.draggingEdgePayload;this.params.onEdgeReattachPrevented({id:c.id,from:c.from,to:c.to,shape:c.shape,priority:c.priority})}}}class ge{constructor(e,t){this.applier=e,this.trigger=t,this.trigger.subscribe(()=>{this.applier.apply()})}static configure(e,t){new ge(e,t)}}class pe{constructor(e,t,o){i(this,"applyScheduled",!1);i(this,"apply",()=>{this.applyScheduled=!1,this.applier.apply()});this.graph=e,this.applier=t,this.defererFn=o,this.graph.onAfterNodeAdded.subscribe(()=>{this.scheduleApply()}),this.graph.onBeforeNodeRemoved.subscribe(()=>{this.scheduleApply()}),this.graph.onAfterEdgeAdded.subscribe(()=>{this.scheduleApply()}),this.graph.onBeforeEdgeRemoved.subscribe(()=>{this.scheduleApply()})}static configure(e,t,o){new pe(e,t,o)}scheduleApply(){this.applyScheduled||(this.applyScheduled=!0,this.defererFn(this.apply))}}class qt{constructor(e,t,o){this.canvas=e,this.layoutAlgorithm=t,this.params=o}apply(){const e=this.layoutAlgorithm.calculateCoordinates({graph:this.canvas.graph,viewport:this.canvas.viewport});this.params.onBeforeApplied(),e.forEach((t,o)=>{this.params.staticNodeResolver(o)||this.canvas.updateNode(o,t)}),this.params.onAfterApplied()}}class er{static configure(e,t){const o=t.applyOn,s=new qt(e,t.algorithm,{staticNodeResolver:t.staticNodeResolver,onBeforeApplied:t.onBeforeApplied,onAfterApplied:t.onAfterApplied});switch(o.type){case"trigger":{ge.configure(s,o.trigger);break}case"topologyChangeSchedule":{pe.configure(e.graph,s,o.schedule);break}}}}class tr{constructor(e,t){i(this,"previousTimeStamp");i(this,"step",e=>{if(this.previousTimeStamp===void 0)this.previousTimeStamp=e;else{const t=(e-this.previousTimeStamp)/1e3;this.previousTimeStamp=e,this.callback(t)}this.win.requestAnimationFrame(this.step)});this.win=e,this.callback=t,this.win.requestAnimationFrame(this.step)}}class rr{constructor(e,t,o){this.canvas=e,this.layoutAlgorithm=t,this.params=o}apply(e){const t=this.layoutAlgorithm.calculateNextCoordinates({graph:this.canvas.graph,viewport:this.canvas.viewport,dt:e});this.params.onBeforeApplied(),t.forEach((o,s)=>{this.params.staticNodeResolver(s)||this.canvas.updateNode(s,o)}),this.params.onAfterApplied()}}class we{constructor(e,t,o){i(this,"applier");i(this,"step",e=>{this.applier.apply(e)});this.win=o,this.applier=new rr(e,t.algorithm,{staticNodeResolver:t.staticNodeResolver,onBeforeApplied:t.onBeforeApplied,onAfterApplied:t.onAfterApplied}),new tr(this.win,this.step)}static configure(e,t,o){new we(e,t,o)}}class fe{constructor(e,t,o,s,n){i(this,"configurator");i(this,"onNodeSelected");i(this,"selectionHandledTag",ie);i(this,"onAfterNodeAdded",e=>{const{element:t}=this.canvas.graph.getNode(e);this.configurator.enable(t)});i(this,"onBeforeNodeRemoved",e=>{const{element:t}=this.canvas.graph.getNode(e);this.configurator.disable(t)});i(this,"reset",()=>{this.canvas.graph.getAllNodeIds().forEach(e=>{const{element:t}=this.canvas.graph.getNode(e);this.configurator.disable(t)})});this.canvas=e,this.window=t,this.pointInsideVerifier=o,this.eventTagger=s,this.onNodeSelected=n.onNodeSelected,this.configurator=new ne(this.window,this.pointInsideVerifier,{onSelected:(a,h)=>{const d=this.canvas.graph.findNodeIdByElement(a);this.eventTagger.tag(h,this.selectionHandledTag),this.onNodeSelected(d)},movementThreshold:n.movementThreshold,mouseDownEventVerifier:n.mouseDownEventVerifier,mouseUpEventVerifier:n.mouseUpEventVerifier}),this.canvas.graph.onAfterNodeAdded.subscribe(this.onAfterNodeAdded),this.canvas.graph.onBeforeNodeRemoved.subscribe(this.onBeforeNodeRemoved),this.canvas.graph.onBeforeClear.subscribe(this.reset),this.canvas.onBeforeDestroy.subscribe(this.reset)}static configure(e,t,o,s,n){new fe(e,t,o,s,n)}}class ye{constructor(e,t,o,s,n){i(this,"configurator");i(this,"selectionHandledTag",ie);i(this,"onEdgeSelected");i(this,"onAfterEdgeAdded",e=>{const{shape:t}=this.canvas.graph.getEdge(e);this.configurator.enable(t.svg)});i(this,"onBeforeEdgeRemoved",e=>{const{shape:t}=this.canvas.graph.getEdge(e);this.configurator.disable(t.svg)});i(this,"reset",()=>{this.canvas.graph.getAllEdgeIds().forEach(e=>{const{shape:t}=this.canvas.graph.getEdge(e);this.configurator.disable(t.svg)})});this.canvas=e,this.window=t,this.pointInsideVerifier=o,this.eventTagger=s,this.onEdgeSelected=n.onEdgeSelected,this.configurator=new ne(this.window,this.pointInsideVerifier,{onSelected:(a,h)=>{const d=this.canvas.graph.findEdgeIdByElement(a);this.eventTagger.tag(h,this.selectionHandledTag),this.onEdgeSelected(d)},movementThreshold:n.movementThreshold,mouseDownEventVerifier:n.mouseDownEventVerifier,mouseUpEventVerifier:n.mouseUpEventVerifier}),this.canvas.graph.onAfterEdgeAdded.subscribe(this.onAfterEdgeAdded),this.canvas.graph.onBeforeEdgeRemoved.subscribe(this.onBeforeEdgeRemoved),this.canvas.graph.onBeforeClear.subscribe(this.reset),this.canvas.onBeforeDestroy.subscribe(this.reset)}static configure(e,t,o,s,n){new ye(e,t,o,s,n)}}class ve{constructor(e,t,o,s,n,a){i(this,"configurator");i(this,"onCanvasSelected");i(this,"selectionHandledTag",ie);this.canvas=e,this.element=t,this.window=o,this.pointInsideVerifier=s,this.eventTagger=n,this.onCanvasSelected=a.onCanvasSelected,this.configurator=new ne(this.window,this.pointInsideVerifier,{onSelected:(h,d)=>{this.eventTagger.has(d,this.selectionHandledTag)||this.onCanvasSelected()},movementThreshold:a.movementThreshold,mouseDownEventVerifier:a.mouseDownEventVerifier,mouseUpEventVerifier:a.mouseUpEventVerifier}),this.configurator.enable(this.element),this.canvas.onBeforeDestroy.subscribe(()=>{this.configurator.disable(this.element)})}static configure(e,t,o,s,n,a){new ve(e,t,o,s,n,a)}}const or=()=>{const r=document.createElement("div");return r.style.width="100%",r.style.height="100%",r.style.position="relative",r},me=()=>{const r=document.createElement("div");return r.style.position="absolute",r.style.inset="0",r},tt=()=>{const r=me();return r.style.pointerEvents="none",r};class sr{constructor(e){i(this,"background",me());i(this,"main",me());i(this,"overlayConnectablePorts",tt());i(this,"overlayDraggableEdges",tt());i(this,"host",or());this.element=e,this.element.appendChild(this.host),this.host.appendChild(this.background),this.host.appendChild(this.main),this.host.appendChild(this.overlayConnectablePorts),this.host.appendChild(this.overlayDraggableEdges)}destroy(){this.host.removeChild(this.background),this.host.removeChild(this.main),this.host.removeChild(this.overlayConnectablePorts),this.host.removeChild(this.overlayDraggableEdges),this.element.removeChild(this.host)}}const P=Object.freeze({seed:"HTMLGraph is awesome",maxTimeDeltaSec:.01,nodeCharge:1e5,nodeMass:1,edgeEquilibriumLength:300,edgeStiffness:1e3,dtSec:.01,maxIterations:1e3,convergenceVelocity:10,maxForce:1e7,nodeForceCoefficient:1,barnesHutAreaRadiusThreshold:.01,barnesHutTheta:1}),I=Object.freeze({mouseDownEventVerifier:r=>r.button===0,mouseUpEventVerifier:r=>r.button===0,movementThreshold:10}),N=()=>{},rt=(r,e,t)=>r==="closest-connectable-port"?new et(e,t):r==="nearest-connectable-port"?new et(e,t):new Kt(r),Ee=r=>{if(typeof r=="function")return r;switch(r.type){case"straight":return()=>new We({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowOffset:r.arrowOffset,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleSquareSide:r.cycleSquareSide,roundness:r.roundness,detourDistance:r.detourDistance,detourDirection:r.detourDirection});case"horizontal":return()=>new Oe({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowOffset:r.arrowOffset,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleSquareSide:r.cycleSquareSide,roundness:r.roundness,detourDistance:r.detourDistance});case"vertical":return()=>new He({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowOffset:r.arrowOffset,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleSquareSide:r.cycleSquareSide,roundness:r.roundness,detourDistance:r.detourDistance});case"direct":return()=>new te({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,sourceOffset:r.sourceOffset,targetOffset:r.targetOffset});default:return()=>new $e({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleRadius:r.cycleRadius,smallCycleRadius:r.smallCycleRadius,curvature:r.curvature,detourDistance:r.detourDistance,detourDirection:r.detourDirection})}},ir=r=>{var l,u,g,p,w,f;const e=r.nodeDragVerifier??(()=>!0),t=r.moveOnTop!==!1,o=r.moveEdgesOnTop!==!1&&t,s=(l=r.mouse)==null?void 0:l.dragCursor,n=s!==void 0?s:"grab",a=(u=r.mouse)==null?void 0:u.mouseDownEventVerifier,h=a!==void 0?a:y=>y.button===0,d=(g=r.mouse)==null?void 0:g.mouseUpEventVerifier,c=d!==void 0?d:y=>y.button===0;return{moveOnTop:t,moveEdgesOnTop:o,dragCursor:n,gridSize:r.gridSize??null,mouseDownEventVerifier:h,mouseUpEventVerifier:c,onNodeDragStarted:((p=r.events)==null?void 0:p.onNodeDragStarted)??N,onNodeDrag:((w=r.events)==null?void 0:w.onNodeDrag)??N,nodeDragVerifier:e,onNodeDragFinished:((f=r.events)==null?void 0:f.onNodeDragFinished)??N}},nr=r=>{const e=r.minX!==null?r.minX:-1/0,t=r.maxX!==null?r.maxX:1/0,o=r.minY!==null?r.minY:-1/0,s=r.maxY!==null?r.maxY:1/0;return n=>{let a=n.nextTransform.x,h=n.nextTransform.y;a<e&&a<n.prevTransform.x&&(a=Math.min(n.prevTransform.x,e));const d=n.canvasWidth*n.prevTransform.scale,c=t-d;a>c&&a>n.prevTransform.x&&(a=Math.max(n.prevTransform.x,c)),h<o&&h<n.prevTransform.y&&(h=Math.min(n.prevTransform.y,o));const l=n.canvasHeight*n.prevTransform.scale,u=s-l;return h>u&&h>n.prevTransform.y&&(h=Math.max(n.prevTransform.y,u)),{scale:n.nextTransform.scale,x:a,y:h}}},ar=r=>{const e=r.maxContentScale,t=r.minContentScale,o=e!==null?1/e:0,s=t!==null?1/t:1/0;return n=>{const a=n.prevTransform,h=n.nextTransform;let d=h.scale,c=h.x,l=h.y;if(h.scale>s&&h.scale>a.scale){d=Math.max(a.scale,s),c=a.x,l=a.y;const u=(d-a.scale)/(h.scale-a.scale);c=a.x+(h.x-a.x)*u,l=a.y+(h.y-a.y)*u}if(h.scale<o&&h.scale<a.scale){d=Math.min(a.scale,o),c=a.x,l=a.y;const u=(d-a.scale)/(h.scale-a.scale);c=a.x+(h.x-a.x)*u,l=a.y+(h.y-a.y)*u}return{scale:d,x:c,y:l}}},hr=r=>e=>r.reduce((t,o)=>o({prevTransform:e.prevTransform,nextTransform:t,canvasWidth:e.canvasWidth,canvasHeight:e.canvasHeight}),e.nextTransform),ot=r=>{if(typeof r=="function")return r;switch(r.type){case"scale-limit":return ar({minContentScale:r.minContentScale??0,maxContentScale:r.maxContentScale??1/0});case"shift-limit":return nr({minX:r.minX??-1/0,maxX:r.maxX??1/0,minY:r.minY??-1/0,maxY:r.maxY??1/0})}},st=r=>{var g,p,w,f,y,E,x,C,D,L,W,z;const e=(g=r==null?void 0:r.scale)==null?void 0:g.mouseWheelSensitivity,t=e!==void 0?e:1.2,o=r==null?void 0:r.transformPreprocessor;let s;o!==void 0?Array.isArray(o)?s=hr(o.map(V=>ot(V))):s=ot(o):s=V=>V.nextTransform;const n=((p=r==null?void 0:r.shift)==null?void 0:p.cursor)!==void 0?r.shift.cursor:"grab",a=(w=r==null?void 0:r.shift)==null?void 0:w.mouseDownEventVerifier,h=a!==void 0?a:V=>V.button===0,d=(f=r==null?void 0:r.shift)==null?void 0:f.mouseUpEventVerifier,c=d!==void 0?d:V=>V.button===0,l=(y=r==null?void 0:r.scale)==null?void 0:y.mouseWheelEventVerifier,u=l!==void 0?l:()=>!0;return{wheelSensitivity:t,onTransformStarted:((E=r==null?void 0:r.events)==null?void 0:E.onTransformStarted)??N,onTransformFinished:((x=r==null?void 0:r.events)==null?void 0:x.onTransformFinished)??N,onBeforeTransformChange:((C=r==null?void 0:r.events)==null?void 0:C.onBeforeTransformChange)??N,onTransformChange:((D=r==null?void 0:r.events)==null?void 0:D.onTransformChange)??N,transformPreprocessor:s,shiftCursor:n,mouseDownEventVerifier:h,mouseUpEventVerifier:c,mouseWheelEventVerifier:u,scaleWheelFinishTimeout:((L=r==null?void 0:r.scale)==null?void 0:L.wheelFinishTimeout)??500,onResizeTransformStarted:((W=r==null?void 0:r.events)==null?void 0:W.onResizeTransformStarted)??N,onResizeTransformFinished:((z=r==null?void 0:r.events)==null?void 0:z.onResizeTransformFinished)??N}},dr=(r,e)=>{const t=document.createElementNS("http://www.w3.org/2000/svg","circle");return t.setAttribute("cx","0"),t.setAttribute("cy","0"),t.setAttribute("r",`${r}`),t.setAttribute("fill",`${e}`),t},cr=r=>r instanceof SVGElement?r:dr((r==null?void 0:r.radius)??1.5,(r==null?void 0:r.color)??"#d8d8d8"),lr=r=>{const e=r.tileDimensions,t=(e==null?void 0:e.width)??25,o=(e==null?void 0:e.height)??25,s=cr(r.renderer??{});return{tileWidth:t,tileHeight:o,renderer:s,maxViewportScale:r.maxViewportScale??10}},O=Object.freeze({connectionTypeResolver:()=>"direct",connectionPreprocessor:r=>r,connectionAllowedVerifier:()=>!0,mouseEventVerifier:r=>r.button===0}),ur=(r,e,t)=>{var s,n,a;const o=r.connectionAllowedVerifier??O.connectionAllowedVerifier;return{connectionTypeResolver:r.connectionTypeResolver??O.connectionTypeResolver,connectionAllowedVerifier:o,draggingPortDirectionResolver:rt(r.dragPortDirection,t,o),edgeShapeFactory:r.edgeShape!==void 0?Ee(r.edgeShape):e,connectionPreprocessor:r.connectionPreprocessor??O.connectionPreprocessor,mouseDownEventVerifier:r.mouseDownEventVerifier??O.mouseEventVerifier,mouseUpEventVerifier:r.mouseUpEventVerifier??O.mouseEventVerifier,onAfterEdgeCreated:((s=r.events)==null?void 0:s.onAfterEdgeCreated)??N,onEdgeCreationInterrupted:((n=r.events)==null?void 0:n.onEdgeCreationInterrupted)??N,onEdgeCreationPrevented:((a=r.events)==null?void 0:a.onEdgeCreationPrevented)??N}},Q=Object.freeze({connectionAllowedVerifier:()=>!0,connectionPreprocessor:r=>r,mouseDownEventVerifier:r=>r.button===0&&r.ctrlKey,mouseUpEventVerifier:r=>r.button===0}),gr=(r,e)=>{var s,n,a;const t=h=>{const d=e.getPortAdjacentEdgeIds(h);return d.length>0?d[d.length-1]:null},o=r.connectionAllowedVerifier??Q.connectionAllowedVerifier;return{connectionPreprocessor:r.connectionPreprocessor??Q.connectionPreprocessor,connectionAllowedVerifier:o,mouseDownEventVerifier:r.mouseDownEventVerifier??Q.mouseDownEventVerifier,mouseUpEventVerifier:r.mouseUpEventVerifier??Q.mouseUpEventVerifier,draggingEdgeResolver:r.draggingEdgeResolver??t,draggingEdgeShapeFactory:r.draggingEdgeShape!==void 0?Ee(r.draggingEdgeShape):null,onAfterEdgeReattached:((s=r.events)==null?void 0:s.onAfterEdgeReattached)??N,onEdgeReattachInterrupted:((n=r.events)==null?void 0:n.onEdgeReattachInterrupted)??N,onEdgeReattachPrevented:((a=r.events)==null?void 0:a.onEdgeReattachPrevented)??N,draggingPortDirectionResolver:rt(r.dragPortDirection,e,o)}},pr=r=>({nodeVerticalRadius:r.nodeContainingRadius.vertical,nodeHorizontalRadius:r.nodeContainingRadius.horizontal}),wr=r=>{var e,t;return{onAfterNodeDetached:((e=r==null?void 0:r.events)==null?void 0:e.onAfterNodeDetached)??N,onBeforeNodeAttached:((t=r==null?void 0:r.events)==null?void 0:t.onBeforeNodeAttached)??N}};class it extends Error{constructor(){super(...arguments);i(this,"name","CanvasBuilderError")}}class nt{constructor(e,t,o){i(this,"dt");i(this,"nodeMass");i(this,"edgeEquilibriumLength");i(this,"edgeStiffness");i(this,"nodeForcesApplicationStrategy");i(this,"distanceVectorGenerator");this.graph=e,this.currentCoords=t,this.dt=o.dtSec,this.nodeMass=o.nodeMass,this.edgeEquilibriumLength=o.edgeEquilibriumLength,this.edgeStiffness=o.edgeStiffness,this.distanceVectorGenerator=o.distanceVectorGenerator,this.nodeForcesApplicationStrategy=o.nodeForcesApplicationStrategy}apply(){let e=0;const t=new Map;return this.graph.getAllNodeIds().forEach(s=>{t.set(s,{x:0,y:0})}),this.nodeForcesApplicationStrategy.apply(this.currentCoords,t),this.applyEdgeForces(t),this.currentCoords.forEach((s,n)=>{const a=t.get(n),h={x:a.x/this.nodeMass*this.dt,y:a.y/this.nodeMass*this.dt};e=Math.max(e,Math.sqrt(h.x*h.x+h.y*h.y));const d=h.x*this.dt,c=h.y*this.dt;s.x+=d,s.y+=c}),e}applyEdgeForces(e){this.graph.getAllEdgeIds().forEach(t=>{const o=this.graph.getEdge(t),s=this.graph.getPort(o.from),n=this.graph.getPort(o.to),a=this.currentCoords.get(s.nodeId),h=this.currentCoords.get(n.nodeId),d=this.distanceVectorGenerator.create(a,h),l=(d.d-this.edgeEquilibriumLength)*this.edgeStiffness,u=d.ex*l,g=d.ey*l,p=e.get(s.nodeId),w=e.get(n.nodeId);p.x+=u,p.y+=g,w.x-=u,w.y-=g})}}class at{constructor(e){i(this,"PI2",2*Math.PI);this.rand=e}create(e,t){const o=t.x-e.x,s=t.y-e.y,n=o*o+s*s;if(n===0){const c=this.PI2*this.rand();return{ex:Math.cos(c),ey:Math.sin(c),d:0}}const a=Math.sqrt(n),h=o/a,d=s/a;return{ex:h,ey:d,d:a}}}const ht=r=>{if(r.distance===0)return r.maxForce;const e=r.coefficient*(r.sourceCharge*r.targetCharge/(r.distance*r.distance));return Math.min(e,r.maxForce)};class fr{constructor(e){i(this,"nodeCharge");i(this,"distanceVectorGenerator");i(this,"maxForce");this.nodeCharge=e.nodeCharge,this.distanceVectorGenerator=e.distanceVectorGenerator,this.maxForce=e.maxForce}apply(e,t){const o=Array.from(t.keys()),s=o.length;for(let n=0;n<s;n++){const a=o[n];for(let h=n+1;h<s;h++){const d=o[h],c=e.get(a),l=e.get(d),u=this.distanceVectorGenerator.create(c,l),g=ht({coefficient:1,sourceCharge:this.nodeCharge,targetCharge:this.nodeCharge,distance:u.d,maxForce:this.maxForce}),p=g*u.ex,w=g*u.ey,f=t.get(a),y=t.get(d);f.x-=p,f.y-=w,y.x+=p,y.y+=w}}}}const yr=r=>{if(r.size===0)return{centerX:0,centerY:0,radius:0};let e=1/0,t=-1/0,o=1/0,s=-1/0;r.forEach(d=>{e=Math.min(e,d.x),t=Math.max(t,d.x),o=Math.min(o,d.y),s=Math.max(s,d.y)});const n=t-e,a=s-o,h=Math.max(n,a);return{centerX:(e+t)/2,centerY:(o+s)/2,radius:h/2}};class vr{constructor(e){i(this,"root");i(this,"leaves",new Map);i(this,"coords");i(this,"areaRadiusThreshold");i(this,"nodeMass");i(this,"nodeCharge");i(this,"sortedParentNodes",[]);this.coords=e.coords,this.areaRadiusThreshold=e.areaRadiusThreshold,this.nodeMass=e.nodeMass,this.nodeCharge=e.nodeCharge,this.root={nodeIds:new Set(e.coords.keys()),box:e.box,totalMass:0,totalCharge:0,chargeCenter:{x:0,y:0},parent:null,lb:null,lt:null,rb:null,rt:null};let t=[this.root];for(;t.length>0;){const o=[];for(;t.length>0;){const s=t.pop();this.processNode(s).forEach(a=>{o.push(a)})}t=o}this.sortedParentNodes.reverse().forEach(o=>{let s=0,n=0,a=0,h=0;o.lb!==null&&(a+=o.lb.totalMass,h+=o.lb.totalCharge,s+=o.lb.chargeCenter.x*o.lb.totalCharge,n+=o.lb.chargeCenter.y*o.lb.totalCharge),o.lt!==null&&(a+=o.lt.totalMass,h+=o.lt.totalCharge,s+=o.lt.chargeCenter.x*o.lt.totalCharge,n+=o.lt.chargeCenter.y*o.lt.totalCharge),o.rb!==null&&(a+=o.rb.totalMass,h+=o.rb.totalCharge,s+=o.rb.chargeCenter.x*o.rb.totalCharge,n+=o.rb.chargeCenter.y*o.rb.totalCharge),o.rt!==null&&(a+=o.rt.totalMass,h+=o.rt.totalCharge,s+=o.rt.chargeCenter.x*o.rt.totalCharge,n+=o.rt.chargeCenter.y*o.rt.totalCharge),o.totalMass=a,o.totalCharge=h,o.chargeCenter.x=s/h,o.chargeCenter.y=n/h})}getRoot(){return this.root}getLeaf(e){return this.leaves.get(e)}processNode(e){if(e.nodeIds.size<2)return this.setLeaf(e),[];const{centerX:t,centerY:o,radius:s}=e.box;if(s<this.areaRadiusThreshold)return this.setLeaf(e),[];this.sortedParentNodes.push(e);const n=new Set,a=new Set,h=new Set,d=new Set,c=s/2;e.nodeIds.forEach(g=>{const{x:p,y:w}=this.coords.get(g);p<t?w<o?d.add(g):h.add(g):w<o?a.add(g):n.add(g),e.nodeIds.delete(g)});const l={parent:e,lb:null,lt:null,rb:null,rt:null},u=[];if(n.size>0){const g={nodeIds:n,totalMass:0,totalCharge:0,chargeCenter:{x:0,y:0},box:{centerX:t+c,centerY:o+c,radius:c},...l};e.rt=g,u.push(g)}if(a.size>0){const g={nodeIds:a,totalMass:0,totalCharge:0,chargeCenter:{x:0,y:0},box:{centerX:t+c,centerY:o-c,radius:c},...l};e.rb=g,u.push(g)}if(h.size>0){const g={nodeIds:h,totalMass:0,totalCharge:0,chargeCenter:{x:0,y:0},box:{centerX:t-c,centerY:o+c,radius:c},...l};e.lt=g,u.push(g)}if(d.size>0){const g={nodeIds:d,totalMass:0,totalCharge:0,chargeCenter:{x:0,y:0},box:{centerX:t-c,centerY:o-c,radius:c},...l};e.lb=g,u.push(g)}return u}setLeaf(e){e.totalMass=this.nodeMass*e.nodeIds.size,e.totalCharge=this.nodeCharge*e.nodeIds.size,e.chargeCenter=this.calculateLeafChargeCenter(e.nodeIds),e.nodeIds.forEach(t=>{this.leaves.set(t,e)})}calculateLeafChargeCenter(e){if(e.size===0)return{x:0,y:0};let t=0,o=0;return e.forEach(s=>{const n=this.coords.get(s);t+=n.x,o+=n.y}),{x:t/e.size,y:o/e.size}}}class mr{constructor(e){i(this,"areaRadiusThreshold");i(this,"nodeMass");i(this,"nodeCharge");i(this,"theta");i(this,"distanceVectorGenerator");i(this,"nodeForceCoefficient");i(this,"maxForce");this.areaRadiusThreshold=e.areaRadiusThreshold,this.nodeMass=e.nodeMass,this.nodeCharge=e.nodeCharge,this.theta=e.theta,this.distanceVectorGenerator=e.distanceVectorGenerator,this.nodeForceCoefficient=e.nodeForceCoefficient,this.maxForce=e.maxForce}apply(e,t){const o=yr(e),s=new vr({box:o,coords:e,areaRadiusThreshold:this.areaRadiusThreshold,nodeMass:this.nodeMass,nodeCharge:this.nodeCharge});e.forEach((n,a)=>{const h=this.calculateForceForNode(s.getLeaf(a),a,e),d=t.get(a);this.applyForce(d,h)})}calculateForceForNode(e,t,o){const s=o.get(t),n={x:0,y:0};e.nodeIds.forEach(h=>{if(h!==t){const d=o.get(h),c=this.calculateNodeRepulsiveForce({sourceCharge:this.nodeCharge,targetCharge:this.nodeCharge,sourceCoords:d,targetCoords:s});this.applyForce(n,c)}});let a=e;for(;a!==null;){const h=a.parent;if(h!==null){const d=this.distanceVectorGenerator.create(h.chargeCenter,s);h.box.radius*2<d.d*this.theta?(this.tryApplyFarForce({totalForce:n,targetCoords:s,target:h.lb,current:a}),this.tryApplyFarForce({totalForce:n,targetCoords:s,target:h.rb,current:a}),this.tryApplyFarForce({totalForce:n,targetCoords:s,target:h.rt,current:a}),this.tryApplyFarForce({totalForce:n,targetCoords:s,target:h.lt,current:a})):(this.tryApplyNearForce({totalForce:n,targetCoords:s,target:h.lb,current:a,nodesCoords:o}),this.tryApplyNearForce({totalForce:n,targetCoords:s,target:h.rb,current:a,nodesCoords:o}),this.tryApplyNearForce({totalForce:n,targetCoords:s,target:h.rt,current:a,nodesCoords:o}),this.tryApplyNearForce({totalForce:n,targetCoords:s,target:h.lt,current:a,nodesCoords:o}))}a=a.parent}return n}calculateExactForce(e,t,o){const s={x:0,y:0},n=[e];for(;n.length>0;){const a=n.pop();a.nodeIds.forEach(h=>{const d=o.get(h),c=this.calculateNodeRepulsiveForce({sourceCharge:this.nodeCharge,targetCharge:this.nodeCharge,sourceCoords:d,targetCoords:t});this.applyForce(s,c)}),a.lb!==null&&n.push(a.lb),a.rb!==null&&n.push(a.rb),a.lt!==null&&n.push(a.lt),a.rt!==null&&n.push(a.rt)}return s}calculateApproximateForce(e,t){return this.calculateNodeRepulsiveForce({sourceCharge:this.nodeCharge,targetCharge:e.totalCharge,sourceCoords:e.chargeCenter,targetCoords:t})}calculateNodeRepulsiveForce(e){const t=this.distanceVectorGenerator.create(e.sourceCoords,e.targetCoords),o=ht({coefficient:this.nodeForceCoefficient,sourceCharge:e.sourceCharge,targetCharge:e.targetCharge,distance:t.d,maxForce:this.maxForce});return{x:o*t.ex,y:o*t.ey}}applyForce(e,t){e.x+=t.x,e.y+=t.y}tryApplyFarForce(e){if(e.target!==null&&e.target!==e.current){const t=this.calculateApproximateForce(e.target,e.targetCoords);this.applyForce(e.totalForce,t)}}tryApplyNearForce(e){if(e.target!==null&&e.target!==e.current){const t=this.calculateExactForce(e.target,e.targetCoords,e.nodesCoords);this.applyForce(e.totalForce,t)}}}const dt=r=>r.theta!==0?new mr({nodeCharge:r.nodeCharge,nodeForceCoefficient:r.nodeForceCoefficient,distanceVectorGenerator:r.distanceVectorGenerator,maxForce:r.maxForce,theta:r.theta,nodeMass:r.nodeMass,areaRadiusThreshold:r.areaRadiusThreshold}):new fr({nodeCharge:r.nodeCharge,nodeForceCoefficient:r.nodeForceCoefficient,distanceVectorGenerator:r.distanceVectorGenerator,maxForce:r.maxForce});class ct{constructor(e){i(this,"rand");i(this,"sparsity");this.rand=e.rand,this.sparsity=e.sparsity}calculateCoordinates(e){const{graph:t,viewport:o}=e,s=new Map,n=t.getAllNodeIds().filter(w=>{const f=t.getNode(w);return f.x===null||f.y===null}),a=Math.sqrt(n.length)*this.sparsity,{width:h,height:d}=o.getDimensions(),c={x:h/2,y:d/2},l=o.createContentCoords(c),u=a/2,g={x:l.x-u,y:l.y-u};return t.getAllNodeIds().forEach(w=>{const f=t.getNode(w);s.set(w,{x:f.x??g.x+a*this.rand(),y:f.y??g.y+a*this.rand()})}),s}}class Er{constructor(e){i(this,"distanceVectorGenerator");i(this,"nodeForcesApplicationStrategy");i(this,"fillerLayoutAlgorithm");i(this,"maxIterations");i(this,"dtSec");i(this,"nodeMass");i(this,"edgeEquilibriumLength");i(this,"edgeStiffness");i(this,"convergenceVelocity");this.maxIterations=e.maxIterations,this.dtSec=e.dtSec,this.nodeMass=e.nodeMass,this.edgeEquilibriumLength=e.edgeEquilibriumLength,this.edgeStiffness=e.edgeStiffness,this.convergenceVelocity=e.convergenceVelocity,this.distanceVectorGenerator=new at(e.rand),this.nodeForcesApplicationStrategy=dt({distanceVectorGenerator:this.distanceVectorGenerator,nodeCharge:e.nodeCharge,maxForce:e.maxForce,nodeForceCoefficient:e.nodeForceCoefficient,theta:e.barnesHutTheta,areaRadiusThreshold:e.barnesHutAreaRadiusThreshold,nodeMass:e.nodeMass}),this.fillerLayoutAlgorithm=new ct({rand:e.rand,sparsity:e.edgeEquilibriumLength})}calculateCoordinates(e){const{graph:t,viewport:o}=e,s=this.fillerLayoutAlgorithm.calculateCoordinates({graph:t,viewport:o});for(let n=0;n<this.maxIterations&&!(new nt(t,s,{distanceVectorGenerator:this.distanceVectorGenerator,nodeForcesApplicationStrategy:this.nodeForcesApplicationStrategy,dtSec:this.dtSec,nodeMass:this.nodeMass,edgeEquilibriumLength:this.edgeEquilibriumLength,edgeStiffness:this.edgeStiffness}).apply()<this.convergenceVelocity);n++);return s}}class xr{constructor(e,t){i(this,"forest",new Set);i(this,"remainingNodeIds");for(this.graph=e,this.nextLayerNodesResolver=t,this.remainingNodeIds=new Set(this.graph.getAllNodeIds());this.remainingNodeIds.size>0;){const[o]=this.remainingNodeIds;this.traverse(o)}}generate(){return this.forest}traverse(e){const t={nodeId:e,children:new Set},o=[];this.forest.add({root:t,sequence:o});let s=[t];for(this.remainingNodeIds.delete(t.nodeId);s.length>0;){const n=[];s.forEach(a=>{o.push(a);const h=this.nextLayerNodesResolver({graph:this.graph,currentNodeId:a.nodeId});for(const d of h){if(!this.remainingNodeIds.has(d))continue;this.remainingNodeIds.delete(d);const c={nodeId:d,children:new Set};a.children.add(c),n.push(c)}}),s=n}}}class Ar{constructor(e){this.params=e}generate(e){let t=0;const o=[],s=e.length-1,n=[];e.forEach((h,d)=>{if(t+=this.params.spaceAroundRadius,h.forEach((c,l)=>{n[l]===void 0?n[l]={start:t+c.start,end:t+c.end}:n[l].end=t+c.end}),o.push(t),d!==s){const c=e[d+1],l=n.map(u=>({start:u.start-t,end:u.end-t}));t+=this.calculateMaxDiff(l,c)}t+=this.params.spaceAroundRadius});const a=t/2;return{childOffsets:o.map(h=>h-a),subtreeSpans:n.map(h=>({start:h.start-a,end:h.end-a}))}}calculateMaxDiff(e,t){let o=0;const s=Math.min(e.length,t.length);for(let n=0;n<s;n++){const a=e[n].end-t[n].start;o=Math.max(o,a)}return o-2*this.params.spaceAroundRadius}}class Sr{constructor(e,t){i(this,"offsets",new Map);i(this,"treeSpans",new Map);this.tree=e;const o=t.spaceAroundRadius,s=new Ar({spaceAroundRadius:o});[...this.tree.sequence].reverse().forEach(n=>{const a=Array.from(n.children).map(c=>this.treeSpans.get(c.nodeId)),h=s.generate(a);let d=0;n.children.forEach(c=>{this.offsets.set(c.nodeId,h.childOffsets[d]),d++}),this.treeSpans.set(n.nodeId,[{start:-o,end:o},...h.subtreeSpans]),n.children.forEach(c=>{this.treeSpans.delete(c.nodeId)})}),this.offsets.set(this.tree.root.nodeId,0)}generate(){return this.offsets}}class br{constructor(e){this.params=e}calculateCoordinates(e){const t=new Map,s=new xr(e.graph,this.params.nextLayerNodesResolver).generate();let n=0;return s.forEach(a=>{t.set(a.root.nodeId,{x:n,y:0});const d=new Sr(a,{spaceAroundRadius:this.params.layerSpace/2}).generate();let c=[a.root];for(;c.length>0;){const l=[];n+=this.params.layerWidth,c.forEach(u=>{u.children.forEach(g=>{const p=t.get(u.nodeId).y;t.set(g.nodeId,{y:p+d.get(g.nodeId),x:n}),l.push(g)})}),c=l}}),t.forEach((a,h)=>{t.set(h,this.params.transform(a))}),t}}const Pr=r=>{const{graph:e,currentNodeId:t}=r,o=e.getNodeOutgoingEdgeIds(t).map(n=>{const a=e.getEdge(n);return e.getPort(a.to).nodeId}),s=e.getNodeIncomingEdgeIds(t).map(n=>{const a=e.getEdge(n);return e.getPort(a.from).nodeId});return new Set([...o,...s])},Nr=r=>{const{graph:e,currentNodeId:t}=r,o=e.getNodeOutgoingEdgeIds(t).map(s=>{const n=e.getEdge(s);return e.getPort(n.to).nodeId});return new Set(o)},Cr=r=>{const{graph:e,currentNodeId:t}=r,o=e.getNodeIncomingEdgeIds(t).map(s=>{const n=e.getEdge(s);return e.getPort(n.from).nodeId});return new Set(o)};class Tr{constructor(e){i(this,"distanceVectorGenerator");i(this,"nodeForcesApplicationStrategy");i(this,"convergenceVelocity");i(this,"maxTimeDeltaSec");i(this,"nodeMass");i(this,"edgeEquilibriumLength");i(this,"edgeStiffness");i(this,"fillerLayoutAlgorithm");this.convergenceVelocity=e.convergenceVelocity,this.maxTimeDeltaSec=e.maxTimeDeltaSec,this.nodeMass=e.nodeMass,this.edgeEquilibriumLength=e.edgeEquilibriumLength,this.edgeStiffness=e.edgeStiffness,this.distanceVectorGenerator=new at(e.rand),this.nodeForcesApplicationStrategy=dt({distanceVectorGenerator:this.distanceVectorGenerator,nodeCharge:e.nodeCharge,maxForce:e.maxForce,nodeForceCoefficient:e.nodeForceCoefficient,theta:e.barnesHutTheta,areaRadiusThreshold:e.barnesHutAreaRadiusThreshold,nodeMass:e.nodeMass}),this.fillerLayoutAlgorithm=new ct({rand:e.rand,sparsity:e.edgeEquilibriumLength})}calculateNextCoordinates(e){const{graph:t,viewport:o,dt:s}=e,n=this.fillerLayoutAlgorithm.calculateCoordinates({graph:t,viewport:o});return new nt(t,n,{distanceVectorGenerator:this.distanceVectorGenerator,nodeForcesApplicationStrategy:this.nodeForcesApplicationStrategy,dtSec:Math.min(s,this.maxTimeDeltaSec),nodeMass:this.nodeMass,edgeEquilibriumLength:this.edgeEquilibriumLength,edgeStiffness:this.edgeStiffness}).apply()<this.convergenceVelocity&&!t.getAllNodeIds().some(c=>{const l=t.getNode(c);return l.x===null||l.y===null})?new Map:n}}const lt=r=>{let e=1779033703,t=3144134277,o=1013904242,s=2773480762;for(let n=0,a;n<r.length;n++)a=r.charCodeAt(n),e=t^Math.imul(e^a,597399067),t=o^Math.imul(t^a,2869860233),o=s^Math.imul(o^a,951274213),s=e^Math.imul(s^a,2716044179);return e=Math.imul(o^e>>>18,597399067),t=Math.imul(s^t>>>22,2869860233),o=Math.imul(e^o>>>17,951274213),s=Math.imul(t^s>>>19,2716044179),e^=t^o^s,t^=e,o^=e,s^=e,[e>>>0,t>>>0,o>>>0,s>>>0]},ut=(r,e,t,o)=>function(){r|=0,e|=0,t|=0,o|=0;const s=(r+e|0)+o|0;return o=o+1|0,r=e^e>>>9,e=t+(t<<3)|0,t=t<<21|t>>>11,t=t+s|0,(s>>>0)/4294967296},Mr=r=>{var e,t;switch(r==null?void 0:r.type){case"custom":return r.instance;default:{const o=lt((r==null?void 0:r.seed)??P.seed),s=ut(o[0],o[1],o[2],o[3]);return new Tr({rand:s,maxTimeDeltaSec:(r==null?void 0:r.maxTimeDeltaSec)??P.maxTimeDeltaSec,nodeCharge:(r==null?void 0:r.nodeCharge)??P.nodeCharge,nodeMass:(r==null?void 0:r.nodeMass)??P.nodeMass,edgeEquilibriumLength:(r==null?void 0:r.edgeEquilibriumLength)??P.edgeEquilibriumLength,edgeStiffness:(r==null?void 0:r.edgeStiffness)??P.edgeStiffness,convergenceVelocity:(r==null?void 0:r.convergenceVelocity)??P.convergenceVelocity,maxForce:(r==null?void 0:r.maxForce)??P.maxForce,nodeForceCoefficient:(r==null?void 0:r.nodeForceCoefficient)??P.nodeForceCoefficient,barnesHutTheta:((e=r==null?void 0:r.barnesHut)==null?void 0:e.theta)??P.barnesHutTheta,barnesHutAreaRadiusThreshold:((t=r==null?void 0:r.barnesHut)==null?void 0:t.areaRadiusThreshold)??P.barnesHutAreaRadiusThreshold})}}},Dr={staticNodeResolver:()=>!1},Rr=r=>{var t,o;return{algorithm:Mr((r==null?void 0:r.algorithm)??{}),staticNodeResolver:(r==null?void 0:r.staticNodeResolver)??Dr.staticNodeResolver,onBeforeApplied:((t=r==null?void 0:r.events)==null?void 0:t.onBeforeApplied)??N,onAfterApplied:((o=r==null?void 0:r.events)==null?void 0:o.onAfterApplied)??N}},Lr=r=>r instanceof k?{type:"trigger",trigger:r}:r==="topologyChangeMicrotask"?{type:"topologyChangeSchedule",schedule:Je}:(r==null?void 0:r.type)==="topologyChangeMacrotask"?{type:"topologyChangeSchedule",schedule:Gt}:{type:"topologyChangeSchedule",schedule:Je},xe=Object.freeze({staticNodeResolver:()=>!1,hierarchicalLayout:{layerWidth:300,layerSpace:300}}),Z=(r,e)=>({a:r.a*e.a+r.b*e.d,b:r.a*e.b+r.b*e.e,c:r.a*e.c+r.b*e.f+r.c,d:r.d*e.a+r.e*e.d,e:r.d*e.b+r.e*e.e,f:r.d*e.c+r.e*e.f+r.f}),Vr=r=>{const{a:e,b:t,c:o,d:s,e:n,f:a}=r,h=e*n-t*s;return{a:n/h,b:-t/h,c:(t*a-o*n)/h,d:-s/h,e:e/h,f:(o*s-e*a)/h}};class Ir{resolve(e){if("shift"in e)return this.createShiftBaseMatrix(e.shift);if("scale"in e){const t=e.origin??{x:0,y:0};return this.createScaleRelativeMatrix(e.scale,t)}if("rotate"in e){const t=e.origin??{x:0,y:0};return this.createRotateRelativeMatrix(e.rotate,t)}if("mirror"in e){const t=e.origin??{x:0,y:0};return this.createMirrorRelativeMatrix(e.mirror,t)}return{a:e.a??1,b:e.b??0,c:e.c??0,d:e.d??0,e:e.e??1,f:e.f??0}}createScaleRelativeMatrix(e,t){const o=this.createScaleBaseMatrix(e),s=this.createShiftBaseMatrix(t);return this.createRelativeTransform(o,s)}createRotateRelativeMatrix(e,t){const o=this.createRotateBaseMatrix(e),s=this.createShiftBaseMatrix(t);return this.createRelativeTransform(o,s)}createMirrorRelativeMatrix(e,t){const o=this.createMirrorYBaseMatrix(),s=Z(this.createShiftBaseMatrix(t),this.createRotateBaseMatrix(e));return this.createRelativeTransform(o,s)}createRelativeTransform(e,t){const o=Z(t,e),s=Vr(t);return Z(o,s)}createShiftBaseMatrix(e){return{a:1,b:0,c:e.x,d:0,e:1,f:e.y}}createScaleBaseMatrix(e){return{a:e,b:0,c:0,d:0,e,f:0}}createRotateBaseMatrix(e){const t=Math.sin(e),o=Math.cos(e);return{a:o,b:-t,c:0,d:t,e:o,f:0}}createMirrorYBaseMatrix(){return{a:1,b:0,c:0,d:0,e:-1,f:0}}}const Fr=r=>{if(r===void 0)return s=>s;if(typeof r=="function")return r;const e=Array.isArray(r)?r:[r];let t={a:1,b:0,c:0,d:0,e:1,f:0};const o=new Ir;return e.forEach(s=>{const n=o.resolve(s);t=Z(t,n)}),s=>{const{x:n,y:a}=s;return{x:t.a*n+t.b*a+t.c,y:t.d*n+t.e*a+t.f}}},Ur=r=>{if(typeof r=="function")return r;switch(r){case"outgoing":return Nr;case"incoming":return Cr;default:return Pr}},$r=r=>{var e,t;switch(r==null?void 0:r.type){case"custom":return r.instance;case"hierarchical":return new br({layerWidth:r.layerWidth??xe.hierarchicalLayout.layerWidth,layerSpace:r.layerSpace??xe.hierarchicalLayout.layerSpace,transform:Fr(r.transform),nextLayerNodesResolver:Ur(r.nextLayerNodesResolver)});default:{const o=lt((r==null?void 0:r.seed)??P.seed),s=ut(o[0],o[1],o[2],o[3]);return new Er({dtSec:(r==null?void 0:r.dtSec)??P.dtSec,maxIterations:(r==null?void 0:r.maxIterations)??P.maxIterations,rand:s,nodeCharge:(r==null?void 0:r.nodeCharge)??P.nodeCharge,nodeMass:(r==null?void 0:r.nodeMass)??P.nodeMass,edgeEquilibriumLength:(r==null?void 0:r.edgeEquilibriumLength)??P.edgeEquilibriumLength,edgeStiffness:(r==null?void 0:r.edgeStiffness)??P.edgeStiffness,convergenceVelocity:(r==null?void 0:r.convergenceVelocity)??P.convergenceVelocity,maxForce:(r==null?void 0:r.maxForce)??P.maxForce,nodeForceCoefficient:(r==null?void 0:r.nodeForceCoefficient)??P.nodeForceCoefficient,barnesHutTheta:((e=r==null?void 0:r.barnesHut)==null?void 0:e.theta)??P.barnesHutTheta,barnesHutAreaRadiusThreshold:((t=r==null?void 0:r.barnesHut)==null?void 0:t.areaRadiusThreshold)??P.barnesHutAreaRadiusThreshold})}}},Br=r=>{var e,t;return{algorithm:$r(r.algorithm),applyOn:Lr(r.applyOn),staticNodeResolver:r.staticNodeResolver??xe.staticNodeResolver,onBeforeApplied:((e=r.events)==null?void 0:e.onBeforeApplied)??N,onAfterApplied:((t=r.events)==null?void 0:t.onAfterApplied)??N}},Or=(r,e)=>({...r,onNodeDragStarted:t=>{e.add(t),r.onNodeDragStarted(t)},onNodeDragFinished:t=>{e.delete(t),r.onNodeDragFinished(t)}}),Wr=(r,e)=>{r.graph.onBeforeNodeRemoved.subscribe(t=>{e.delete(t)}),r.graph.onBeforeClear.subscribe(()=>{e.clear()}),r.onBeforeDestroy.subscribe(()=>{e.clear()})},zr=(r,e)=>({...r,staticNodeResolver:t=>r.staticNodeResolver(t)||e.has(t)}),Ae=r=>()=>r,gt=Ae(0),kr=()=>{let r=0;return()=>r++},Hr=(r,e)=>{let t=gt,o=gt;const s=kr();return r==="incremental"&&(t=s),e==="incremental"&&(o=s),typeof r=="number"&&(t=Ae(r)),typeof e=="number"&&(o=Ae(e)),typeof r=="function"&&(t=r),typeof e=="function"&&(o=e),{nodesPriorityFn:t,edgesPriorityFn:o}},Yr=r=>{var t,o,s,n,a;const e=Hr((t=r.nodes)==null?void 0:t.priority,(o=r.edges)==null?void 0:o.priority);return{nodes:{centerFn:((s=r.nodes)==null?void 0:s.centerFn)??Re,priorityFn:e.nodesPriorityFn},ports:{direction:((n=r.ports)==null?void 0:n.direction)??0},edges:{shapeFactory:Ee(((a=r.edges)==null?void 0:a.shape)??{}),priorityFn:e.edgesPriorityFn}}},Xr=r=>r.applyOn.type==="topologyChangeSchedule"?r.applyOn.schedule:se,Gr=r=>{var o,s,n,a;const{canvasDefaults:e}=r,t=r.hasLayout?Xr(r.layoutParams):se;return{focus:{contentPadding:((o=e.focus)==null?void 0:o.contentPadding)??((s=e.focus)==null?void 0:s.contentOffset)??100,minContentScale:((n=e.focus)==null?void 0:n.minContentScale)??0,schedule:t,animationDuration:((a=e.focus)==null?void 0:a.animationDuration)??0}}},jr=r=>({onNodeSelected:r.onNodeSelected,mouseDownEventVerifier:r.mouseDownEventVerifier??I.mouseDownEventVerifier,mouseUpEventVerifier:r.mouseUpEventVerifier??I.mouseUpEventVerifier,movementThreshold:r.movementThreshold??I.movementThreshold}),Jr=r=>({onCanvasSelected:r.onCanvasSelected,mouseDownEventVerifier:r.mouseDownEventVerifier??I.mouseDownEventVerifier,mouseUpEventVerifier:r.mouseUpEventVerifier??I.mouseUpEventVerifier,movementThreshold:r.movementThreshold??I.movementThreshold}),Kr=r=>({onEdgeSelected:r.onEdgeSelected,mouseDownEventVerifier:r.mouseDownEventVerifier??I.mouseDownEventVerifier,mouseUpEventVerifier:r.mouseUpEventVerifier??I.mouseUpEventVerifier,movementThreshold:r.movementThreshold??I.movementThreshold});class Qr{constructor(e){i(this,"used",!1);i(this,"canvasDefaults",{});i(this,"dragConfig",{});i(this,"transformConfig",{});i(this,"backgroundConfig",{});i(this,"connectablePortsConfig",{});i(this,"draggableEdgesConfig",{});i(this,"virtualScrollConfig");i(this,"layoutConfig",{});i(this,"animatedLayoutConfig",{});i(this,"userSelectableNodesConfig");i(this,"userSelectableEdgesConfig");i(this,"userSelectableCanvasConfig");i(this,"hasDraggableNodes",!1);i(this,"hasTransformableViewport",!1);i(this,"hasNodeResizeReactiveEdges",!1);i(this,"hasBackground",!1);i(this,"hasUserConnectablePorts",!1);i(this,"hasUserDraggableEdges",!1);i(this,"hasAnimatedLayout",!1);i(this,"hasLayout",!1);i(this,"boxRenderingTrigger",new k);i(this,"window",window);i(this,"animationStaticNodes",new Set);i(this,"pointInsideVerifier");i(this,"eventTagger",new Jt);this.element=e,this.pointInsideVerifier=new bt(e,this.window)}setDefaults(e){return this.canvasDefaults=e,this}enableUserDraggableNodes(e){return this.hasDraggableNodes=!0,this.dragConfig=e??{},this}enableUserTransformableViewport(e){return this.hasTransformableViewport=!0,this.transformConfig=e??{},this}enableNodeResizeReactiveEdges(){return this.hasNodeResizeReactiveEdges=!0,this}enableVirtualScroll(e){return this.virtualScrollConfig=e,this}enableBackground(e){return this.hasBackground=!0,this.backgroundConfig=e??{},this}enableUserConnectablePorts(e){return this.hasUserConnectablePorts=!0,this.connectablePortsConfig=e??{},this}enableUserDraggableEdges(e){return this.hasUserDraggableEdges=!0,this.draggableEdgesConfig=e??{},this}enableLayout(e){return this.layoutConfig=e??{},this.hasLayout=!0,this.hasAnimatedLayout=!1,this}enableAnimatedLayout(e){return this.animatedLayoutConfig=e??{},this.hasAnimatedLayout=!0,this.hasLayout=!1,this}enableUserSelectableNodes(e){return this.userSelectableNodesConfig=e,this}enableUserSelectableEdges(e){return this.userSelectableEdgesConfig=e,this}enableUserSelectableCanvas(e){return this.userSelectableCanvasConfig=e,this}build(){if(this.used)throw new it("Failed to build Canvas because CanvasBuilder is a single-use object");this.used=!0;const e=new xt(this.element),t=new Pe,o=new sr(this.element),s=this.createHtmlView(o.main,t,e),n=Yr(this.canvasDefaults),a=new je(t,s,n),h=Br(this.layoutConfig),d=Gr({canvasDefaults:this.canvasDefaults,hasLayout:this.hasLayout,layoutParams:h}),c=new _e(t,e,d,this.window),l=new Ke(e),u=new Ge(t),g=new be(u,l,a,c);if(this.hasBackground&&ce.configure(g,lr(this.backgroundConfig),o.background),this.hasNodeResizeReactiveEdges&&_.configure(g),this.userSelectableEdgesConfig!==void 0){const p=Kr(this.userSelectableEdgesConfig);ye.configure(g,this.window,this.pointInsideVerifier,this.eventTagger,p)}if(this.userSelectableNodesConfig!==void 0){const p=jr(this.userSelectableNodesConfig);fe.configure(g,this.window,this.pointInsideVerifier,this.eventTagger,p)}if(this.userSelectableCanvasConfig!==void 0){const p=Jr(this.userSelectableCanvasConfig);ve.configure(g,o.main,this.window,this.pointInsideVerifier,this.eventTagger,p)}if(this.hasDraggableNodes){let p=ir(this.dragConfig);this.hasAnimatedLayout&&(p=Or(p,this.animationStaticNodes)),he.configure(g,o.main,this.window,this.pointInsideVerifier,p)}if(this.hasUserConnectablePorts){const p=ur(this.connectablePortsConfig,n.edges.shapeFactory,g.graph);le.configure(g,o.overlayConnectablePorts,e,this.window,this.pointInsideVerifier,p)}if(this.hasUserDraggableEdges){const p=gr(this.draggableEdgesConfig,g.graph);ue.configure(g,o.overlayDraggableEdges,e,this.window,this.pointInsideVerifier,p)}if(this.virtualScrollConfig!==void 0?de.configure(g,o.main,this.window,st(this.transformConfig),this.boxRenderingTrigger,this.pointInsideVerifier,pr(this.virtualScrollConfig)):this.hasTransformableViewport&&K.configure(g,o.main,this.window,this.pointInsideVerifier,st(this.transformConfig)),this.hasLayout&&er.configure(g,h),this.hasAnimatedLayout){let p=Rr(this.animatedLayoutConfig);this.hasDraggableNodes&&(Wr(g,this.animationStaticNodes),p=zr(p,this.animationStaticNodes)),we.configure(g,p,this.window)}return g.onBeforeDestroy.subscribe(()=>{o.destroy()}),g}createHtmlView(e,t,o){let s=new Se(t,o,e);return this.virtualScrollConfig!==void 0&&(s=new vt(s,t,this.boxRenderingTrigger,wr(this.virtualScrollConfig))),s=new mt(s,t),s}}A.BezierEdgeShape=$e,A.CanvasBuilder=Qr,A.CanvasBuilderError=it,A.CanvasError=S,A.ConnectionCategory=T,A.DirectEdgeShape=te,A.EventSubject=k,A.HorizontalEdgeShape=Oe,A.InteractiveEdgeError=Xe,A.InteractiveEdgeShape=re,A.MidpointEdgeShape=Xt,A.StraightEdgeShape=We,A.VerticalEdgeShape=He,Object.defineProperty(A,Symbol.toStringTag,{value:"Module"})});
1
+ (function(A,T){typeof exports=="object"&&typeof module<"u"?T(exports):typeof define=="function"&&define.amd?define(["exports"],T):(A=typeof globalThis<"u"?globalThis:A||self,T(A.HtmlGraph={}))})(this,function(A){"use strict";var qr=Object.defineProperty;var eo=(A,T,B)=>T in A?qr(A,T,{enumerable:!0,configurable:!0,writable:!0,value:B}):A[T]=B;var i=(A,T,B)=>eo(A,typeof T!="symbol"?T+"":T,B);var T=(r=>(r.Line="line",r.NodeCycle="node-cycle",r.PortCycle="port-cycle",r))(T||{});const B=()=>{const r=document.createElement("div");return r.style.width="100%",r.style.height="100%",r.style.position="relative",r.style.overflow="hidden",r},yt=()=>{const r=document.createElement("div");return r.style.position="absolute",r.style.top="0",r.style.left="0",r.style.width="0",r.style.height="0",r},ft=r=>{r.style.position="absolute",r.style.top="0",r.style.left="0",r.style.visibility="hidden"},vt=r=>{r.style.removeProperty("position"),r.style.removeProperty("top"),r.style.removeProperty("left"),r.style.removeProperty("visibility"),r.style.removeProperty("transform")};class Se{constructor(e,t,o){i(this,"host",B());i(this,"container",yt());i(this,"edgeIdToElementMap",new Map);i(this,"attachedNodeIds",new Set);i(this,"applyTransform",()=>{const e=this.viewportStore.getContentMatrix();this.container.style.transform=`matrix(${e.scale}, 0, 0, ${e.scale}, ${e.x}, ${e.y})`});this.graphStore=e,this.viewportStore=t,this.element=o,this.element.appendChild(this.host),this.host.appendChild(this.container),this.viewportStore.onAfterUpdated.subscribe(this.applyTransform)}attachNode(e){const t=this.graphStore.getNode(e);ft(t.element),this.attachedNodeIds.add(e),this.container.appendChild(t.element),this.updateNodePosition(e),this.updateNodePriority(e),t.element.style.visibility="visible"}detachNode(e){const t=this.graphStore.getNode(e);vt(t.element),this.container.removeChild(t.element),this.attachedNodeIds.delete(e)}attachEdge(e){const t=this.graphStore.getEdge(e).payload.shape.svg;this.edgeIdToElementMap.set(e,t),this.container.appendChild(t),this.renderEdge(e),this.updateEdgePriority(e)}detachEdge(e){const t=this.edgeIdToElementMap.get(e);this.container.removeChild(t),this.edgeIdToElementMap.delete(e)}clear(){this.edgeIdToElementMap.forEach((e,t)=>{this.detachEdge(t)}),this.attachedNodeIds.forEach(e=>{this.detachNode(e)})}destroy(){this.clear(),this.viewportStore.onAfterUpdated.unsubscribe(this.applyTransform),this.element.removeChild(this.host),this.host.removeChild(this.container)}updateNodePosition(e){const t=this.graphStore.getNode(e),{width:o,height:s}=t.element.getBoundingClientRect(),n=this.viewportStore.getViewportMatrix().scale,{payload:a}=t,h=a.centerFn(o,s),d=a.x-n*h.x,c=a.y-n*h.y;t.element.style.transform=`translate(${d}px, ${c}px)`}updateNodePriority(e){const t=this.graphStore.getNode(e);t.element.style.zIndex=`${t.payload.priority}`}updateEdgeShape(e){const t=this.edgeIdToElementMap.get(e);this.container.removeChild(t);const s=this.graphStore.getEdge(e).payload.shape.svg;this.edgeIdToElementMap.set(e,s),this.container.appendChild(s)}renderEdge(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from),s=this.graphStore.getPort(t.to),n=o.element.getBoundingClientRect(),a=s.element.getBoundingClientRect(),h=this.host.getBoundingClientRect(),d=this.viewportStore.getViewportMatrix().scale,c=this.createEdgeRenderPort(o,n,h,d),u=this.createEdgeRenderPort(s,a,h,d);let l=T.Line;o.element===s.element?l=T.PortCycle:o.nodeId===s.nodeId&&(l=T.NodeCycle),t.payload.shape.render({from:c,to:u,category:l})}updateEdgePriority(e){const t=this.graphStore.getEdge(e);t.payload.shape.svg.style.zIndex=`${t.payload.priority}`}createEdgeRenderPort(e,t,o,s){const n=this.viewportStore.createContentCoords({x:t.left-o.left,y:t.top-o.top});return{x:n.x,y:n.y,width:t.width*s,height:t.height*s,direction:e.payload.direction}}}class mt{constructor(e){i(this,"xFrom",1/0);i(this,"yFrom",1/0);i(this,"xTo",1/0);i(this,"yTo",1/0);this.graphStore=e}setRenderingBox(e){this.xFrom=e.x,this.xTo=e.x+e.width,this.yFrom=e.y,this.yTo=e.y+e.height}hasNode(e){const t=this.graphStore.getNode(e).payload,{x:o,y:s}=t;return o>=this.xFrom&&o<=this.xTo&&s>=this.yFrom&&s<=this.yTo}hasEdge(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from).nodeId,s=this.graphStore.getPort(t.to).nodeId,n=this.graphStore.getNode(o).payload,a=this.graphStore.getNode(s).payload,h=Math.min(n.x,a.x),d=Math.max(n.x,a.x),c=Math.min(n.y,a.y),u=Math.max(n.y,a.y);return h<=this.xTo&&d>=this.xFrom&&c<=this.yTo&&u>=this.yFrom}}class Et{constructor(e,t,o,s){i(this,"attachedNodes",new Set);i(this,"attachedEdges",new Set);i(this,"renderingBox");i(this,"updateViewport",e=>{this.renderingBox.setRenderingBox(e);const t=new Set,o=new Set,s=new Set,n=new Set;this.graphStore.getAllNodeIds().forEach(a=>{const h=this.renderingBox.hasNode(a),d=this.attachedNodes.has(a);h&&!d?t.add(a):!h&&d&&o.add(a)}),this.graphStore.getAllEdgeIds().forEach(a=>{const h=this.renderingBox.hasEdge(a),d=this.attachedEdges.has(a),c=this.graphStore.getEdge(a),u=this.graphStore.getPort(c.from).nodeId,l=this.graphStore.getPort(c.to).nodeId;h&&(this.renderingBox.hasNode(u)||(t.add(u),o.delete(u)),this.renderingBox.hasNode(l)||(t.add(l),o.delete(l))),h&&!d?s.add(a):!h&&d&&n.add(a)}),n.forEach(a=>{this.handleDetachEdge(a)}),o.forEach(a=>{this.handleDetachNode(a)}),t.forEach(a=>{this.attachedNodes.has(a)||this.handleAttachNode(a)}),s.forEach(a=>{this.handleAttachEdge(a)})});this.htmlView=e,this.graphStore=t,this.trigger=o,this.params=s,this.renderingBox=new mt(this.graphStore),this.trigger.subscribe(this.updateViewport)}attachNode(e){this.renderingBox.hasNode(e)&&this.handleAttachNode(e)}detachNode(e){this.attachedNodes.has(e)&&this.handleDetachNode(e)}attachEdge(e){this.renderingBox.hasEdge(e)&&this.attachEdgeEntities(e)}detachEdge(e){this.attachedEdges.has(e)&&this.handleDetachEdge(e)}updateNodePosition(e){this.attachedNodes.has(e)?this.htmlView.updateNodePosition(e):this.renderingBox.hasNode(e)&&(this.handleAttachNode(e),this.graphStore.getNodeAdjacentEdgeIds(e).forEach(t=>{this.attachEdgeEntities(t)}))}updateNodePriority(e){this.attachedNodes.has(e)&&this.htmlView.updateNodePriority(e)}updateEdgeShape(e){this.attachedEdges.has(e)&&this.htmlView.updateEdgeShape(e)}renderEdge(e){this.attachedEdges.has(e)&&this.htmlView.renderEdge(e)}updateEdgePriority(e){this.attachedEdges.has(e)&&this.htmlView.updateEdgePriority(e)}clear(){this.htmlView.clear(),this.attachedNodes.clear(),this.attachedEdges.clear()}destroy(){this.clear(),this.htmlView.destroy(),this.trigger.unsubscribe(this.updateViewport)}attachEdgeEntities(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from).nodeId,s=this.graphStore.getPort(t.to).nodeId;this.attachedNodes.has(o)||this.handleAttachNode(o),this.attachedNodes.has(s)||this.handleAttachNode(s),this.handleAttachEdge(e)}handleAttachNode(e){this.params.onBeforeNodeAttached(e),this.attachedNodes.add(e),this.htmlView.attachNode(e)}handleDetachNode(e){this.htmlView.detachNode(e),this.attachedNodes.delete(e),this.params.onAfterNodeDetached(e)}handleAttachEdge(e){this.attachedEdges.add(e),this.htmlView.attachEdge(e)}handleDetachEdge(e){this.htmlView.detachEdge(e),this.attachedEdges.delete(e)}}class xt{constructor(e,t){i(this,"deferredNodes",new Set);i(this,"deferredEdges",new Set);this.htmlView=e,this.graphStore=t}attachNode(e){this.isNodeValid(e)?this.htmlView.attachNode(e):this.deferredNodes.add(e)}detachNode(e){this.deferredNodes.has(e)?this.deferredNodes.delete(e):this.htmlView.detachNode(e)}attachEdge(e){this.isEdgeValid(e)?this.htmlView.attachEdge(e):this.deferredEdges.add(e)}detachEdge(e){this.deferredEdges.has(e)?this.deferredEdges.delete(e):this.htmlView.detachEdge(e)}updateNodePosition(e){this.deferredNodes.has(e)?this.tryAttachNode(e):this.htmlView.updateNodePosition(e)}updateNodePriority(e){this.deferredNodes.has(e)?this.tryAttachNode(e):this.htmlView.updateNodePriority(e)}updateEdgeShape(e){this.deferredEdges.has(e)?this.tryAttachEdge(e):this.htmlView.updateEdgeShape(e)}renderEdge(e){this.deferredEdges.has(e)?this.tryAttachEdge(e):this.htmlView.renderEdge(e)}updateEdgePriority(e){this.deferredEdges.has(e)?this.tryAttachEdge(e):this.htmlView.updateEdgePriority(e)}clear(){this.deferredNodes.clear(),this.deferredEdges.clear(),this.htmlView.clear()}destroy(){this.htmlView.destroy()}isNodeValid(e){const t=this.graphStore.getNode(e);return!(t.payload.x===null||t.payload.y===null)}tryAttachNode(e){this.isNodeValid(e)&&(this.deferredNodes.delete(e),this.htmlView.attachNode(e))}isEdgeValid(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from),s=this.graphStore.getPort(t.to);return!(this.deferredNodes.has(o.nodeId)||this.deferredNodes.has(s.nodeId))}tryAttachEdge(e){this.isEdgeValid(e)&&(this.deferredEdges.delete(e),this.htmlView.attachEdge(e))}}class k{constructor(){i(this,"callbacks",new Set)}subscribe(e){this.callbacks.add(e)}unsubscribe(e){this.callbacks.delete(e)}emit(e){this.callbacks.forEach(t=>{t(e)})}}const M=()=>{const r=new k;return[r,r]};class be{constructor(e,t,o,s){i(this,"beforeDestroyEmitter");i(this,"destroyed",!1);i(this,"onBeforeDestroy");this.graph=e,this.viewport=t,this.graphController=o,this.viewportController=s,[this.beforeDestroyEmitter,this.onBeforeDestroy]=M()}addNode(e){return this.graphController.addNode(e),this}updateNode(e,t){return this.graphController.updateNode(e,t),this}removeNode(e){return this.graphController.removeNode(e),this}markPort(e){return this.graphController.markPort(e),this}updatePort(e,t){return this.graphController.updatePort(e,t),this}unmarkPort(e){return this.graphController.unmarkPort(e),this}addEdge(e){return this.graphController.addEdge(e),this}updateEdge(e,t){return this.graphController.updateEdge(e,t),this}removeEdge(e){return this.graphController.removeEdge(e),this}clear(){return this.graphController.clear(),this}focus(e){return this.viewportController.focus(e),this}center(e,t){return this.viewportController.center(e,t),this}patchViewportMatrix(e){return this.viewportController.patchViewportMatrix(e),this}patchContentMatrix(e){return this.viewportController.patchContentMatrix(e),this}destroy(){this.destroyed||(this.destroyed=!0,this.beforeDestroyEmitter.emit(),this.graphController.destroy(),this.viewportController.destroy())}}class At{constructor(){i(this,"singleToMultiMap",new Map);i(this,"multiToSingleMap",new Map)}addRecord(e,t){const o=this.singleToMultiMap.get(e);o===void 0?this.singleToMultiMap.set(e,new Set([t])):o.add(t),this.multiToSingleMap.set(t,e)}getMultiBySingle(e){const t=this.singleToMultiMap.get(e)??new Set;return Array.from(t.values())}removeByMulti(e){const t=this.multiToSingleMap.get(e),o=this.singleToMultiMap.get(t);o.delete(e),o.size===0&&this.singleToMultiMap.delete(t),this.multiToSingleMap.delete(e)}getByMulti(e){return this.multiToSingleMap.get(e)}removeBySingle(e){this.singleToMultiMap.get(e).forEach(o=>{this.multiToSingleMap.delete(o)}),this.singleToMultiMap.delete(e)}clear(){this.singleToMultiMap.clear(),this.multiToSingleMap.clear()}forEachSingle(e){this.singleToMultiMap.forEach((t,o)=>{e(o)})}hasSingle(e){return this.singleToMultiMap.get(e)!==void 0}hasMulti(e){return this.multiToSingleMap.get(e)!==void 0}}class S extends Error{constructor(){super(...arguments);i(this,"name","CanvasError")}}const b=Object.freeze({accessNonexistingNode:r=>`Failed to access node with ID ${JSON.stringify(r)} because it does not exist`,addNodeWithExistingId:r=>`Failed to add node with ID ${JSON.stringify(r)} because a node with this ID already exists`,addNodeWithElementInUse:(r,e)=>`Failed to add node with ID ${JSON.stringify(r)} because its HTML element is already attached to node with ID ${JSON.stringify(e)}`,updateNonexistentNode:r=>`Failed to update node with ID ${JSON.stringify(r)} because it does not exist`,removeNonexistentNode:r=>`Failed to remove node with ID ${JSON.stringify(r)} because it does not exist`,accessNonexistentPort:r=>`Failed to access port with ID ${JSON.stringify(r)} because it does not exist`,addPortWithExistingId:r=>`Failed to add port with ID ${JSON.stringify(r)} because a port with this ID already exists`,addPortToNonexistentNode:(r,e)=>`Failed to add port with ID ${JSON.stringify(r)} to node with ID ${JSON.stringify(e)} because the node does not exist`,updateNonexistentPort:r=>`Failed to update port with ID ${JSON.stringify(r)} because it does not exist`,accessPortsOfNonexistentNode:r=>`Failed to access ports of node with ID ${JSON.stringify(r)} because the node does not exist`,removeNonexistentPort:r=>`Failed to remove port with ID ${JSON.stringify(r)} because it does not exist`,accessNonexistentEdge:r=>`Failed to access edge with ID ${JSON.stringify(r)} because it does not exist`,addEdgeWithExistingId:r=>`Failed to add edge with ID ${JSON.stringify(r)} because an edge with this ID already exists`,addEdgeWithElementInUse:(r,e)=>`Failed to add edge with ID ${JSON.stringify(r)} because its element is already attached to edge with ID ${JSON.stringify(e)}`,addEdgeFromNonexistentPort:(r,e)=>`Failed to add edge with ID ${JSON.stringify(r)} from port with ID ${JSON.stringify(e)} because the port does not exist`,addEdgeToNonexistentPort:(r,e)=>`Failed to add edge with ID ${JSON.stringify(r)} to port with ID ${JSON.stringify(e)} because the port does not exist`,updateNonexistentEdge:r=>`Failed to update edge with ID ${JSON.stringify(r)} because it does not exist`,updateNonexistentEdgeSource:(r,e)=>`Failed to update source of edge with ID ${JSON.stringify(r)} because source port with ID ${JSON.stringify(e)} does not exist`,updateNonexistentEdgeTarget:(r,e)=>`Failed to update target of edge with ID ${JSON.stringify(r)} because target port with ID ${JSON.stringify(e)} does not exist`,removeNonexistentEdge:r=>`Failed to remove edge with ID ${JSON.stringify(r)} because it does not exist`,accessEdgesForNonexistentPort:r=>`Failed to access edges for port with ID ${JSON.stringify(r)} because the port does not exist`});class Pe{constructor(){i(this,"nodes",new Map);i(this,"ports",new Map);i(this,"edges",new Map);i(this,"nodesElementsMap",new Map);i(this,"portIncomingEdges",new Map);i(this,"portOutgoingEdges",new Map);i(this,"portCycleEdges",new Map);i(this,"elementPorts",new At);i(this,"edgesElementsMap",new Map);i(this,"afterNodeAddedEmitter");i(this,"onAfterNodeAdded");i(this,"afterNodeUpdatedEmitter");i(this,"onAfterNodeUpdated");i(this,"afterNodePriorityUpdatedEmitter");i(this,"onAfterNodePriorityUpdated");i(this,"beforeNodeRemovedEmitter");i(this,"onBeforeNodeRemoved");i(this,"afterPortAddedEmitter");i(this,"onAfterPortAdded");i(this,"afterPortUpdatedEmitter");i(this,"onAfterPortUpdated");i(this,"beforePortRemovedEmitter");i(this,"onBeforePortRemoved");i(this,"afterEdgeAddedEmitter");i(this,"onAfterEdgeAdded");i(this,"afterEdgeShapeUpdatedEmitter");i(this,"onAfterEdgeShapeUpdated");i(this,"afterEdgeUpdatedEmitter");i(this,"onAfterEdgeUpdated");i(this,"afterEdgePriorityUpdatedEmitter");i(this,"onAfterEdgePriorityUpdated");i(this,"beforeEdgeRemovedEmitter");i(this,"onBeforeEdgeRemoved");i(this,"beforeClearEmitter");i(this,"onBeforeClear");[this.afterNodeAddedEmitter,this.onAfterNodeAdded]=M(),[this.afterNodeUpdatedEmitter,this.onAfterNodeUpdated]=M(),[this.afterNodePriorityUpdatedEmitter,this.onAfterNodePriorityUpdated]=M(),[this.beforeNodeRemovedEmitter,this.onBeforeNodeRemoved]=M(),[this.afterPortAddedEmitter,this.onAfterPortAdded]=M(),[this.afterPortUpdatedEmitter,this.onAfterPortUpdated]=M(),[this.beforePortRemovedEmitter,this.onBeforePortRemoved]=M(),[this.afterEdgeAddedEmitter,this.onAfterEdgeAdded]=M(),[this.afterEdgeShapeUpdatedEmitter,this.onAfterEdgeShapeUpdated]=M(),[this.afterEdgeUpdatedEmitter,this.onAfterEdgeUpdated]=M(),[this.afterEdgePriorityUpdatedEmitter,this.onAfterEdgePriorityUpdated]=M(),[this.beforeEdgeRemovedEmitter,this.onBeforeEdgeRemoved]=M(),[this.beforeClearEmitter,this.onBeforeClear]=M()}hasNode(e){return this.nodes.has(e)}getNode(e){const t=this.nodes.get(e);if(t===void 0)throw new S(b.accessNonexistingNode(e));return t}addNode(e){if(this.hasNode(e.id))throw new S(b.addNodeWithExistingId(e.id));const t=this.findNodeIdByElement(e.element);if(t!==void 0)throw new S(b.addNodeWithElementInUse(e.id,t));const o=new Map,s={element:e.element,payload:{x:e.x,y:e.y,centerFn:e.centerFn,priority:e.priority},ports:o};this.nodes.set(e.id,s),this.nodesElementsMap.set(e.element,e.id),this.afterNodeAddedEmitter.emit(e.id)}getAllNodeIds(){return Array.from(this.nodes.keys())}findNodeIdByElement(e){return this.nodesElementsMap.get(e)}updateNode(e,t){if(!this.hasNode(e))throw new S(b.updateNonexistentNode(e));const{payload:o}=this.nodes.get(e);o.x=t.x??o.x,o.y=t.y??o.y,o.centerFn=t.centerFn??o.centerFn,t.priority!==void 0&&(o.priority=t.priority,this.afterNodePriorityUpdatedEmitter.emit(e)),this.afterNodeUpdatedEmitter.emit(e)}removeNode(e){if(!this.hasNode(e))throw new S(b.removeNonexistentNode(e));this.beforeNodeRemovedEmitter.emit(e);const t=this.getNode(e);this.nodesElementsMap.delete(t.element),this.nodes.delete(e)}hasPort(e){return this.ports.has(e)}getPort(e){const t=this.ports.get(e);if(t===void 0)throw new S(b.accessNonexistentPort(e));return t}addPort(e){if(this.hasPort(e.id))throw new S(b.addPortWithExistingId(e.id));if(!this.hasNode(e.nodeId))throw new S(b.addPortToNonexistentNode(e.id,e.nodeId));this.ports.set(e.id,{element:e.element,payload:{direction:e.direction},nodeId:e.nodeId}),this.elementPorts.addRecord(e.element,e.id),this.portCycleEdges.set(e.id,new Set),this.portIncomingEdges.set(e.id,new Set),this.portOutgoingEdges.set(e.id,new Set),this.getNode(e.nodeId).ports.set(e.id,e.element),this.afterPortAddedEmitter.emit(e.id)}updatePort(e,t){if(!this.hasPort(e))throw new S(b.updateNonexistentPort(e));const o=this.getPort(e).payload;o.direction=t.direction??o.direction,this.afterPortUpdatedEmitter.emit(e)}getAllPortIds(){return Array.from(this.ports.keys())}findPortIdsByElement(e){return this.elementPorts.getMultiBySingle(e)}getNodePortIds(e){const t=this.nodes.get(e);if(t===void 0)throw new S(b.accessPortsOfNonexistentNode(e));return Array.from(t.ports.keys())}removePort(e){if(!this.hasPort(e))throw new S(b.removeNonexistentPort(e));const t=this.getPort(e).nodeId;this.beforePortRemovedEmitter.emit(e),this.getNode(t).ports.delete(e),this.ports.delete(e),this.elementPorts.removeByMulti(e)}hasEdge(e){return this.edges.has(e)}getEdge(e){const t=this.edges.get(e);if(t===void 0)throw new S(b.accessNonexistentEdge(e));return t}findEdgeIdByElement(e){return this.edgesElementsMap.get(e)}addEdge(e){if(this.hasEdge(e.id))throw new S(b.addEdgeWithExistingId(e.id));if(!this.hasPort(e.from))throw new S(b.addEdgeFromNonexistentPort(e.id,e.from));if(!this.hasPort(e.to))throw new S(b.addEdgeToNonexistentPort(e.id,e.to));const t=this.findEdgeIdByElement(e.shape.svg);if(t!==void 0)throw new S(b.addEdgeWithElementInUse(e.id,t));this.addEdgeInternal(e),this.afterEdgeAddedEmitter.emit(e.id)}updateEdge(e,t){if(!this.hasEdge(e))throw new S(b.updateNonexistentEdge(e));if(t.from!==void 0||t.to!==void 0){if(t.from!==void 0&&!this.hasPort(t.from))throw new S(b.updateNonexistentEdgeSource(e,t.from));if(t.to!==void 0&&!this.hasPort(t.to))throw new S(b.updateNonexistentEdgeTarget(e,t.to));const s=this.getEdge(e),n=s.payload;this.removeEdgeInternal(e),this.addEdgeInternal({id:e,from:t.from??s.from,to:t.to??s.to,shape:n.shape,priority:n.priority})}const o=this.edges.get(e);t.shape!==void 0&&(o.payload.shape=t.shape,this.afterEdgeShapeUpdatedEmitter.emit(e)),t.priority!==void 0&&(o.payload.priority=t.priority,this.afterEdgePriorityUpdatedEmitter.emit(e)),this.afterEdgeUpdatedEmitter.emit(e)}getAllEdgeIds(){return Array.from(this.edges.keys())}removeEdge(e){if(!this.hasEdge(e))throw new S(b.removeNonexistentEdge(e));this.beforeEdgeRemovedEmitter.emit(e),this.removeEdgeInternal(e)}clear(){this.beforeClearEmitter.emit(),this.portIncomingEdges.clear(),this.portOutgoingEdges.clear(),this.portCycleEdges.clear(),this.elementPorts.clear(),this.nodesElementsMap.clear(),this.edgesElementsMap.clear(),this.edges.clear(),this.ports.clear(),this.nodes.clear()}getPortIncomingEdgeIds(e){const t=this.portIncomingEdges.get(e);if(t===void 0)throw new S(b.accessEdgesForNonexistentPort(e));return Array.from(t)}getPortOutgoingEdgeIds(e){const t=this.portOutgoingEdges.get(e);if(t===void 0)throw new S(b.accessEdgesForNonexistentPort(e));return Array.from(t)}getPortCycleEdgeIds(e){const t=this.portCycleEdges.get(e);if(t===void 0)throw new S(b.accessEdgesForNonexistentPort(e));return Array.from(t)}getPortAdjacentEdgeIds(e){return[...this.getPortIncomingEdgeIds(e),...this.getPortOutgoingEdgeIds(e),...this.getPortCycleEdgeIds(e)]}getNodeIncomingEdgeIds(e){const t=Array.from(this.getNode(e).ports.keys()),o=[];return t.forEach(s=>{this.getPortIncomingEdgeIds(s).filter(n=>{const a=this.getEdge(n);return this.getPort(a.from).nodeId!==e}).forEach(n=>{o.push(n)})}),o}getNodeOutgoingEdgeIds(e){const t=Array.from(this.getNode(e).ports.keys()),o=[];return t.forEach(s=>{this.getPortOutgoingEdgeIds(s).filter(n=>{const a=this.getEdge(n);return this.getPort(a.to).nodeId!==e}).forEach(n=>{o.push(n)})}),o}getNodeCycleEdgeIds(e){const t=Array.from(this.getNode(e).ports.keys()),o=[];return t.forEach(s=>{this.getPortCycleEdgeIds(s).forEach(n=>{o.push(n)}),this.getPortIncomingEdgeIds(s).filter(n=>{const a=this.getEdge(n);return this.getPort(a.to).nodeId===e}).forEach(n=>{o.push(n)})}),o}getNodeAdjacentEdgeIds(e){const t=Array.from(this.getNode(e).ports.keys()),o=[];return t.forEach(s=>{this.getPortIncomingEdgeIds(s).forEach(n=>{o.push(n)}),this.getPortOutgoingEdgeIds(s).forEach(n=>{o.push(n)}),this.getPortCycleEdgeIds(s).forEach(n=>{o.push(n)})}),o}addEdgeInternal(e){this.edges.set(e.id,{from:e.from,to:e.to,payload:{shape:e.shape,priority:e.priority}}),this.edgesElementsMap.set(e.shape.svg,e.id),e.from!==e.to?(this.portOutgoingEdges.get(e.from).add(e.id),this.portIncomingEdges.get(e.to).add(e.id)):this.portCycleEdges.get(e.from).add(e.id)}removeEdgeInternal(e){const{from:t,to:o,payload:s}=this.getEdge(e);this.portCycleEdges.get(t).delete(e),this.portCycleEdges.get(o).delete(e),this.portIncomingEdges.get(t).delete(e),this.portIncomingEdges.get(o).delete(e),this.portOutgoingEdges.get(t).delete(e),this.portOutgoingEdges.get(o).delete(e),this.edges.delete(e),this.edgesElementsMap.delete(s.shape.svg)}}const Ne=r=>({scale:1/r.scale,x:-r.x/r.scale,y:-r.y/r.scale}),Ce={scale:1,x:0,y:0},Te=(r,e)=>({x:r.scale*e.x+r.x,y:r.scale*e.y+r.y});class St{constructor(e){i(this,"viewportMatrix",Ce);i(this,"contentMatrix",Ce);i(this,"beforeUpdateEmitter");i(this,"onBeforeUpdated");i(this,"afterUpdateEmitter");i(this,"onAfterUpdated");i(this,"afterResizeEmitter");i(this,"onAfterResize");i(this,"observer",new ResizeObserver(()=>{this.afterResizeEmitter.emit()}));this.host=e,[this.afterUpdateEmitter,this.onAfterUpdated]=M(),[this.beforeUpdateEmitter,this.onBeforeUpdated]=M(),[this.afterResizeEmitter,this.onAfterResize]=M(),this.observer.observe(this.host)}getViewportMatrix(){return this.viewportMatrix}getContentMatrix(){return this.contentMatrix}patchViewportMatrix(e){this.viewportMatrix={scale:e.scale??this.viewportMatrix.scale,x:e.x??this.viewportMatrix.x,y:e.y??this.viewportMatrix.y},this.beforeUpdateEmitter.emit(),this.contentMatrix=Ne(this.viewportMatrix),this.afterUpdateEmitter.emit()}patchContentMatrix(e){this.contentMatrix={scale:e.scale??this.contentMatrix.scale,x:e.x??this.contentMatrix.x,y:e.y??this.contentMatrix.y},this.beforeUpdateEmitter.emit(),this.viewportMatrix=Ne(this.contentMatrix),this.afterUpdateEmitter.emit()}getDimensions(){const{width:e,height:t}=this.host.getBoundingClientRect();return{width:e,height:t}}createContentCoords(e){return Te(this.viewportMatrix,e)}createViewportCoords(e){return Te(this.contentMatrix,e)}destroy(){this.observer.disconnect()}}class _{constructor(e){i(this,"elementToNodeId",new Map);i(this,"nodesResizeObserver");i(this,"onAfterNodeAdded",e=>{const t=this.canvas.graph.getNode(e);this.elementToNodeId.set(t.element,e),this.nodesResizeObserver.observe(t.element)});i(this,"onBeforeNodeRemoved",e=>{const t=this.canvas.graph.getNode(e);this.elementToNodeId.delete(t.element),this.nodesResizeObserver.unobserve(t.element)});i(this,"reset",()=>{this.nodesResizeObserver.disconnect(),this.elementToNodeId.clear()});i(this,"revert",()=>{this.reset()});this.canvas=e,this.nodesResizeObserver=new ResizeObserver(t=>{t.forEach(o=>{const s=o.target;this.handleNodeResize(s)})}),this.canvas.graph.onAfterNodeAdded.subscribe(this.onAfterNodeAdded),this.canvas.graph.onBeforeNodeRemoved.subscribe(this.onBeforeNodeRemoved),this.canvas.graph.onBeforeClear.subscribe(this.reset),this.canvas.onBeforeDestroy.subscribe(this.revert)}static configure(e){new _(e)}handleNodeResize(e){const t=this.elementToNodeId.get(e);this.canvas.updateNode(t)}}const bt=(r,e,t)=>{const{x:o,y:s,width:n,height:a}=r.getBoundingClientRect();return e>=o&&e<=o+n&&t>=s&&t<=s+a},Pt=(r,e,t)=>e>=0&&e<=r.innerWidth&&t>=0&&t<=r.innerHeight;class Nt{constructor(e,t){this.element=e,this.win=t}verify(e,t){return bt(this.element,e,t)&&Pt(this.win,e,t)}}const H=(r,e)=>{e!==null?r.style.cursor=e:r.style.removeProperty("cursor")},Y=r=>{const e=document.createElement("div");return{id:r.overlayNodeId,element:e,x:r.portCoords.x,y:r.portCoords.y,ports:[{id:r.overlayNodeId,element:e,direction:r.portDirection}]}},Ct=(r,e)=>{let t=e;for(;t!==null;){const o=r.findPortIdsByElement(t)[0]??null;if(o!==null)return{status:"portFound",portId:o};if(r.findNodeIdByElement(t)!==void 0)return{status:"nodeEncountered"};t=t.parentElement}return{status:"notFound"}};function*Me(r,e){const t=r.elementsFromPoint(e.x,e.y);for(const o of t){if(o.shadowRoot!==null){const s=Me(o.shadowRoot,e);for(const n of s)yield n}yield o}}const De=(r,e)=>{const t=Me(document,e);for(const o of t){const s=Ct(r,o);if(s.status==="portFound")return s.portId;if(s.status==="nodeEncountered")return null}return null};var V=(r=>(r.StaticNodeId="static",r.DraggingNodeId="dragging",r.EdgeId="edge",r))(V||{});const Re=(r,e)=>({x:r/2,y:e/2}),E=(r,e,t)=>({x:e.x*r.x-e.y*r.y+((1-e.x)*t.x+e.y*t.y),y:e.y*r.x+e.x*r.y+((1-e.x)*t.y-e.y*t.x)}),Le=(r,e,t)=>{const o={x:r.x+r.width/2,y:r.y+r.height/2},s={x:e.x+e.width/2,y:e.y+e.height/2},n=Math.min(o.x,s.x)-t,a=Math.min(o.y,s.y)-t,h=2*t,d=Math.abs(s.x-o.x)+h,c=Math.abs(s.y-o.y)+h;return{x:n,y:a,width:d,height:c,from:{x:o.x-n,y:o.y-a},to:{x:s.x-n,y:s.y-a}}};class Tt{constructor(e){i(this,"path");i(this,"midpoint");const{from:t,to:o,arrowLength:s,fromDir:n,toDir:a,curvature:h,hasSourceArrow:d,hasTargetArrow:c}=e,u=(t.x+o.x)/2,l=(t.y+o.y)/2;this.midpoint={x:u,y:l};const g=E({x:t.x+s,y:t.y},n,t),p=E({x:o.x-s,y:o.y},a,o),w={x:g.x+n.x*h,y:g.y+n.y*h},y={x:p.x-a.x*h,y:p.y-a.y*h},f=`M ${g.x} ${g.y} C ${w.x} ${w.y}, ${y.x} ${y.y}, ${p.x} ${p.y}`,m=d?"":`M ${t.x} ${t.y} L ${g.x} ${g.y} `,x=c?"":` M ${p.x} ${p.y} L ${o.x} ${o.y}`;this.path=`${m}${f}${x}`}}class Mt{constructor(e){i(this,"path");i(this,"midpoint");const{hasSourceArrow:t,hasTargetArrow:o,curvature:s,fromDir:n,toDir:a,detourDir:h,from:d,to:c,arrowLength:u,detourDistance:l}=e,g=t?E({x:d.x+u,y:d.y},n,d):d,p=o?E({x:c.x-u,y:c.y},a,c):c,w=Math.cos(h)*l,y=Math.sin(h)*l,f=E({x:d.x+u,y:d.y},n,d),m={x:f.x+w,y:f.y+y},x=E({x:c.x-u,y:c.y},a,c),N={x:x.x+w,y:x.y+y},D={x:(m.x+N.x)/2,y:(m.y+N.y)/2},R={x:f.x+s*n.x,y:f.y+s*n.y},F={x:x.x-s*a.x,y:x.y-s*a.y},U={x:f.x+w,y:f.y+y},L={x:x.x+w,y:x.y+y};this.path=[`M ${g.x} ${g.y}`,`L ${f.x} ${f.y}`,`C ${R.x} ${R.y} ${U.x} ${U.y} ${D.x} ${D.y}`,`C ${L.x} ${L.y} ${F.x} ${F.y} ${x.x} ${x.y}`,`L ${p.x} ${p.y}`].join(" "),this.midpoint=D}}const q=Object.freeze({edgeColor:"--edge-color"}),Ve=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","svg");return e.style.pointerEvents="none",e.style.position="absolute",e.style.top="0",e.style.left="0",e.style.overflow="visible",e.style.setProperty(q.edgeColor,r),e},Ie=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","path");return e.setAttribute("stroke",`var(${q.edgeColor})`),e.setAttribute("stroke-width",`${r}`),e.setAttribute("fill","none"),e},X=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","path");return r.setAttribute("fill",`var(${q.edgeColor})`),r},Fe=(r,e)=>{r.style.transform=`translate(${e.x}px, ${e.y}px)`,r.style.width=`${e.width}px`,r.style.height=`${e.height}px`},$=(r,e)=>{const t=[];if(r.length>0&&t.push(`M ${r[0].x} ${r[0].y}`),r.length===2&&t.push(`L ${r[1].x} ${r[1].y}`),r.length>2){const o=r.length-1;let s=0,n=0,a=0;r.forEach((h,d)=>{let c=0,u=0,l=0;const g=d>0,p=d<o,w=g&&p;if(g&&(c=-s,u=-n,l=a),p){const R=r[d+1];s=R.x-h.x,n=R.y-h.y,a=Math.sqrt(s*s+n*n)}const f=a!==0?Math.min((w?e:0)/a,d<o-1?.5:1):0,m=w?{x:h.x+s*f,y:h.y+n*f}:h,N=l!==0?Math.min((w?e:0)/l,d>1?.5:1):0,D=w?{x:h.x+c*N,y:h.y+u*N}:h;d>0&&t.push(`L ${D.x} ${D.y}`),w&&t.push(`C ${h.x} ${h.y} ${h.x} ${h.y} ${m.x} ${m.y}`)})}return t.join(" ")},Dt=(r,e)=>{const t=r.linePoint,o=e.linePoint,s=o.x-t.x>=0,n=r.dirX>=0,a=e.dirX>=0;if(n===a){const l=n===s,g=(t.x+o.x)/2,p=(t.y+o.y)/2,w={x:g,y:p};return l?{points:[r.arrowPoint,{x:w.x,y:t.y},{x:w.x,y:o.y},e.arrowPoint],midpoint:w}:{points:[r.arrowPoint,t,{x:t.x,y:w.y},{x:o.x,y:w.y},o,e.arrowPoint],midpoint:w}}const d=n===s,c=(t.y+o.y)/2;if(d){const l={x:o.x,y:t.y};return{points:[r.arrowPoint,l,o,e.arrowPoint],midpoint:{x:l.x,y:c}}}const u={x:t.x,y:o.y};return{points:[r.arrowPoint,t,u,e.arrowPoint],midpoint:{x:u.x,y:c}}};class Rt{constructor(e){i(this,"path");i(this,"midpoint");const{from:t,to:o,fromDir:s,toDir:n,arrowLength:a,arrowOffset:h,roundness:d,hasSourceArrow:c,hasTargetArrow:u}=e,l=c?E({x:t.x+a,y:t.y},s,t):t,g=u?E({x:o.x-a,y:o.y},n,o):o,p=a+h,w=E({x:t.x+p,y:t.y},s,t),y=E({x:o.x-p,y:o.y},n,o),f=Dt({arrowPoint:l,linePoint:w,dirX:s.x},{arrowPoint:g,linePoint:y,dirX:n.x});this.path=$(f.points,d),this.midpoint=f.midpoint}}class Lt{constructor(e){i(this,"path");i(this,"midpoint");const{hasSourceArrow:t,hasTargetArrow:o,from:s,to:n,arrowLength:a,fromDir:h,toDir:d,arrowOffset:c,detourDir:u,detourDistance:l,roundness:g}=e,p=t?E({x:s.x+a,y:s.y},h,s):s,w=o?E({x:n.x-a,y:n.y},d,n):n,y=a+c,f=Math.cos(u)*l,m=Math.sin(u)*l,x=E({x:s.x+y,y:s.y},h,s),N={x:x.x+f,y:x.y+m},D=E({x:n.x-y,y:n.y},d,n),R={x:D.x+f,y:D.y+m};this.midpoint={x:(N.x+R.x)/2,y:(N.y+R.y)/2},this.path=$([p,x,N,R,D,w],g)}}class Vt{constructor(e){i(this,"path");i(this,"midpoint");const{from:t,to:o,hasSourceArrow:s,hasTargetArrow:n,arrowLength:a,fromDir:h,toDir:d,arrowOffset:c,roundness:u}=e,l=this.createArrowPoint(s,h,t,a),g=this.createArrowPoint(n,d,o,-a),p=a+c,w={x:t.x+p,y:t.y},y=E(w,h,t),f={x:o.x-p,y:o.y},m=E(f,d,o);this.path=$([l,y,m,g],u);const x=(y.x+m.x)/2,N=(y.y+m.y)/2;this.midpoint={x,y:N}}createArrowPoint(e,t,o,s){if(!e)return o;const n={x:o.x+s,y:o.y};return E(n,t,o)}}const It=(r,e)=>{const t=r.linePoint,o=e.linePoint,s=o.y-t.y>=0,n=r.dirY>=0,a=e.dirY>=0;if(n===a){const l=n===s,g=(t.x+o.x)/2,p=(t.y+o.y)/2,w={x:g,y:p};return l?{points:[r.arrowPoint,{x:t.x,y:w.y},{x:o.x,y:w.y},e.arrowPoint],midpoint:w}:{points:[r.arrowPoint,t,{x:w.x,y:t.y},{x:w.x,y:o.y},o,e.arrowPoint],midpoint:w}}const d=n===s,c=(t.x+o.x)/2;if(d){const l={x:t.x,y:o.y};return{points:[r.arrowPoint,l,o,e.arrowPoint],midpoint:{x:c,y:l.y}}}const u={x:o.x,y:t.y};return{points:[r.arrowPoint,t,u,e.arrowPoint],midpoint:{x:c,y:u.y}}};class Ft{constructor(e){i(this,"path");i(this,"midpoint");const{from:t,to:o,hasSourceArrow:s,hasTargetArrow:n,arrowLength:a,arrowOffset:h,fromDir:d,toDir:c,roundness:u}=e,l=s?E({x:t.x+a,y:t.y},d,t):t,g=n?E({x:o.x-a,y:o.y},c,o):o,p=a+h,w=E({x:t.x+p,y:t.y},d,t),y=E({x:o.x-p,y:o.y},c,o),f=It({arrowPoint:l,linePoint:w,dirY:d.y},{arrowPoint:g,linePoint:y,dirY:c.y});this.path=$(f.points,u),this.midpoint=f.midpoint}}class ee{constructor(e){i(this,"path");i(this,"midpoint");const{side:t,arrowLength:o,arrowOffset:s,dir:n,origin:a,hasArrow:h}=e,d=o+s,c=d+2*t,l=[{x:o,y:0},{x:d,y:0},{x:d,y:t},{x:c,y:t},{x:c,y:-t},{x:d,y:-t},{x:d,y:0},{x:o,y:0}].map(p=>E(p,n,{x:0,y:0})).map(p=>({x:p.x+a.x,y:p.y+a.y})),g=`M ${a.x} ${a.y} L ${l[0].x} ${l[0].y} `;this.path=`${h?"":g}${$(l,e.roundness)}`,this.midpoint={x:(l[3].x+l[4].x)/2,y:(l[3].y+l[4].y)/2}}}class Ut{constructor(e){i(this,"path");i(this,"midpoint");const{arrowLength:t,radius:o,smallRadius:s,dir:n,origin:a,hasArrow:h}=e,d=s+o,c=s*o/d,u=Math.sqrt(d*d-s*s),l=u*s/d,g=u+o+t,p=t+l,y=[{x:t,y:0},{x:p,y:c},{x:p,y:-c},{x:g,y:0}].map(x=>E(x,n,{x:0,y:0})).map(x=>({x:x.x+a.x,y:x.y+a.y})),f=[`M ${y[0].x} ${y[0].y}`,`A ${s} ${s} 0 0 1 ${y[1].x} ${y[1].y}`,`A ${o} ${o} 0 1 0 ${y[2].x} ${y[2].y}`,`A ${s} ${s} 0 0 1 ${y[0].x} ${y[0].y}`].join(" "),m=`M ${a.x} ${a.y} L ${y[0].x} ${y[0].y} `;this.path=`${h?"":m}${f}`,this.midpoint=y[3]}}const $t=(r,e,t)=>{const o=Math.max(r.y,e.y),s=Math.min(r.y,e.y);return(t>=0?o:s)+t};class Bt{constructor(e){i(this,"path");i(this,"midpoint");const{hasSourceArrow:t,hasTargetArrow:o,arrowLength:s,from:n,to:a,fromDir:h,toDir:d,arrowOffset:c,roundness:u,detourDistance:l}=e,g=t?E({x:n.x+s,y:n.y},h,n):n,p=o?E({x:a.x-s,y:a.y},d,a):a,w=s+c,y=E({x:n.x+w,y:n.y},h,n),f=E({x:a.x-w,y:a.y},d,a),m=$t(y,f,l);this.midpoint={x:(y.x+f.x)/2,y:m},this.path=$([g,y,{x:y.x,y:m},{x:f.x,y:m},f,p],u)}}const Ot=(r,e,t)=>{const o=Math.max(r.x,e.x),s=Math.min(r.x,e.x);return(t>=0?o:s)+t};class Wt{constructor(e){i(this,"path");i(this,"midpoint");const{hasSourceArrow:t,hasTargetArrow:o,arrowLength:s,fromDir:n,toDir:a,from:h,to:d,arrowOffset:c,detourDistance:u,roundness:l}=e,g=t?E({x:h.x+s,y:h.y},n,h):h,p=o?E({x:d.x-s,y:d.y},a,d):d,w=s+c,y=E({x:h.x+w,y:h.y},n,h),f=E({x:d.x-w,y:d.y},a,d),m=Ot(y,f,u);this.midpoint={x:m,y:(y.y+f.y)/2},this.path=$([g,y,{x:m,y:y.y},{x:m,y:f.y},f,p],l)}}const v=Object.freeze({color:"#777777",width:1,arrowLength:20,polygonArrowRadius:4,circleArrowRadius:8,wedgeArrowSmallRadius:20,wedgeArrowRadius:100,wedgeArrowAngle:Math.PI/12,arrowOffset:15,hasSourceArrow:!1,hasTargetArrow:!1,cycleRadius:30,cycleSquareSide:30,roundness:10,detourDistance:100,detourDirection:-Math.PI/2,detourDirectionVertical:0,smallCycleRadius:15,curvature:90,interactiveWidth:10,portOffset:0}),Ue=r=>({x:Math.cos(r),y:Math.sin(r)});class G{constructor(e){i(this,"svg");i(this,"group",document.createElementNS("http://www.w3.org/2000/svg","g"));i(this,"line");i(this,"sourceArrow",null);i(this,"targetArrow",null);i(this,"onAfterRender");i(this,"afterRenderEmitter");i(this,"arrowRenderer");this.params=e,[this.afterRenderEmitter,this.onAfterRender]=M(),this.arrowRenderer=this.params.arrowRenderer,this.svg=Ve(e.color),this.svg.appendChild(this.group),this.line=Ie(e.width),this.group.appendChild(this.line),e.hasSourceArrow&&(this.sourceArrow=X(),this.group.appendChild(this.sourceArrow)),e.hasTargetArrow&&(this.targetArrow=X(),this.group.appendChild(this.targetArrow))}render(e){const{x:t,y:o,width:s,height:n,from:a,to:h}=Le(e.from,e.to,this.params.padding);Fe(this.svg,{x:t,y:o,width:s,height:n});const d=Ue(e.from.direction),c=Ue(e.to.direction);let u={x:-c.x,y:-c.y},l;e.category===T.PortCycle?(l=this.params.createCyclePath,u=d):e.category===T.NodeCycle?l=this.params.createDetourPath:l=this.params.createLinePath;const g=l(a,h,d,c);this.line.setAttribute("d",g.path);let p=null;this.sourceArrow&&(p=this.arrowRenderer({direction:d,shift:a,arrowLength:this.params.arrowLength}),this.sourceArrow.setAttribute("d",p));let w=null;this.targetArrow&&(w=this.arrowRenderer({direction:u,shift:h,arrowLength:this.params.arrowLength}),this.targetArrow.setAttribute("d",w)),this.afterRenderEmitter.emit({edgePath:g,sourceArrowPath:p,targetArrowPath:w})}}const zt=r=>e=>{const o=[{x:0,y:0},{x:e.arrowLength,y:r.radius},{x:e.arrowLength,y:-r.radius}].map(h=>E(h,e.direction,{x:0,y:0})).map(h=>({x:h.x+e.shift.x,y:h.y+e.shift.y})),s=`M ${o[0].x} ${o[0].y}`,n=`L ${o[1].x} ${o[1].y}`,a=`L ${o[2].x} ${o[2].y}`;return`${s} ${n} ${a} Z`},kt=r=>e=>{const t=r.radius,o=e.arrowLength,s=(o*o+2*o*t)/(2*t),n=s+t,a=o+t-t*(o+t)/n,h=t*s/n,c=[{x:0,y:0},{x:a,y:-h},{x:a,y:h}].map(w=>E(w,e.direction,{x:0,y:0})).map(w=>({x:w.x+e.shift.x,y:w.y+e.shift.y})),u=`M ${c[0].x} ${c[0].y}`,l=`A ${s} ${s} 0 0 0 ${c[1].x} ${c[1].y}`,g=`A ${t} ${t} 0 0 0 ${c[2].x} ${c[2].y}`,p=`A ${s} ${s} 0 0 0 ${c[0].x} ${c[0].y}`;return`${u} ${l} ${g} ${p}`},Ht=r=>e=>{const t=r.smallRadius,o=r.radius,s=E({x:e.arrowLength,y:0},{x:Math.cos(r.angle),y:Math.sin(r.angle)},{x:e.arrowLength+r.smallRadius,y:0}),a=[{x:0,y:0},{x:s.x,y:-s.y},s].map(l=>E(l,e.direction,{x:0,y:0})).map(l=>({x:l.x+e.shift.x,y:l.y+e.shift.y})),h=`M ${a[0].x} ${a[0].y}`,d=`A ${o} ${o} 0 0 1 ${a[1].x} ${a[1].y}`,c=`A ${t} ${t} 0 0 1 ${a[2].x} ${a[2].y}`,u=`A ${o} ${o} 0 0 1 ${a[0].x} ${a[0].y}`;return`${h} ${d} ${c} ${u}`},O=r=>{if(typeof r=="function")return r;switch(r.type){case"triangle":return zt({radius:r.radius??v.polygonArrowRadius});case"arc":return kt({radius:r.radius??v.circleArrowRadius});default:return Ht({smallRadius:r.smallRadius??v.wedgeArrowSmallRadius,angle:r.angle??v.wedgeArrowAngle,radius:r.radius??v.wedgeArrowRadius})}},W=50;class $e{constructor(e){i(this,"svg");i(this,"group");i(this,"line");i(this,"sourceArrow");i(this,"targetArrow");i(this,"onAfterRender");i(this,"arrowLength");i(this,"curvature");i(this,"portCycleRadius");i(this,"portCycleSmallRadius");i(this,"detourDirection");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");i(this,"pathShape");i(this,"createCyclePath",(e,t,o)=>new Ut({origin:e,dir:o,radius:this.portCycleRadius,smallRadius:this.portCycleSmallRadius,arrowLength:this.arrowLength,hasArrow:this.hasSourceArrow||this.hasTargetArrow}));i(this,"createDetourPath",(e,t,o,s)=>new Mt({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,detourDir:this.detourDirection,detourDistance:this.detourDistance,curvature:this.curvature,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));i(this,"createLinePath",(e,t,o,s)=>new Tt({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,curvature:this.curvature,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??v.arrowLength,this.curvature=(e==null?void 0:e.curvature)??v.curvature,this.portCycleRadius=(e==null?void 0:e.cycleRadius)??v.cycleRadius,this.portCycleSmallRadius=(e==null?void 0:e.smallCycleRadius)??v.smallCycleRadius,this.detourDirection=(e==null?void 0:e.detourDirection)??v.detourDirection,this.detourDistance=(e==null?void 0:e.detourDistance)??v.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??v.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??v.hasTargetArrow,this.pathShape=new G({color:(e==null?void 0:e.color)??v.color,width:(e==null?void 0:e.width)??v.width,arrowRenderer:O((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath,padding:W}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){this.pathShape.render(e)}}const Be=r=>Math.cos(r)>0?0:Math.PI;class Oe{constructor(e){i(this,"svg");i(this,"group");i(this,"line");i(this,"sourceArrow");i(this,"targetArrow");i(this,"onAfterRender");i(this,"arrowLength");i(this,"arrowOffset");i(this,"roundness");i(this,"cycleSquareSide");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");i(this,"pathShape");i(this,"createCyclePath",(e,t,o)=>new ee({origin:e,dir:o,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasArrow:this.hasSourceArrow||this.hasTargetArrow}));i(this,"createDetourPath",(e,t,o,s)=>new Bt({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));i(this,"createLinePath",(e,t,o,s)=>new Rt({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??v.arrowLength,this.arrowOffset=(e==null?void 0:e.arrowOffset)??v.arrowOffset,this.cycleSquareSide=(e==null?void 0:e.cycleSquareSide)??v.cycleSquareSide;const t=(e==null?void 0:e.roundness)??v.roundness;this.roundness=Math.min(t,this.arrowOffset,this.cycleSquareSide/2),this.detourDistance=(e==null?void 0:e.detourDistance)??v.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??v.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??v.hasTargetArrow,this.pathShape=new G({color:(e==null?void 0:e.color)??v.color,width:(e==null?void 0:e.width)??v.width,arrowRenderer:O((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath,padding:W}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){const{from:t,to:o,category:s}=e;this.pathShape.render({category:s,from:{...t,direction:Be(t.direction)},to:{...o,direction:Be(o.direction)}})}}class We{constructor(e){i(this,"svg");i(this,"group");i(this,"line");i(this,"sourceArrow");i(this,"targetArrow");i(this,"onAfterRender");i(this,"arrowLength");i(this,"arrowOffset");i(this,"roundness");i(this,"cycleSquareSide");i(this,"detourDirection");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");i(this,"pathShape");i(this,"createCyclePath",(e,t,o)=>new ee({origin:e,dir:o,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasArrow:this.hasSourceArrow||this.hasTargetArrow}));i(this,"createDetourPath",(e,t,o,s)=>new Lt({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDir:this.detourDirection,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));i(this,"createLinePath",(e,t,o,s)=>new Vt({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??v.arrowLength,this.arrowOffset=(e==null?void 0:e.arrowOffset)??v.arrowOffset,this.cycleSquareSide=(e==null?void 0:e.cycleSquareSide)??v.cycleSquareSide;const t=(e==null?void 0:e.roundness)??v.roundness;this.roundness=Math.min(t,this.arrowOffset,this.cycleSquareSide/2),this.detourDirection=(e==null?void 0:e.detourDirection)??v.detourDirection,this.detourDistance=(e==null?void 0:e.detourDistance)??v.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??v.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??v.hasTargetArrow,this.pathShape=new G({color:(e==null?void 0:e.color)??v.color,width:(e==null?void 0:e.width)??v.width,arrowRenderer:O((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath,padding:W}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){this.pathShape.render(e)}}const ze=Math.PI/2,ke=r=>Math.sin(r)>0?ze:-ze;class He{constructor(e){i(this,"svg");i(this,"group");i(this,"line");i(this,"sourceArrow");i(this,"targetArrow");i(this,"onAfterRender");i(this,"arrowLength");i(this,"arrowOffset");i(this,"roundness");i(this,"cycleSquareSide");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");i(this,"pathShape");i(this,"createCyclePath",(e,t,o)=>new ee({origin:e,dir:o,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasArrow:this.hasSourceArrow||this.hasTargetArrow}));i(this,"createDetourPath",(e,t,o,s)=>new Wt({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));i(this,"createLinePath",(e,t,o,s)=>new Ft({from:e,to:t,fromDir:o,toDir:s,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??v.arrowLength,this.arrowOffset=(e==null?void 0:e.arrowOffset)??v.arrowOffset,this.cycleSquareSide=(e==null?void 0:e.cycleSquareSide)??v.cycleSquareSide;const t=(e==null?void 0:e.roundness)??v.roundness;this.roundness=Math.min(t,this.arrowOffset,this.cycleSquareSide/2),this.detourDistance=(e==null?void 0:e.detourDistance)??v.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??v.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??v.hasTargetArrow,this.pathShape=new G({color:(e==null?void 0:e.color)??v.color,width:(e==null?void 0:e.width)??v.width,arrowRenderer:O((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath,padding:W}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){const{from:t,to:o,category:s}=e;this.pathShape.render({category:s,from:{...t,direction:ke(t.direction)},to:{...o,direction:ke(o.direction)}})}}const Ye=r=>typeof r=="number"?()=>r:r,Xe=v.portOffset;class te{constructor(e){i(this,"svg");i(this,"group",document.createElementNS("http://www.w3.org/2000/svg","g"));i(this,"line");i(this,"sourceArrow",null);i(this,"targetArrow",null);i(this,"color");i(this,"width");i(this,"arrowLength");i(this,"sourceOffsetFn");i(this,"targetOffsetFn");i(this,"onAfterRender");i(this,"afterRenderEmitter");i(this,"arrowRenderer");[this.afterRenderEmitter,this.onAfterRender]=M(),this.color=(e==null?void 0:e.color)??v.color,this.width=(e==null?void 0:e.width)??v.width,this.arrowLength=(e==null?void 0:e.arrowLength)??v.arrowLength,this.arrowRenderer=O((e==null?void 0:e.arrowRenderer)??{}),this.sourceOffsetFn=Ye((e==null?void 0:e.sourceOffset)??Xe),this.targetOffsetFn=Ye((e==null?void 0:e.targetOffset)??Xe),this.svg=Ve(this.color),this.svg.appendChild(this.group),this.line=Ie(this.width),this.group.appendChild(this.line),e!=null&&e.hasSourceArrow&&(this.sourceArrow=X(),this.group.appendChild(this.sourceArrow)),e!=null&&e.hasTargetArrow&&(this.targetArrow=X(),this.group.appendChild(this.targetArrow))}render(e){const{x:t,y:o,width:s,height:n,from:a,to:h}=Le(e.from,e.to,W);Fe(this.svg,{x:t,y:o,width:s,height:n});const d=h.x-a.x,c=h.y-a.y,u=Math.sqrt(d*d+c*c);if(u===0){this.renderEmpty(a);return}const l={x:d/u,y:c/u},g=this.sourceOffsetFn({direction:{x:l.x,y:l.y},radius:{horizontal:e.from.width/2,vertical:e.from.height/2}}),p=this.targetOffsetFn({direction:{x:-l.x,y:-l.y},radius:{horizontal:e.to.width/2,vertical:e.to.height/2}}),w={x:a.x+g*l.x,y:a.y+g*l.y},y={x:h.x-p*l.x,y:h.y-p*l.y},f=this.sourceArrow!==null?this.arrowLength:0,m={x:w.x+f*l.x,y:w.y+f*l.y},x=this.targetArrow!==null?this.arrowLength:0,N={x:y.x-x*l.x,y:y.y-x*l.y},D={x:(w.x+y.x)/2,y:(w.y+y.y)/2},R=`M ${m.x} ${m.y} L ${N.x} ${N.y}`;this.line.setAttribute("d",R);let F=null,U=null;if(this.sourceArrow){const L={x:l.x*g+a.x,y:l.y*g+a.y};F=this.arrowRenderer({direction:l,shift:L,arrowLength:this.arrowLength}),this.sourceArrow.setAttribute("d",F)}if(this.targetArrow){const L={x:l.x*p,y:l.y*p};U=this.arrowRenderer({direction:{x:-l.x,y:-l.y},shift:{x:h.x-L.x,y:h.y-L.y},arrowLength:this.arrowLength}),this.targetArrow.setAttribute("d",U)}this.afterRenderEmitter.emit({edgePath:{path:R,midpoint:D},sourceArrowPath:F,targetArrowPath:U})}renderEmpty(e){const t="";let o=null,s=null;this.line.setAttribute("d",t),this.sourceArrow!==null&&(o="",this.sourceArrow.setAttribute("d",o)),this.targetArrow!==null&&(s="",this.targetArrow.setAttribute("d",s)),this.afterRenderEmitter.emit({edgePath:{path:t,midpoint:e},sourceArrowPath:o,targetArrowPath:s})}}const Yt=r=>{const{direction:e,radius:t}=r,{x:o,y:s}=e,{horizontal:n,vertical:a}=t,h=s/o,d=Math.abs(a/h),c=Math.abs(n*h),u=Math.min(n,d),l=Math.min(a,c);return Math.sqrt(u*u+l*l)},Xt=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","g");return r.style.pointerEvents="auto",r.style.cursor="pointer",r},Gt=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","path");return e.setAttribute("stroke","transparent"),e.setAttribute("stroke-width",`${r}`),e.setAttribute("fill","none"),e.setAttribute("stroke-linecap","round"),e},Ge=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","path");return e.setAttribute("stroke-linejoin","round"),e.setAttribute("stroke-width",`${r}`),e.setAttribute("fill","transparent"),e.setAttribute("stroke","transparent"),e};class je extends Error{constructor(e){super(e),this.name="InteractiveEdgeError"}}class re{constructor(e,t){i(this,"svg");i(this,"group");i(this,"line");i(this,"sourceArrow");i(this,"targetArrow");i(this,"handle",Xt());i(this,"onAfterRender");i(this,"interactiveLine");i(this,"interactiveSourceArrow",null);i(this,"interactiveTargetArrow",null);if(this.baseEdge=e,e instanceof re)throw new je("interactive edge can be configured only once");this.svg=this.baseEdge.svg,this.group=this.baseEdge.group,this.line=this.baseEdge.line,this.sourceArrow=this.baseEdge.sourceArrow,this.targetArrow=this.baseEdge.targetArrow,this.onAfterRender=this.baseEdge.onAfterRender;const o=(t==null?void 0:t.distance)??v.interactiveWidth;this.interactiveLine=Gt(o),this.handle.appendChild(this.interactiveLine),this.sourceArrow&&(this.interactiveSourceArrow=Ge(o),this.handle.appendChild(this.interactiveSourceArrow)),this.targetArrow&&(this.interactiveTargetArrow=Ge(o),this.handle.appendChild(this.interactiveTargetArrow)),this.group.appendChild(this.handle),this.baseEdge.onAfterRender.subscribe(s=>{this.interactiveLine.setAttribute("d",s.edgePath.path),this.interactiveSourceArrow&&this.interactiveSourceArrow.setAttribute("d",s.sourceArrowPath),this.interactiveTargetArrow&&this.interactiveTargetArrow.setAttribute("d",s.targetArrowPath)})}render(e){this.baseEdge.render(e)}}class jt{constructor(e,t){i(this,"group");i(this,"line");i(this,"sourceArrow");i(this,"targetArrow");i(this,"onAfterRender");i(this,"svg");this.baseShape=e,this.midpointElement=t,this.svg=this.baseShape.svg,this.group=this.baseShape.group,this.line=this.baseShape.line,this.sourceArrow=this.baseShape.sourceArrow,this.targetArrow=this.baseShape.targetArrow,this.onAfterRender=this.baseShape.onAfterRender,this.svg.append(this.midpointElement),this.baseShape.onAfterRender.subscribe(o=>{const s=o.edgePath.midpoint,n=`translate(${s.x}px, ${s.y}px)`;this.midpointElement.style.setProperty("transform",n)})}render(e){this.baseShape.render(e)}}class Je{constructor(e){i(this,"onAfterNodeAdded");i(this,"onAfterNodeUpdated");i(this,"onAfterNodePriorityUpdated");i(this,"onBeforeNodeRemoved");i(this,"onAfterPortMarked");i(this,"onAfterPortUpdated");i(this,"onBeforePortUnmarked");i(this,"onAfterEdgeAdded");i(this,"onAfterEdgeShapeUpdated");i(this,"onAfterEdgeUpdated");i(this,"onAfterEdgePriorityUpdated");i(this,"onBeforeEdgeRemoved");i(this,"onBeforeClear");this.graphStore=e,this.onAfterNodeAdded=this.graphStore.onAfterNodeAdded,this.onAfterNodeUpdated=this.graphStore.onAfterNodeUpdated,this.onAfterNodePriorityUpdated=this.graphStore.onAfterNodePriorityUpdated,this.onBeforeNodeRemoved=this.graphStore.onBeforeNodeRemoved,this.onAfterPortMarked=this.graphStore.onAfterPortAdded,this.onAfterPortUpdated=this.graphStore.onAfterPortUpdated,this.onBeforePortUnmarked=this.graphStore.onBeforePortRemoved,this.onAfterEdgeAdded=this.graphStore.onAfterEdgeAdded,this.onAfterEdgeShapeUpdated=this.graphStore.onAfterEdgeShapeUpdated,this.onAfterEdgeUpdated=this.graphStore.onAfterEdgeUpdated,this.onAfterEdgePriorityUpdated=this.graphStore.onAfterEdgePriorityUpdated,this.onBeforeEdgeRemoved=this.graphStore.onBeforeEdgeRemoved,this.onBeforeClear=this.graphStore.onBeforeClear}hasNode(e){return this.graphStore.hasNode(e)}getNode(e){const t=this.graphStore.getNode(e),{payload:o}=t;return{element:t.element,x:o.x,y:o.y,centerFn:o.centerFn,priority:o.priority}}findNodeIdByElement(e){return this.graphStore.findNodeIdByElement(e)}getAllNodeIds(){return this.graphStore.getAllNodeIds()}hasPort(e){return this.graphStore.hasPort(e)}getPort(e){const t=this.graphStore.getPort(e);return{element:t.element,direction:t.payload.direction,nodeId:t.nodeId}}getAllPortIds(){return this.graphStore.getAllPortIds()}getNodePortIds(e){return this.graphStore.getNodePortIds(e)}findPortIdsByElement(e){return this.graphStore.findPortIdsByElement(e)}getAllEdgeIds(){return this.graphStore.getAllEdgeIds()}hasEdge(e){return this.graphStore.hasEdge(e)}getEdge(e){const t=this.graphStore.getEdge(e),{payload:o}=t;return{from:t.from,to:t.to,priority:o.priority,shape:o.shape}}findEdgeIdByElement(e){return this.graphStore.findEdgeIdByElement(e)}getPortIncomingEdgeIds(e){return this.graphStore.getPortIncomingEdgeIds(e)}getPortOutgoingEdgeIds(e){return this.graphStore.getPortOutgoingEdgeIds(e)}getPortCycleEdgeIds(e){return this.graphStore.getPortCycleEdgeIds(e)}getPortAdjacentEdgeIds(e){return this.graphStore.getPortAdjacentEdgeIds(e)}getNodeIncomingEdgeIds(e){return this.graphStore.getNodeIncomingEdgeIds(e)}getNodeOutgoingEdgeIds(e){return this.graphStore.getNodeOutgoingEdgeIds(e)}getNodeCycleEdgeIds(e){return this.graphStore.getNodeCycleEdgeIds(e)}getNodeAdjacentEdgeIds(e){return this.graphStore.getNodeAdjacentEdgeIds(e)}}class oe{constructor(e){i(this,"counter",0);this.checkExists=e}create(e){if(e!==void 0)return e;for(;this.checkExists(this.counter);)this.counter++;return this.counter}reset(){this.counter=0}}class Ke{constructor(e,t,o){i(this,"nodeIdGenerator",new oe(e=>this.graphStore.hasNode(e)));i(this,"portIdGenerator",new oe(e=>this.graphStore.hasPort(e)));i(this,"edgeIdGenerator",new oe(e=>this.graphStore.hasEdge(e)));i(this,"onAfterNodeAdded",e=>{this.htmlView.attachNode(e)});i(this,"onAfterNodeUpdated",e=>{this.htmlView.updateNodePosition(e),this.graphStore.getNodeAdjacentEdgeIds(e).forEach(t=>{this.htmlView.renderEdge(t)})});i(this,"onAfterNodePriorityUpdated",e=>{this.htmlView.updateNodePriority(e)});i(this,"onBeforeNodeRemoved",e=>{this.graphStore.getNodePortIds(e).forEach(t=>{this.unmarkPort(t)}),this.htmlView.detachNode(e)});i(this,"onAfterPortUpdated",e=>{this.graphStore.getPortAdjacentEdgeIds(e).forEach(t=>{this.htmlView.renderEdge(t)})});i(this,"onBeforePortUnmarked",e=>{this.graphStore.getPortAdjacentEdgeIds(e).forEach(t=>{this.removeEdge(t)})});i(this,"onAfterEdgeAdded",e=>{this.htmlView.attachEdge(e)});i(this,"onAfterEdgeShapeUpdated",e=>{this.htmlView.updateEdgeShape(e)});i(this,"onAfterEdgeUpdated",e=>{this.htmlView.renderEdge(e)});i(this,"onAfterEdgePriorityUpdated",e=>{this.htmlView.updateEdgePriority(e)});i(this,"onBeforeEdgeRemoved",e=>{this.htmlView.detachEdge(e)});i(this,"onBeforeClear",()=>{this.nodeIdGenerator.reset(),this.portIdGenerator.reset(),this.edgeIdGenerator.reset(),this.htmlView.clear()});this.graphStore=e,this.htmlView=t,this.params=o,this.graphStore.onAfterNodeAdded.subscribe(this.onAfterNodeAdded),this.graphStore.onAfterNodeUpdated.subscribe(this.onAfterNodeUpdated),this.graphStore.onAfterNodePriorityUpdated.subscribe(this.onAfterNodePriorityUpdated),this.graphStore.onBeforeNodeRemoved.subscribe(this.onBeforeNodeRemoved),this.graphStore.onAfterPortUpdated.subscribe(this.onAfterPortUpdated),this.graphStore.onBeforePortRemoved.subscribe(this.onBeforePortUnmarked),this.graphStore.onAfterEdgeAdded.subscribe(this.onAfterEdgeAdded),this.graphStore.onAfterEdgeShapeUpdated.subscribe(this.onAfterEdgeShapeUpdated),this.graphStore.onAfterEdgeUpdated.subscribe(this.onAfterEdgeUpdated),this.graphStore.onAfterEdgePriorityUpdated.subscribe(this.onAfterEdgePriorityUpdated),this.graphStore.onBeforeEdgeRemoved.subscribe(this.onBeforeEdgeRemoved),this.graphStore.onBeforeClear.subscribe(this.onBeforeClear)}addNode(e){const t=this.nodeIdGenerator.create(e.id);if(this.graphStore.addNode({id:t,element:e.element,x:e.x??null,y:e.y??null,centerFn:e.centerFn??this.params.nodes.centerFn,priority:e.priority??this.params.nodes.priorityFn()}),e.ports!==void 0)for(const o of e.ports)this.markPort({id:o.id,element:o.element,nodeId:t,direction:o.direction})}updateNode(e,t){this.graphStore.updateNode(e,t??{})}removeNode(e){this.graphStore.removeNode(e)}markPort(e){const t=this.portIdGenerator.create(e.id);this.graphStore.addPort({id:t,element:e.element,nodeId:e.nodeId,direction:e.direction??this.params.ports.direction})}updatePort(e,t){this.graphStore.updatePort(e,t??{})}unmarkPort(e){this.graphStore.removePort(e)}addEdge(e){const t=this.edgeIdGenerator.create(e.id);this.graphStore.addEdge({id:t,from:e.from,to:e.to,shape:e.shape??this.params.edges.shapeFactory(t),priority:e.priority??this.params.edges.priorityFn()})}updateEdge(e,t){this.graphStore.updateEdge(e,t??{})}removeEdge(e){this.graphStore.removeEdge(e)}clear(){this.graphStore.clear()}destroy(){this.graphStore.onAfterNodeAdded.unsubscribe(this.onAfterNodeAdded),this.graphStore.onAfterNodeUpdated.unsubscribe(this.onAfterNodeUpdated),this.graphStore.onAfterNodePriorityUpdated.unsubscribe(this.onAfterNodePriorityUpdated),this.graphStore.onBeforeNodeRemoved.unsubscribe(this.onBeforeNodeRemoved),this.graphStore.onAfterPortUpdated.unsubscribe(this.onAfterPortUpdated),this.graphStore.onBeforePortRemoved.unsubscribe(this.onBeforePortUnmarked),this.graphStore.onAfterEdgeAdded.unsubscribe(this.onAfterEdgeAdded),this.graphStore.onAfterEdgeShapeUpdated.unsubscribe(this.onAfterEdgeShapeUpdated),this.graphStore.onAfterEdgeUpdated.unsubscribe(this.onAfterEdgeUpdated),this.graphStore.onAfterEdgePriorityUpdated.unsubscribe(this.onAfterEdgePriorityUpdated),this.graphStore.onBeforeEdgeRemoved.unsubscribe(this.onBeforeEdgeRemoved),this.graphStore.onBeforeClear.unsubscribe(this.onBeforeClear),this.htmlView.destroy()}}const Jt=r=>{setTimeout(()=>{r()})},Qe=r=>{queueMicrotask(()=>{r()})},se=r=>{r()};class Ze{constructor(e){i(this,"onBeforeUpdated");i(this,"onAfterUpdated");i(this,"onAfterResize");this.viewportStore=e,this.onBeforeUpdated=this.viewportStore.onBeforeUpdated,this.onAfterUpdated=this.viewportStore.onAfterUpdated,this.onAfterResize=this.viewportStore.onAfterResize}getViewportMatrix(){return{...this.viewportStore.getViewportMatrix()}}getContentMatrix(){return{...this.viewportStore.getContentMatrix()}}getDimensions(){return this.viewportStore.getDimensions()}createContentCoords(e){return this.viewportStore.createContentCoords(e)}createViewportCoords(e){return this.viewportStore.createViewportCoords(e)}}const Kt=(r,e)=>Symbol.iterator in r?{minContentScale:e.focus.minContentScale,nodes:r,contentPadding:e.focus.contentPadding,animationDuration:e.focus.animationDuration}:{minContentScale:r.minContentScale??e.focus.minContentScale,nodes:r.nodes??[],contentPadding:r.contentPadding??r.contentOffset??e.focus.contentPadding,animationDuration:r.animationDuration??e.focus.animationDuration},_e=(r,e,t)=>({scale:r.scale,x:r.x+r.scale*e,y:r.y+r.scale*t}),qe=(r,e,t,o)=>({scale:r.scale*e,x:r.scale*(1-e)*t+r.x,y:r.scale*(1-e)*o+r.y});class et{constructor(e,t,o,s){this.graphStore=e,this.viewportStore=t,this.params=o,this.win=s}patchViewportMatrix(e){this.viewportStore.patchViewportMatrix(e)}patchContentMatrix(e){this.viewportStore.patchContentMatrix(e)}center(e,t){const{width:o,height:s}=this.viewportStore.getDimensions(),n={x:o/2,y:s/2},a=this.viewportStore.getViewportMatrix(),h=this.viewportStore.createViewportCoords(e),d=h.x-n.x,c=h.y-n.y;let u=_e(a,d,c);const l=t==null?void 0:t.contentScale;if(l!==void 0){const p=1/l;u=qe(u,p/a.scale,n.x,n.y)}const g=(t==null?void 0:t.animationDuration)??0;g>0?this.animateNavigation(a,u,g):this.viewportStore.patchViewportMatrix(u)}focus(e){const t=Kt(e??{},this.params);this.params.focus.schedule(()=>{this.navigate(t)})}destroy(){this.viewportStore.destroy()}navigate(e){let t=1/0,o=-1/0,s=1/0,n=-1/0,a=0;const h=new Set;for(const d of e.nodes)h.add(d);if(this.graphStore.getAllNodeIds().forEach(d=>{const{payload:c}=this.graphStore.getNode(d);c.x!==null&&c.y!==null&&(h.size===0||h.has(d))&&(t=Math.min(c.x,t),o=Math.max(c.x,o),s=Math.min(c.y,s),n=Math.max(c.y,n),a++)}),a>0){t-=e.contentPadding,s-=e.contentPadding,o+=e.contentPadding,n+=e.contentPadding;const d={x:(t+o)/2,y:(s+n)/2},{scale:c}=this.viewportStore.getContentMatrix(),u=(n-s)*c,l=(o-t)*c,{width:g,height:p}=this.viewportStore.getDimensions(),w=Math.max(l/g,u/p),y=w>1?c/w:c,f=Math.max(y,e.minContentScale);this.center(d,{contentScale:f,animationDuration:e.animationDuration})}}animateNavigation(e,t,o){let s;const n={scale:t.scale-e.scale,x:t.x-e.x,y:t.y-e.y},a=h=>{s===void 0&&(s=h);const d=Math.min((h-s)/o,1);this.viewportStore.patchViewportMatrix({scale:e.scale+d*n.scale,x:e.x+d*n.x,y:e.y+d*n.y}),d<1&&this.win.requestAnimationFrame(a)};this.win.requestAnimationFrame(a)}}const tt=(r,e,t)=>{const o=new Pe,s=new Je(o),n=new Ze(e),a=new Se(o,e,r),h={nodes:{centerFn:Re,priorityFn:()=>0},edges:{shapeFactory:()=>new te,priorityFn:()=>0},ports:{direction:0}},d=new Ke(o,a,h),c={focus:{contentPadding:0,minContentScale:0,schedule:se,animationDuration:0}},u=new et(o,e,c,t);return new be(s,n,d,u)};class j{constructor(e,t,o,s){i(this,"onAfterPortMarked",e=>{const t=this.canvas.graph.getPort(e);this.canvas.graph.findPortIdsByElement(t.element).length===1&&this.hookPortEvents(t.element)});i(this,"onBeforePortUnmarked",e=>{const t=this.canvas.graph.getPort(e);this.canvas.graph.findPortIdsByElement(t.element).length===1&&this.unhookPortEvents(t.element)});i(this,"onPortMouseDown",e=>{const t=e;if(!this.params.mouseDownEventVerifier(t))return;const o=e.currentTarget,s=this.canvas.graph.findPortIdsByElement(o)[0];this.params.onPointerDownVerifier(s,{x:t.clientX,y:t.clientY})&&(e.stopPropagation(),this.window.addEventListener("mousemove",this.onWindowMouseMove,{passive:!0}),this.window.addEventListener("mouseup",this.onWindowMouseUp,{passive:!0}))});i(this,"onWindowMouseMove",e=>{if(!this.pointInsideVerifier.verify(e.clientX,e.clientY)){this.removeWindowMouseListeners(),this.params.onPointerOutside();return}this.params.onPointerMove({x:e.clientX,y:e.clientY})});i(this,"onWindowMouseUp",e=>{this.params.mouseUpEventVerifier(e)&&(this.removeWindowMouseListeners(),this.params.onPointerUp({x:e.clientX,y:e.clientY}))});i(this,"onPortTouchStart",e=>{const t=e;if(t.touches.length!==1)return;const o=t.touches[0],s=e.currentTarget,n=this.canvas.graph.findPortIdsByElement(s)[0];this.params.onPointerDownVerifier(n,{x:o.clientX,y:o.clientY})&&(e.stopPropagation(),this.window.addEventListener("touchmove",this.onWindowTouchMove,{passive:!0}),this.window.addEventListener("touchend",this.onWindowTouchFinish,{passive:!0}),this.window.addEventListener("touchcancel",this.onWindowTouchFinish,{passive:!0}))});i(this,"onWindowTouchMove",e=>{const t=e.touches[0];if(!this.pointInsideVerifier.verify(t.clientX,t.clientY)){this.removeWindowTouchListeners(),this.params.onPointerOutside();return}this.params.onPointerMove({x:t.clientX,y:t.clientY})});i(this,"onWindowTouchFinish",e=>{this.removeWindowTouchListeners();const t=e.changedTouches[0];this.params.onPointerUp({x:t.clientX,y:t.clientY})});i(this,"reset",()=>{this.canvas.graph.getAllPortIds().forEach(e=>{const t=this.canvas.graph.getPort(e);this.unhookPortEvents(t.element)})});i(this,"revert",()=>{this.removeWindowMouseListeners(),this.removeWindowTouchListeners(),this.reset()});this.canvas=e,this.window=t,this.pointInsideVerifier=o,this.params=s,this.canvas.graph.onAfterPortMarked.subscribe(this.onAfterPortMarked),this.canvas.graph.onBeforePortUnmarked.subscribe(this.onBeforePortUnmarked),this.canvas.graph.onBeforeClear.subscribe(this.reset),this.canvas.onBeforeDestroy.subscribe(this.revert)}static configure(e,t,o,s){new j(e,t,o,s)}hookPortEvents(e){e.addEventListener("mousedown",this.onPortMouseDown,{passive:!0}),e.addEventListener("touchstart",this.onPortTouchStart,{passive:!0})}unhookPortEvents(e){e.removeEventListener("mousedown",this.onPortMouseDown),e.removeEventListener("touchstart",this.onPortTouchStart)}removeWindowMouseListeners(){this.window.removeEventListener("mousemove",this.onWindowMouseMove),this.window.removeEventListener("mouseup",this.onWindowMouseUp)}removeWindowTouchListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}}class Qt{constructor(){i(this,"field","_htmlGraphTags")}has(e,t){const s=e[this.field];return s!==void 0&&s.has(t)}tag(e,t){const o=e,s=o[this.field];s===void 0?o[this.field]=new Set([t]):s.add(t)}}const ie=Symbol();class ne{constructor(e,t,o){i(this,"onSelected");i(this,"mouseDownEventVerifier");i(this,"mouseUpEventVerifier");i(this,"movementThreshold");i(this,"selectionCandidate",null);i(this,"previousMouse",null);i(this,"previousTouch",null);i(this,"movedDistance",0);i(this,"onElementMouseDown",e=>{const t=e;this.mouseDownEventVerifier(t)&&(this.selectionCandidate=t.currentTarget,this.previousMouse={x:t.clientX,y:t.clientY},this.movedDistance=0,this.window.addEventListener("mousemove",this.onWindowMouseMove,{passive:!0}),this.window.addEventListener("mouseup",this.onWindowMouseUp,{passive:!0}))});i(this,"onElementTouchStart",e=>{const t=e,o=t.touches;o.length===1&&(this.selectionCandidate=t.currentTarget,this.previousTouch=o[0],this.movedDistance=0,this.window.addEventListener("touchmove",this.onWindowTouchMove,{passive:!0}),this.window.addEventListener("touchend",this.onWindowTouchEnd,{passive:!0}),this.window.addEventListener("touchcancel",this.onWindowTouchCancel,{passive:!0}))});i(this,"onWindowMouseMove",e=>{const t=e;if(!this.pointInsideVerifier.verify(t.clientX,t.clientY)){this.removeWindowMouseListeners();return}const s=this.previousMouse,n=t.clientX-s.x,a=t.clientY-s.y;this.previousMouse={x:t.clientX,y:t.clientY},this.processMoveThresholdVerification(n,a,()=>{this.removeWindowMouseListeners()})});i(this,"onWindowMouseUp",e=>{const t=e;this.mouseUpEventVerifier(t)&&(this.removeWindowMouseListeners(),this.onSelected(this.selectionCandidate,e))});i(this,"onWindowTouchMove",e=>{const o=e.touches;if(o.length!==1){this.removeWindowTouchListeners();return}const s=o[0];if(!this.pointInsideVerifier.verify(s.clientX,s.clientY)){this.removeWindowTouchListeners();return}const a=this.previousTouch,h=s.clientX-a.clientX,d=s.clientY-a.clientY;this.previousTouch=s,this.processMoveThresholdVerification(h,d,()=>{this.removeWindowTouchListeners()})});i(this,"onWindowTouchEnd",e=>{this.removeWindowTouchListeners(),this.onSelected(this.selectionCandidate,e)});i(this,"onWindowTouchCancel",()=>{this.removeWindowTouchListeners()});this.window=e,this.pointInsideVerifier=t,this.mouseDownEventVerifier=o.mouseDownEventVerifier,this.mouseUpEventVerifier=o.mouseUpEventVerifier,this.onSelected=o.onSelected,this.movementThreshold=o.movementThreshold}enable(e){e.addEventListener("mousedown",this.onElementMouseDown),e.addEventListener("touchstart",this.onElementTouchStart)}disable(e){e.removeEventListener("mousedown",this.onElementMouseDown),e.removeEventListener("touchstart",this.onElementTouchStart),e===this.selectionCandidate&&(this.removeWindowMouseListeners(),this.removeWindowTouchListeners())}removeWindowMouseListeners(){this.window.removeEventListener("mousemove",this.onWindowMouseMove),this.window.removeEventListener("mouseup",this.onWindowMouseUp)}removeWindowTouchListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchEnd),this.window.removeEventListener("touchcancel",this.onWindowTouchCancel)}processMoveThresholdVerification(e,t,o){const s=Math.sqrt(e*e+t*t);this.movedDistance+=s,this.movedDistance>this.movementThreshold&&o()}}const ae=(r,e)=>({from:r.isDirect?r.staticPortId:e,to:r.isDirect?e:r.staticPortId});class rt{constructor(e,t){this.graph=e,this.connectionAllowedVerifier=t}resolve(e){let t,o=1/0;const{cursor:s}=e;return this.graph.getAllPortIds().forEach(n=>{const{element:a,direction:h,nodeId:d}=this.graph.getPort(n),{x:c,y:u}=this.graph.getNode(d);if(c===null||u===null)return;const l=ae(e,n);if(!this.connectionAllowedVerifier(l))return;const{top:g,left:p,width:w,height:y}=a.getBoundingClientRect(),f={x:p+w/2,y:g+y/2},m=s.x-f.x,x=s.y-f.y,N=Math.sqrt(m*m+x*x);N<o&&(o=N,t=h)}),t}}class Zt{constructor(e){this.direction=e}resolve(){return this.direction}}class he{constructor(e,t,o,s,n){i(this,"grabbedNode",null);i(this,"maxNodePriority",0);i(this,"graph");i(this,"onAfterNodeAdded",e=>{this.updateMaxNodePriority(e);const{element:t}=this.graph.getNode(e);t.addEventListener("mousedown",this.onMouseDown,{passive:!0}),t.addEventListener("touchstart",this.onTouchStart,{passive:!0})});i(this,"onAfterNodeUpdated",e=>{this.updateMaxNodePriority(e)});i(this,"onBeforeNodeRemoved",e=>{const{element:t}=this.graph.getNode(e);t.removeEventListener("mousedown",this.onMouseDown),t.removeEventListener("touchstart",this.onTouchStart)});i(this,"onMouseDown",e=>{const t=e;if(!this.params.mouseDownEventVerifier(t))return;const o=e.currentTarget,s=this.graph.findNodeIdByElement(o),n=this.graph.getNode(s);if(!this.params.nodeDragVerifier(s))return;this.params.onNodeDragStarted(s),e.stopPropagation();const h=this.calculateContentPoint({x:t.clientX,y:t.clientY});this.grabbedNode={nodeId:s,dx:h.x-n.x,dy:h.y-n.y},H(this.element,this.params.dragCursor),this.moveNodeOnTop(s),this.window.addEventListener("mousemove",this.onWindowMouseMove,{passive:!0}),this.window.addEventListener("mouseup",this.onWindowMouseUp,{passive:!0})});i(this,"onTouchStart",e=>{const t=e;if(t.touches.length!==1)return;e.stopPropagation();const o=t.touches[0],s=e.currentTarget,n=this.canvas.graph.findNodeIdByElement(s),a=this.graph.getNode(n);if(!this.params.nodeDragVerifier({nodeId:n,element:a.element,x:a.x,y:a.y}))return;const d=this.calculateContentPoint({x:o.clientX,y:o.clientY});this.grabbedNode={nodeId:n,dx:d.x-a.x,dy:d.y-a.y},this.moveNodeOnTop(n),this.window.addEventListener("touchmove",this.onWindowTouchMove,{passive:!0}),this.window.addEventListener("touchend",this.onWindowTouchFinish,{passive:!0}),this.window.addEventListener("touchcancel",this.onWindowTouchFinish,{passive:!0})});i(this,"onWindowMouseMove",e=>{if(!this.pointInsideVerifier.verify(e.clientX,e.clientY)){this.stopMouseDrag();return}this.grabbedNode!==null&&this.moveNode(this.grabbedNode,{x:e.clientX,y:e.clientY})});i(this,"onWindowMouseUp",e=>{this.params.mouseUpEventVerifier(e)&&this.stopMouseDrag()});i(this,"onWindowTouchMove",e=>{if(e.touches.length!==1)return;const t=e.touches[0];if(!this.pointInsideVerifier.verify(t.clientX,t.clientY)){this.stopTouchDrag();return}this.grabbedNode!==null&&this.moveNode(this.grabbedNode,{x:t.clientX,y:t.clientY})});i(this,"onWindowTouchFinish",()=>{this.stopTouchDrag()});i(this,"reset",()=>{this.canvas.graph.getAllNodeIds().forEach(e=>{const{element:t}=this.canvas.graph.getNode(e);t.removeEventListener("mousedown",this.onMouseDown),t.removeEventListener("touchstart",this.onTouchStart)}),this.maxNodePriority=0});i(this,"revert",()=>{this.reset(),this.removeMouseDragListeners(),this.removeTouchDragListeners()});this.canvas=e,this.element=t,this.window=o,this.pointInsideVerifier=s,this.params=n,this.graph=e.graph,this.graph.onAfterNodeAdded.subscribe(this.onAfterNodeAdded),this.graph.onAfterNodeUpdated.subscribe(this.onAfterNodeUpdated),this.graph.onBeforeNodeRemoved.subscribe(this.onBeforeNodeRemoved),this.graph.onBeforeClear.subscribe(this.reset),this.canvas.onBeforeDestroy.subscribe(this.revert)}static configure(e,t,o,s,n){new he(e,t,o,s,n)}moveNode(e,t){if(!this.graph.hasNode(e.nodeId))return;const o=this.calculateContentPoint(t),s={x:o.x-e.dx,y:o.y-e.dy},n=this.adjustNodeCoords(s);this.canvas.updateNode(e.nodeId,{x:n.x,y:n.y}),this.params.onNodeDrag(e.nodeId)}moveNodeOnTop(e){if(this.params.moveOnTop){if(this.maxNodePriority++,this.params.moveEdgesOnTop){const t=this.maxNodePriority;this.maxNodePriority++,this.graph.getNodeAdjacentEdgeIds(e).forEach(s=>{this.canvas.updateEdge(s,{priority:t})})}this.canvas.updateNode(e,{priority:this.maxNodePriority})}}stopMouseDrag(){this.grabbedNode!==null&&this.graph.hasNode(this.grabbedNode.nodeId)&&this.params.onNodeDragFinished(this.grabbedNode.nodeId),this.grabbedNode=null,H(this.element,null),this.removeMouseDragListeners()}removeMouseDragListeners(){this.window.removeEventListener("mouseup",this.onWindowMouseUp),this.window.removeEventListener("mousemove",this.onWindowMouseMove)}stopTouchDrag(){if(this.grabbedNode!==null&&this.graph.hasNode(this.grabbedNode.nodeId)){const e=this.graph.getNode(this.grabbedNode.nodeId);this.params.onNodeDragFinished({nodeId:this.grabbedNode.nodeId,element:e.element,x:e.x,y:e.y})}this.grabbedNode=null,this.removeTouchDragListeners()}removeTouchDragListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}updateMaxNodePriority(e){const t=this.graph.getNode(e).priority;this.maxNodePriority=Math.max(this.maxNodePriority,t)}calculateContentPoint(e){const t=this.element.getBoundingClientRect();return this.canvas.viewport.createContentCoords({x:e.x-t.x,y:e.y-t.y})}adjustNodeCoords(e){const t=this.params.gridSize;if(t!==null){const o=t/2;return{x:Math.floor((e.x+o)/t)*t,y:Math.floor((e.y+o)/t)*t}}return e}}const J=r=>{const e=[],t=r.touches.length;for(let h=0;h<t;h++)e.push([r.touches[h].clientX,r.touches[h].clientY]);const o=e.reduce((h,d)=>[h[0]+d[0],h[1]+d[1]],[0,0]),s=[o[0]/t,o[1]/t],a=e.map(h=>[h[0]-s[0],h[1]-s[1]]).reduce((h,d)=>h+Math.sqrt(d[0]*d[0]+d[1]*d[1]),0);return{x:s[0],y:s[1],scale:a/t,touchesCnt:t,touches:e}};class K{constructor(e,t,o,s,n){i(this,"viewport");i(this,"prevTouches",null);i(this,"wheelFinishTimer",null);i(this,"transformInProgress",!1);i(this,"revert",()=>{this.removeMouseDragListeners(),this.removeTouchDragListeners()});i(this,"onMouseDown",e=>{this.params.mouseDownEventVerifier(e)&&(H(this.element,this.params.shiftCursor),this.window.addEventListener("mousemove",this.onWindowMouseMove,{passive:!0}),this.window.addEventListener("mouseup",this.onWindowMouseUp,{passive:!0}),this.startRegisteredTransform())});i(this,"onWindowMouseMove",e=>{const t=this.pointInsideVerifier.verify(e.clientX,e.clientY);if(this.element===null||!t){this.stopMouseDrag();return}const o=-e.movementX,s=-e.movementY;this.moveViewport(o,s)});i(this,"onWindowMouseUp",e=>{this.params.mouseUpEventVerifier(e)&&this.stopMouseDrag()});i(this,"onWheelScroll",e=>{if(!this.params.mouseWheelEventVerifier(e))return;const{left:t,top:o}=this.element.getBoundingClientRect(),s=e.clientX-t,n=e.clientY-o,h=1/(e.deltaY<0?this.params.wheelSensitivity:1/this.params.wheelSensitivity);this.wheelFinishTimer===null&&this.params.onTransformStarted(),this.scaleViewport(h,s,n),this.wheelFinishTimer!==null&&clearTimeout(this.wheelFinishTimer),this.wheelFinishTimer=setTimeout(()=>{this.transformInProgress||this.params.onTransformFinished(),this.wheelFinishTimer=null},this.params.scaleWheelFinishTimeout)});i(this,"onTouchStart",e=>{if(this.prevTouches!==null){this.prevTouches=J(e);return}this.prevTouches=J(e),this.window.addEventListener("touchmove",this.onWindowTouchMove,{passive:!0}),this.window.addEventListener("touchend",this.onWindowTouchFinish,{passive:!0}),this.window.addEventListener("touchcancel",this.onWindowTouchFinish,{passive:!0}),this.startRegisteredTransform()});i(this,"onWindowTouchMove",e=>{const t=J(e);if(!t.touches.every(s=>this.pointInsideVerifier.verify(s[0],s[1]))){this.stopTouchDrag();return}if((t.touchesCnt===1||t.touchesCnt===2)&&this.moveViewport(-(t.x-this.prevTouches.x),-(t.y-this.prevTouches.y)),t.touchesCnt===2){const{left:s,top:n}=this.element.getBoundingClientRect(),a=this.prevTouches.x-s,h=this.prevTouches.y-n,c=1/(t.scale/this.prevTouches.scale);this.scaleViewport(c,a,h)}this.prevTouches=t});i(this,"onWindowTouchFinish",e=>{e.touches.length>0?this.prevTouches=J(e):this.stopTouchDrag()});i(this,"preventWheelScaleListener",e=>{e.preventDefault()});this.canvas=e,this.element=t,this.window=o,this.pointInsideVerifier=s,this.params=n,this.element.addEventListener("wheel",this.preventWheelScaleListener,{passive:!1}),this.viewport=e.viewport,this.handleResize(),this.viewport.onAfterResize.subscribe(()=>{this.handleResize()}),this.element.addEventListener("mousedown",this.onMouseDown,{passive:!0}),this.element.addEventListener("wheel",this.onWheelScroll,{passive:!0}),this.element.addEventListener("touchstart",this.onTouchStart,{passive:!0}),e.onBeforeDestroy.subscribe(this.revert)}static configure(e,t,o,s,n){new K(e,t,o,s,n)}moveViewport(e,t){const o=this.viewport.getViewportMatrix(),s=_e(o,e,t),{width:n,height:a}=this.viewport.getDimensions(),h=this.params.transformPreprocessor({prevTransform:o,nextTransform:s,canvasWidth:n,canvasHeight:a});this.performTransform(h)}scaleViewport(e,t,o){const s=this.canvas.viewport.getViewportMatrix(),n=qe(s,e,t,o),{width:a,height:h}=this.viewport.getDimensions(),d=this.params.transformPreprocessor({prevTransform:s,nextTransform:n,canvasWidth:a,canvasHeight:h});this.performTransform(d)}stopMouseDrag(){H(this.element,null),this.removeMouseDragListeners(),this.finishRegisteredTransform()}removeMouseDragListeners(){this.window.removeEventListener("mousemove",this.onWindowMouseMove),this.window.removeEventListener("mouseup",this.onWindowMouseUp)}stopTouchDrag(){this.prevTouches=null,this.removeTouchDragListeners(),this.finishRegisteredTransform()}removeTouchDragListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}performTransform(e){this.params.onBeforeTransformChange(),this.canvas.patchViewportMatrix(e),this.params.onTransformChange()}startRegisteredTransform(){this.transformInProgress=!0,this.params.onTransformStarted()}finishRegisteredTransform(){this.transformInProgress=!1,this.params.onTransformFinished()}handleResize(){const e=this.viewport.getViewportMatrix(),{width:t,height:o}=this.viewport.getDimensions(),s=this.params.transformPreprocessor({prevTransform:e,nextTransform:e,canvasWidth:t,canvasHeight:o});this.params.onResizeTransformStarted(),this.canvas.patchViewportMatrix(s),this.params.onResizeTransformFinished()}}class de{constructor(e,t,o,s,n,a,h){i(this,"nodeHorizontal");i(this,"nodeVertical");i(this,"viewport");i(this,"currentScale");i(this,"loadedArea",{xFrom:1/0,xTo:1/0,yFrom:1/0,yTo:1/0});i(this,"updateLoadedArea",e=>{this.loadedArea={xFrom:e.x,xTo:e.x+e.width,yFrom:e.y,yTo:e.y+e.height}});i(this,"onAfterViewportUpdated",()=>{this.userTransformInProgress||this.loadAreaAroundViewport()});i(this,"userTransformInProgress",!1);this.canvas=e,this.element=t,this.window=o,this.trigger=n,this.pointInsideVerifier=a,this.params=h,this.nodeHorizontal=this.params.nodeVerticalRadius,this.nodeVertical=this.params.nodeHorizontalRadius,this.viewport=e.viewport,this.currentScale=this.viewport.getViewportMatrix().scale,this.scheduleLoadAreaAroundViewport(),this.viewport.onAfterResize.subscribe(()=>{this.scheduleLoadAreaAroundViewport()});const d={...s,onResizeTransformStarted:()=>{this.userTransformInProgress=!0,s.onResizeTransformStarted()},onResizeTransformFinished:()=>{this.userTransformInProgress=!1,s.onResizeTransformFinished()},onBeforeTransformChange:()=>{this.userTransformInProgress=!0,s.onBeforeTransformChange()},onTransformChange:()=>{this.userTransformInProgress=!1;const c=this.currentScale;this.currentScale=this.viewport.getViewportMatrix().scale,c!==this.currentScale&&this.scheduleEnsureViewportAreaLoaded(),s.onTransformChange()},onTransformFinished:()=>{this.scheduleLoadAreaAroundViewport(),s.onTransformFinished()}};K.configure(e,this.element,this.window,this.pointInsideVerifier,d),this.trigger.subscribe(this.updateLoadedArea),this.canvas.viewport.onAfterUpdated.subscribe(this.onAfterViewportUpdated)}static configure(e,t,o,s,n,a,h){new de(e,t,o,s,n,a,h)}scheduleLoadAreaAroundViewport(){setTimeout(()=>{this.loadAreaAroundViewport()})}scheduleEnsureViewportAreaLoaded(){setTimeout(()=>{const{width:e,height:t}=this.viewport.getDimensions(),{scale:o,x:s,y:n}=this.viewport.getViewportMatrix(),a=e*o,h=t*o,d=s-this.nodeHorizontal,c=n-this.nodeVertical,u=s+a+this.nodeHorizontal,l=n+h+this.nodeVertical;this.loadedArea.xFrom<d&&this.loadedArea.xTo>u&&this.loadedArea.yFrom<c&&this.loadedArea.yTo>l||this.loadAreaAroundViewport()})}loadAreaAroundViewport(){const{width:e,height:t}=this.viewport.getDimensions(),{scale:o,x:s,y:n}=this.viewport.getViewportMatrix(),a=e*o,h=t*o,d=s-a-this.nodeHorizontal,c=n-h-this.nodeVertical,u=3*a+2*this.nodeHorizontal,l=3*h+2*this.nodeVertical;this.trigger.emit({x:d,y:c,width:u,height:l})}}const _t=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","svg");return r.style.position="absolute",r.style.inset="0",r},qt=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","rect");return r.setAttribute("fill","url(#pattern)"),r},er=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","pattern");return r.setAttribute("id","pattern"),r};class ce{constructor(e,t,o){i(this,"svg",_t());i(this,"patternRenderingRectangle",qt());i(this,"pattern",er());i(this,"patternContent");i(this,"tileWidth");i(this,"tileHeight");i(this,"halfTileWidth");i(this,"halfTileHeight");i(this,"maxViewportScale");i(this,"visible",!1);i(this,"onAfterTransformUpdated",()=>{const e=this.canvas.viewport.getContentMatrix(),t=e.x-this.halfTileWidth*e.scale,o=e.y-this.halfTileHeight*e.scale,s=`matrix(${e.scale}, 0, 0, ${e.scale}, ${t}, ${o})`;this.pattern.setAttribute("patternTransform",s),this.updateVisibility()});this.canvas=e,this.backgroundHost=o,this.tileWidth=t.tileWidth,this.tileHeight=t.tileHeight,this.halfTileWidth=this.tileWidth/2,this.halfTileHeight=this.tileHeight/2,this.patternContent=t.renderer,this.maxViewportScale=t.maxViewportScale;const s=`translate(${this.halfTileWidth}, ${this.halfTileHeight})`;this.patternContent.setAttribute("transform",s),this.pattern.appendChild(this.patternContent);const n=document.createElementNS("http://www.w3.org/2000/svg","defs");n.appendChild(this.pattern),this.svg.appendChild(n),this.svg.appendChild(this.patternRenderingRectangle),this.updateDimensions(),this.canvas.viewport.onAfterResize.subscribe(()=>{this.updateDimensions()}),this.canvas.viewport.onAfterUpdated.subscribe(this.onAfterTransformUpdated),this.onAfterTransformUpdated()}static configure(e,t,o){new ce(e,t,o)}updateVisibility(){const{scale:e}=this.canvas.viewport.getViewportMatrix(),t=e>this.maxViewportScale;t&&this.visible?(this.visible=!1,this.backgroundHost.removeChild(this.svg)):!t&&!this.visible&&(this.visible=!0,this.backgroundHost.appendChild(this.svg))}updateDimensions(){const{width:e,height:t}=this.canvas.viewport.getDimensions();if(this.svg.setAttribute("width",`${e}`),this.svg.setAttribute("height",`${t}`),this.patternRenderingRectangle.setAttribute("width",`${e}`),this.patternRenderingRectangle.setAttribute("height",`${t}`),e>0&&t>0){const o=this.tileWidth/e,s=this.tileHeight/t;this.pattern.setAttribute("width",`${o}`),this.pattern.setAttribute("height",`${s}`)}}}class le{constructor(e,t,o,s,n,a){i(this,"overlayCanvas");i(this,"edgeInProgress",null);i(this,"onEdgeCreated",e=>{this.params.onAfterEdgeCreated(e)});this.canvas=e,this.overlayLayer=t,this.viewportStore=o,this.window=s,this.pointInsideVerifier=n,this.params=a,this.overlayCanvas=tt(this.overlayLayer,this.viewportStore,this.window);const h={mouseDownEventVerifier:this.params.mouseDownEventVerifier,mouseUpEventVerifier:this.params.mouseUpEventVerifier,onPointerDownVerifier:(d,c)=>{const u=this.params.connectionTypeResolver(d);return u===null?!1:(this.grabPort(d,c,u),!0)},onPointerMove:d=>{this.moveDraggingPort(d)},onPointerUp:d=>{this.tryCreateConnection(d),this.resetDragState()},onPointerOutside:()=>{const d=this.edgeInProgress;this.resetDragState(),this.params.onEdgeCreationInterrupted(d)}};j.configure(this.canvas,this.window,this.pointInsideVerifier,h)}static configure(e,t,o,s,n,a){new le(e,t,o,s,n,a)}grabPort(e,t,o){const s=this.canvas.graph.getPort(e),n=s.element.getBoundingClientRect(),a=n.x+n.width/2,h=n.y+n.height/2,d=this.overlayLayer.getBoundingClientRect(),c=this.canvas.viewport.createContentCoords({x:a-d.x,y:h-d.y}),u=this.canvas.viewport.createContentCoords({x:t.x-d.x,y:t.y-d.y}),l={overlayNodeId:V.StaticNodeId,portCoords:c,portDirection:s.direction},g=o==="direct";this.edgeInProgress={staticPortId:e,isDirect:g};const p=this.params.draggingPortDirectionResolver.resolve({cursor:t,...this.edgeInProgress}),w={overlayNodeId:V.DraggingNodeId,portCoords:u,portDirection:p??s.direction},[y,f]=g?[l,w]:[w,l];this.overlayCanvas.addNode(Y(y)),this.overlayCanvas.addNode(Y(f)),this.overlayCanvas.addEdge({from:y.overlayNodeId,to:f.overlayNodeId,shape:this.params.edgeShapeFactory(V.EdgeId)})}resetDragState(){this.edgeInProgress=null,this.overlayCanvas.clear()}tryCreateConnection(e){const t=De(this.canvas.graph,e);if(t===null){this.params.onEdgeCreationInterrupted(this.edgeInProgress);return}const o=ae(this.edgeInProgress,t),s=this.params.connectionAllowedVerifier(o),n=this.params.connectionPreprocessor({from:o.from,to:o.to});s&&n!==null?(this.canvas.graph.onAfterEdgeAdded.subscribe(this.onEdgeCreated),this.canvas.addEdge(n),this.canvas.graph.onAfterEdgeAdded.unsubscribe(this.onEdgeCreated)):this.params.onEdgeCreationPrevented(o)}moveDraggingPort(e){const t=this.overlayLayer.getBoundingClientRect(),o=this.canvas.viewport.createContentCoords({x:e.x-t.x,y:e.y-t.y}),s=this.params.draggingPortDirectionResolver.resolve({cursor:e,...this.edgeInProgress});this.overlayCanvas.updateNode(V.DraggingNodeId,{x:o.x,y:o.y}).updatePort(V.DraggingNodeId,{direction:s})}}class ue{constructor(e,t,o,s,n,a){i(this,"overlayCanvas");i(this,"edgeInProgress",null);i(this,"draggingEdgePayload",null);i(this,"onEdgeReattached",e=>{this.params.onAfterEdgeReattached(e)});this.canvas=e,this.overlayLayer=t,this.viewportStore=o,this.window=s,this.pointInsideVerifier=n,this.params=a,this.overlayCanvas=tt(this.overlayLayer,this.viewportStore,this.window);const h={mouseDownEventVerifier:this.params.mouseDownEventVerifier,mouseUpEventVerifier:this.params.mouseUpEventVerifier,onPointerDownVerifier:(d,c)=>this.tryStartEdgeDragging(d,c),onPointerMove:d=>{this.moveDraggingPort(d)},onPointerUp:d=>{this.tryCreateConnection(d),this.resetDragState()},onPointerOutside:()=>{const d=this.draggingEdgePayload;this.resetDragState(),this.params.onEdgeReattachInterrupted({id:d.id,from:d.from,to:d.to,shape:d.shape,priority:d.priority})}};j.configure(this.canvas,this.window,this.pointInsideVerifier,h)}static configure(e,t,o,s,n,a){new ue(e,t,o,s,n,a)}tryStartEdgeDragging(e,t){const o=this.params.draggingEdgeResolver(e);if(o===null)return!1;const s=this.canvas.graph.getEdge(o),n=e===s.from,a=e===s.to,h=n?s.to:s.from;this.edgeInProgress={staticPortId:h,isDirect:a};const d=this.canvas.graph.getPort(e),c=this.canvas.graph.getPort(h),u=c.element.getBoundingClientRect(),l={x:u.x+u.width/2,y:u.y+u.height/2},g=this.overlayLayer.getBoundingClientRect(),p=this.canvas.viewport.createContentCoords({x:l.x-g.x,y:l.y-g.y}),w=this.canvas.viewport.createContentCoords({x:t.x-g.x,y:t.y-g.y});this.draggingEdgePayload={id:o,from:s.from,to:s.to,shape:s.shape,priority:s.priority},this.canvas.removeEdge(o);const y={overlayNodeId:V.StaticNodeId,portCoords:p,portDirection:c.direction},f=this.params.draggingPortDirectionResolver.resolve({cursor:t,...this.edgeInProgress}),m={overlayNodeId:V.DraggingNodeId,portCoords:w,portDirection:f??d.direction},[x,N]=a?[y,m]:[m,y];this.overlayCanvas.addNode(Y(x)),this.overlayCanvas.addNode(Y(N));const D=this.params.draggingEdgeShapeFactory!==null?this.params.draggingEdgeShapeFactory(V.EdgeId):s.shape;return this.overlayCanvas.addEdge({id:V.EdgeId,from:x.overlayNodeId,to:N.overlayNodeId,shape:D}),!0}resetDragState(){this.draggingEdgePayload=null,this.edgeInProgress=null,this.overlayCanvas.clear()}moveDraggingPort(e){const t=this.overlayLayer.getBoundingClientRect(),o={x:e.x-t.x,y:e.y-t.y},s=this.canvas.viewport.createContentCoords(o),n=this.params.draggingPortDirectionResolver.resolve({cursor:e,...this.edgeInProgress});this.overlayCanvas.updateNode(V.DraggingNodeId,{x:s.x,y:s.y}).updatePort(V.DraggingNodeId,{direction:n})}tryCreateConnection(e){const t=De(this.canvas.graph,e);if(this.overlayCanvas.removeEdge(V.EdgeId),t===null){const c=this.draggingEdgePayload;this.params.onEdgeReattachInterrupted({id:c.id,from:c.from,to:c.to,shape:c.shape,priority:c.priority});return}const{from:o,to:s}=ae(this.edgeInProgress,t),n=this.draggingEdgePayload,a={id:n.id,from:o,to:s,shape:n.shape,priority:n.priority},h=this.params.connectionAllowedVerifier({from:o,to:s}),d=this.params.connectionPreprocessor(a);if(h&&d!==null)this.canvas.graph.onAfterEdgeAdded.subscribe(this.onEdgeReattached),this.canvas.addEdge(d),this.canvas.graph.onAfterEdgeAdded.unsubscribe(this.onEdgeReattached);else{const c=this.draggingEdgePayload;this.params.onEdgeReattachPrevented({id:c.id,from:c.from,to:c.to,shape:c.shape,priority:c.priority})}}}class ge{constructor(e,t){this.applier=e,this.trigger=t,this.trigger.subscribe(()=>{this.applier.apply()})}static configure(e,t){new ge(e,t)}}class pe{constructor(e,t,o){i(this,"applyScheduled",!1);i(this,"apply",()=>{this.applyScheduled=!1,this.applier.apply()});this.graph=e,this.applier=t,this.defererFn=o,this.graph.onAfterNodeAdded.subscribe(()=>{this.scheduleApply()}),this.graph.onBeforeNodeRemoved.subscribe(()=>{this.scheduleApply()}),this.graph.onAfterEdgeAdded.subscribe(()=>{this.scheduleApply()}),this.graph.onBeforeEdgeRemoved.subscribe(()=>{this.scheduleApply()})}static configure(e,t,o){new pe(e,t,o)}scheduleApply(){this.applyScheduled||(this.applyScheduled=!0,this.defererFn(this.apply))}}class tr{constructor(e,t,o){this.canvas=e,this.layoutAlgorithm=t,this.params=o}apply(){const e=this.layoutAlgorithm.calculateCoordinates({graph:this.canvas.graph,viewport:this.canvas.viewport});this.params.onBeforeApplied(),e.forEach((t,o)=>{this.params.staticNodeResolver(o)||this.canvas.updateNode(o,t)}),this.params.onAfterApplied()}}class rr{static configure(e,t){const o=t.applyOn,s=new tr(e,t.algorithm,{staticNodeResolver:t.staticNodeResolver,onBeforeApplied:t.onBeforeApplied,onAfterApplied:t.onAfterApplied});switch(o.type){case"trigger":{ge.configure(s,o.trigger);break}case"topologyChangeSchedule":{pe.configure(e.graph,s,o.schedule);break}}}}class or{constructor(e,t){i(this,"previousTimeStamp");i(this,"step",e=>{if(this.previousTimeStamp===void 0)this.previousTimeStamp=e;else{const t=(e-this.previousTimeStamp)/1e3;this.previousTimeStamp=e,this.callback(t)}this.win.requestAnimationFrame(this.step)});this.win=e,this.callback=t,this.win.requestAnimationFrame(this.step)}}class sr{constructor(e,t,o){this.canvas=e,this.layoutAlgorithm=t,this.params=o}apply(e){const t=this.layoutAlgorithm.calculateNextCoordinates({graph:this.canvas.graph,viewport:this.canvas.viewport,dt:e});this.params.onBeforeApplied(),t.forEach((o,s)=>{this.params.staticNodeResolver(s)||this.canvas.updateNode(s,o)}),this.params.onAfterApplied()}}class we{constructor(e,t,o){i(this,"applier");i(this,"step",e=>{this.applier.apply(e)});this.win=o,this.applier=new sr(e,t.algorithm,{staticNodeResolver:t.staticNodeResolver,onBeforeApplied:t.onBeforeApplied,onAfterApplied:t.onAfterApplied}),new or(this.win,this.step)}static configure(e,t,o){new we(e,t,o)}}class ye{constructor(e,t,o,s,n){i(this,"configurator");i(this,"onNodeSelected");i(this,"selectionHandledTag",ie);i(this,"onAfterNodeAdded",e=>{const{element:t}=this.canvas.graph.getNode(e);this.configurator.enable(t)});i(this,"onBeforeNodeRemoved",e=>{const{element:t}=this.canvas.graph.getNode(e);this.configurator.disable(t)});i(this,"reset",()=>{this.canvas.graph.getAllNodeIds().forEach(e=>{const{element:t}=this.canvas.graph.getNode(e);this.configurator.disable(t)})});this.canvas=e,this.window=t,this.pointInsideVerifier=o,this.eventTagger=s,this.onNodeSelected=n.onNodeSelected,this.configurator=new ne(this.window,this.pointInsideVerifier,{onSelected:(a,h)=>{const d=this.canvas.graph.findNodeIdByElement(a);this.eventTagger.tag(h,this.selectionHandledTag),this.onNodeSelected(d)},movementThreshold:n.movementThreshold,mouseDownEventVerifier:n.mouseDownEventVerifier,mouseUpEventVerifier:n.mouseUpEventVerifier}),this.canvas.graph.onAfterNodeAdded.subscribe(this.onAfterNodeAdded),this.canvas.graph.onBeforeNodeRemoved.subscribe(this.onBeforeNodeRemoved),this.canvas.graph.onBeforeClear.subscribe(this.reset),this.canvas.onBeforeDestroy.subscribe(this.reset)}static configure(e,t,o,s,n){new ye(e,t,o,s,n)}}class fe{constructor(e,t,o,s,n){i(this,"configurator");i(this,"selectionHandledTag",ie);i(this,"onEdgeSelected");i(this,"onAfterEdgeAdded",e=>{const{shape:t}=this.canvas.graph.getEdge(e);this.configurator.enable(t.svg)});i(this,"onBeforeEdgeRemoved",e=>{const{shape:t}=this.canvas.graph.getEdge(e);this.configurator.disable(t.svg)});i(this,"reset",()=>{this.canvas.graph.getAllEdgeIds().forEach(e=>{const{shape:t}=this.canvas.graph.getEdge(e);this.configurator.disable(t.svg)})});this.canvas=e,this.window=t,this.pointInsideVerifier=o,this.eventTagger=s,this.onEdgeSelected=n.onEdgeSelected,this.configurator=new ne(this.window,this.pointInsideVerifier,{onSelected:(a,h)=>{const d=this.canvas.graph.findEdgeIdByElement(a);this.eventTagger.tag(h,this.selectionHandledTag),this.onEdgeSelected(d)},movementThreshold:n.movementThreshold,mouseDownEventVerifier:n.mouseDownEventVerifier,mouseUpEventVerifier:n.mouseUpEventVerifier}),this.canvas.graph.onAfterEdgeAdded.subscribe(this.onAfterEdgeAdded),this.canvas.graph.onBeforeEdgeRemoved.subscribe(this.onBeforeEdgeRemoved),this.canvas.graph.onBeforeClear.subscribe(this.reset),this.canvas.onBeforeDestroy.subscribe(this.reset)}static configure(e,t,o,s,n){new fe(e,t,o,s,n)}}class ve{constructor(e,t,o,s,n,a){i(this,"configurator");i(this,"onCanvasSelected");i(this,"selectionHandledTag",ie);this.canvas=e,this.element=t,this.window=o,this.pointInsideVerifier=s,this.eventTagger=n,this.onCanvasSelected=a.onCanvasSelected,this.configurator=new ne(this.window,this.pointInsideVerifier,{onSelected:(h,d)=>{this.eventTagger.has(d,this.selectionHandledTag)||this.onCanvasSelected()},movementThreshold:a.movementThreshold,mouseDownEventVerifier:a.mouseDownEventVerifier,mouseUpEventVerifier:a.mouseUpEventVerifier}),this.configurator.enable(this.element),this.canvas.onBeforeDestroy.subscribe(()=>{this.configurator.disable(this.element)})}static configure(e,t,o,s,n,a){new ve(e,t,o,s,n,a)}}const ir=()=>{const r=document.createElement("div");return r.style.width="100%",r.style.height="100%",r.style.position="relative",r},me=()=>{const r=document.createElement("div");return r.style.position="absolute",r.style.inset="0",r},ot=()=>{const r=me();return r.style.pointerEvents="none",r};class nr{constructor(e){i(this,"background",me());i(this,"main",me());i(this,"overlayConnectablePorts",ot());i(this,"overlayDraggableEdges",ot());i(this,"host",ir());this.element=e,this.element.appendChild(this.host),this.host.appendChild(this.background),this.host.appendChild(this.main),this.host.appendChild(this.overlayConnectablePorts),this.host.appendChild(this.overlayDraggableEdges)}destroy(){this.host.removeChild(this.background),this.host.removeChild(this.main),this.host.removeChild(this.overlayConnectablePorts),this.host.removeChild(this.overlayDraggableEdges),this.element.removeChild(this.host)}}const P=Object.freeze({seed:"HTMLGraph is awesome",maxTimeDeltaSec:.01,nodeCharge:1e5,nodeMass:1,edgeEquilibriumLength:300,edgeStiffness:1e3,dtSec:.01,maxIterations:1e3,convergenceVelocity:10,maxForce:1e7,nodeForceCoefficient:1,barnesHutAreaRadiusThreshold:.01,barnesHutTheta:1}),I=Object.freeze({mouseDownEventVerifier:r=>r.button===0,mouseUpEventVerifier:r=>r.button===0,movementThreshold:10}),C=()=>{},st=(r,e,t)=>r==="closest-connectable-port"?new rt(e,t):r==="nearest-connectable-port"?new rt(e,t):new Zt(r),Ee=r=>{if(typeof r=="function")return r;switch(r.type){case"straight":return()=>new We({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowOffset:r.arrowOffset,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleSquareSide:r.cycleSquareSide,roundness:r.roundness,detourDistance:r.detourDistance,detourDirection:r.detourDirection});case"horizontal":return()=>new Oe({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowOffset:r.arrowOffset,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleSquareSide:r.cycleSquareSide,roundness:r.roundness,detourDistance:r.detourDistance});case"vertical":return()=>new He({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowOffset:r.arrowOffset,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleSquareSide:r.cycleSquareSide,roundness:r.roundness,detourDistance:r.detourDistance});case"direct":return()=>new te({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,sourceOffset:r.sourceOffset,targetOffset:r.targetOffset});default:return()=>new $e({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleRadius:r.cycleRadius,smallCycleRadius:r.smallCycleRadius,curvature:r.curvature,detourDistance:r.detourDistance,detourDirection:r.detourDirection})}},ar=r=>{var u,l,g,p,w,y;const e=r.nodeDragVerifier??(()=>!0),t=r.moveOnTop!==!1,o=r.moveEdgesOnTop!==!1&&t,s=(u=r.mouse)==null?void 0:u.dragCursor,n=s!==void 0?s:"grab",a=(l=r.mouse)==null?void 0:l.mouseDownEventVerifier,h=a!==void 0?a:f=>f.button===0,d=(g=r.mouse)==null?void 0:g.mouseUpEventVerifier,c=d!==void 0?d:f=>f.button===0;return{moveOnTop:t,moveEdgesOnTop:o,dragCursor:n,gridSize:r.gridSize??null,mouseDownEventVerifier:h,mouseUpEventVerifier:c,onNodeDragStarted:((p=r.events)==null?void 0:p.onNodeDragStarted)??C,onNodeDrag:((w=r.events)==null?void 0:w.onNodeDrag)??C,nodeDragVerifier:e,onNodeDragFinished:((y=r.events)==null?void 0:y.onNodeDragFinished)??C}},hr=r=>{const e=r.minX!==null?r.minX:-1/0,t=r.maxX!==null?r.maxX:1/0,o=r.minY!==null?r.minY:-1/0,s=r.maxY!==null?r.maxY:1/0;return n=>{let a=n.nextTransform.x,h=n.nextTransform.y;a<e&&a<n.prevTransform.x&&(a=Math.min(n.prevTransform.x,e));const d=n.canvasWidth*n.prevTransform.scale,c=t-d;a>c&&a>n.prevTransform.x&&(a=Math.max(n.prevTransform.x,c)),h<o&&h<n.prevTransform.y&&(h=Math.min(n.prevTransform.y,o));const u=n.canvasHeight*n.prevTransform.scale,l=s-u;return h>l&&h>n.prevTransform.y&&(h=Math.max(n.prevTransform.y,l)),{scale:n.nextTransform.scale,x:a,y:h}}},dr=r=>{const e=r.maxContentScale,t=r.minContentScale,o=e!==null?1/e:0,s=t!==null?1/t:1/0;return n=>{const a=n.prevTransform,h=n.nextTransform;let d=h.scale,c=h.x,u=h.y;if(h.scale>s&&h.scale>a.scale){d=Math.max(a.scale,s),c=a.x,u=a.y;const l=(d-a.scale)/(h.scale-a.scale);c=a.x+(h.x-a.x)*l,u=a.y+(h.y-a.y)*l}if(h.scale<o&&h.scale<a.scale){d=Math.min(a.scale,o),c=a.x,u=a.y;const l=(d-a.scale)/(h.scale-a.scale);c=a.x+(h.x-a.x)*l,u=a.y+(h.y-a.y)*l}return{scale:d,x:c,y:u}}},cr=r=>e=>r.reduce((t,o)=>o({prevTransform:e.prevTransform,nextTransform:t,canvasWidth:e.canvasWidth,canvasHeight:e.canvasHeight}),e.nextTransform),it=r=>{if(typeof r=="function")return r;switch(r.type){case"scale-limit":return dr({minContentScale:r.minContentScale??0,maxContentScale:r.maxContentScale??1/0});case"shift-limit":return hr({minX:r.minX??-1/0,maxX:r.maxX??1/0,minY:r.minY??-1/0,maxY:r.maxY??1/0})}},nt=r=>{var g,p,w,y,f,m,x,N,D,R,F,U;const e=(g=r==null?void 0:r.scale)==null?void 0:g.mouseWheelSensitivity,t=e!==void 0?e:1.2,o=r==null?void 0:r.transformPreprocessor;let s;o!==void 0?Array.isArray(o)?s=cr(o.map(L=>it(L))):s=it(o):s=L=>L.nextTransform;const n=((p=r==null?void 0:r.shift)==null?void 0:p.cursor)!==void 0?r.shift.cursor:"grab",a=(w=r==null?void 0:r.shift)==null?void 0:w.mouseDownEventVerifier,h=a!==void 0?a:L=>L.button===0,d=(y=r==null?void 0:r.shift)==null?void 0:y.mouseUpEventVerifier,c=d!==void 0?d:L=>L.button===0,u=(f=r==null?void 0:r.scale)==null?void 0:f.mouseWheelEventVerifier,l=u!==void 0?u:()=>!0;return{wheelSensitivity:t,onTransformStarted:((m=r==null?void 0:r.events)==null?void 0:m.onTransformStarted)??C,onTransformFinished:((x=r==null?void 0:r.events)==null?void 0:x.onTransformFinished)??C,onBeforeTransformChange:((N=r==null?void 0:r.events)==null?void 0:N.onBeforeTransformChange)??C,onTransformChange:((D=r==null?void 0:r.events)==null?void 0:D.onTransformChange)??C,transformPreprocessor:s,shiftCursor:n,mouseDownEventVerifier:h,mouseUpEventVerifier:c,mouseWheelEventVerifier:l,scaleWheelFinishTimeout:((R=r==null?void 0:r.scale)==null?void 0:R.wheelFinishTimeout)??500,onResizeTransformStarted:((F=r==null?void 0:r.events)==null?void 0:F.onResizeTransformStarted)??C,onResizeTransformFinished:((U=r==null?void 0:r.events)==null?void 0:U.onResizeTransformFinished)??C}},lr=(r,e)=>{const t=document.createElementNS("http://www.w3.org/2000/svg","circle");return t.setAttribute("cx","0"),t.setAttribute("cy","0"),t.setAttribute("r",`${r}`),t.setAttribute("fill",`${e}`),t},ur=r=>r instanceof SVGElement?r:lr((r==null?void 0:r.radius)??1.5,(r==null?void 0:r.color)??"#d8d8d8"),gr=r=>{const e=r.tileDimensions,t=(e==null?void 0:e.width)??25,o=(e==null?void 0:e.height)??25,s=ur(r.renderer??{});return{tileWidth:t,tileHeight:o,renderer:s,maxViewportScale:r.maxViewportScale??10}},z=Object.freeze({connectionTypeResolver:()=>"direct",connectionPreprocessor:r=>r,connectionAllowedVerifier:()=>!0,mouseEventVerifier:r=>r.button===0}),pr=(r,e,t)=>{var s,n,a;const o=r.connectionAllowedVerifier??z.connectionAllowedVerifier;return{connectionTypeResolver:r.connectionTypeResolver??z.connectionTypeResolver,connectionAllowedVerifier:o,draggingPortDirectionResolver:st(r.dragPortDirection,t,o),edgeShapeFactory:r.edgeShape!==void 0?Ee(r.edgeShape):e,connectionPreprocessor:r.connectionPreprocessor??z.connectionPreprocessor,mouseDownEventVerifier:r.mouseDownEventVerifier??z.mouseEventVerifier,mouseUpEventVerifier:r.mouseUpEventVerifier??z.mouseEventVerifier,onAfterEdgeCreated:((s=r.events)==null?void 0:s.onAfterEdgeCreated)??C,onEdgeCreationInterrupted:((n=r.events)==null?void 0:n.onEdgeCreationInterrupted)??C,onEdgeCreationPrevented:((a=r.events)==null?void 0:a.onEdgeCreationPrevented)??C}},Q=Object.freeze({connectionAllowedVerifier:()=>!0,connectionPreprocessor:r=>r,mouseDownEventVerifier:r=>r.button===0&&r.ctrlKey,mouseUpEventVerifier:r=>r.button===0}),wr=(r,e)=>{var s,n,a;const t=h=>{const d=e.getPortAdjacentEdgeIds(h);return d.length>0?d[d.length-1]:null},o=r.connectionAllowedVerifier??Q.connectionAllowedVerifier;return{connectionPreprocessor:r.connectionPreprocessor??Q.connectionPreprocessor,connectionAllowedVerifier:o,mouseDownEventVerifier:r.mouseDownEventVerifier??Q.mouseDownEventVerifier,mouseUpEventVerifier:r.mouseUpEventVerifier??Q.mouseUpEventVerifier,draggingEdgeResolver:r.draggingEdgeResolver??t,draggingEdgeShapeFactory:r.draggingEdgeShape!==void 0?Ee(r.draggingEdgeShape):null,onAfterEdgeReattached:((s=r.events)==null?void 0:s.onAfterEdgeReattached)??C,onEdgeReattachInterrupted:((n=r.events)==null?void 0:n.onEdgeReattachInterrupted)??C,onEdgeReattachPrevented:((a=r.events)==null?void 0:a.onEdgeReattachPrevented)??C,draggingPortDirectionResolver:st(r.dragPortDirection,e,o)}},yr=r=>({nodeVerticalRadius:r.nodeContainingRadius.vertical,nodeHorizontalRadius:r.nodeContainingRadius.horizontal}),fr=r=>{var e,t;return{onAfterNodeDetached:((e=r==null?void 0:r.events)==null?void 0:e.onAfterNodeDetached)??C,onBeforeNodeAttached:((t=r==null?void 0:r.events)==null?void 0:t.onBeforeNodeAttached)??C}};class at extends Error{constructor(){super(...arguments);i(this,"name","CanvasBuilderError")}}class ht{constructor(e,t,o){i(this,"dt");i(this,"nodeMass");i(this,"edgeEquilibriumLength");i(this,"edgeStiffness");i(this,"nodeForcesApplicationStrategy");i(this,"distanceVectorGenerator");this.graph=e,this.currentCoords=t,this.dt=o.dtSec,this.nodeMass=o.nodeMass,this.edgeEquilibriumLength=o.edgeEquilibriumLength,this.edgeStiffness=o.edgeStiffness,this.distanceVectorGenerator=o.distanceVectorGenerator,this.nodeForcesApplicationStrategy=o.nodeForcesApplicationStrategy}apply(){let e=0;const t=new Map;return this.graph.getAllNodeIds().forEach(s=>{t.set(s,{x:0,y:0})}),this.nodeForcesApplicationStrategy.apply(this.currentCoords,t),this.applyEdgeForces(t),this.currentCoords.forEach((s,n)=>{const a=t.get(n),h={x:a.x/this.nodeMass*this.dt,y:a.y/this.nodeMass*this.dt};e=Math.max(e,Math.sqrt(h.x*h.x+h.y*h.y));const d=h.x*this.dt,c=h.y*this.dt;s.x+=d,s.y+=c}),e}applyEdgeForces(e){this.graph.getAllEdgeIds().forEach(t=>{const o=this.graph.getEdge(t),s=this.graph.getPort(o.from),n=this.graph.getPort(o.to),a=this.currentCoords.get(s.nodeId),h=this.currentCoords.get(n.nodeId),d=this.distanceVectorGenerator.create(a,h),u=(d.d-this.edgeEquilibriumLength)*this.edgeStiffness,l=d.ex*u,g=d.ey*u,p=e.get(s.nodeId),w=e.get(n.nodeId);p.x+=l,p.y+=g,w.x-=l,w.y-=g})}}class dt{constructor(e){i(this,"PI2",2*Math.PI);this.rand=e}create(e,t){const o=t.x-e.x,s=t.y-e.y,n=o*o+s*s;if(n===0){const c=this.PI2*this.rand();return{ex:Math.cos(c),ey:Math.sin(c),d:0}}const a=Math.sqrt(n),h=o/a,d=s/a;return{ex:h,ey:d,d:a}}}const ct=r=>{if(r.distance===0)return r.maxForce;const e=r.coefficient*(r.sourceCharge*r.targetCharge/(r.distance*r.distance));return Math.min(e,r.maxForce)};class vr{constructor(e){i(this,"nodeCharge");i(this,"distanceVectorGenerator");i(this,"maxForce");this.nodeCharge=e.nodeCharge,this.distanceVectorGenerator=e.distanceVectorGenerator,this.maxForce=e.maxForce}apply(e,t){const o=Array.from(t.keys()),s=o.length;for(let n=0;n<s;n++){const a=o[n];for(let h=n+1;h<s;h++){const d=o[h],c=e.get(a),u=e.get(d),l=this.distanceVectorGenerator.create(c,u),g=ct({coefficient:1,sourceCharge:this.nodeCharge,targetCharge:this.nodeCharge,distance:l.d,maxForce:this.maxForce}),p=g*l.ex,w=g*l.ey,y=t.get(a),f=t.get(d);y.x-=p,y.y-=w,f.x+=p,f.y+=w}}}}const mr=r=>{if(r.size===0)return{centerX:0,centerY:0,radius:0};let e=1/0,t=-1/0,o=1/0,s=-1/0;r.forEach(d=>{e=Math.min(e,d.x),t=Math.max(t,d.x),o=Math.min(o,d.y),s=Math.max(s,d.y)});const n=t-e,a=s-o,h=Math.max(n,a);return{centerX:(e+t)/2,centerY:(o+s)/2,radius:h/2}};class Er{constructor(e){i(this,"root");i(this,"leaves",new Map);i(this,"coords");i(this,"areaRadiusThreshold");i(this,"nodeMass");i(this,"nodeCharge");i(this,"sortedParentNodes",[]);this.coords=e.coords,this.areaRadiusThreshold=e.areaRadiusThreshold,this.nodeMass=e.nodeMass,this.nodeCharge=e.nodeCharge,this.root={nodeIds:new Set(e.coords.keys()),box:e.box,totalMass:0,totalCharge:0,chargeCenter:{x:0,y:0},parent:null,lb:null,lt:null,rb:null,rt:null};let t=[this.root];for(;t.length>0;){const o=[];for(;t.length>0;){const s=t.pop();this.processNode(s).forEach(a=>{o.push(a)})}t=o}this.sortedParentNodes.reverse().forEach(o=>{let s=0,n=0,a=0,h=0;o.lb!==null&&(a+=o.lb.totalMass,h+=o.lb.totalCharge,s+=o.lb.chargeCenter.x*o.lb.totalCharge,n+=o.lb.chargeCenter.y*o.lb.totalCharge),o.lt!==null&&(a+=o.lt.totalMass,h+=o.lt.totalCharge,s+=o.lt.chargeCenter.x*o.lt.totalCharge,n+=o.lt.chargeCenter.y*o.lt.totalCharge),o.rb!==null&&(a+=o.rb.totalMass,h+=o.rb.totalCharge,s+=o.rb.chargeCenter.x*o.rb.totalCharge,n+=o.rb.chargeCenter.y*o.rb.totalCharge),o.rt!==null&&(a+=o.rt.totalMass,h+=o.rt.totalCharge,s+=o.rt.chargeCenter.x*o.rt.totalCharge,n+=o.rt.chargeCenter.y*o.rt.totalCharge),o.totalMass=a,o.totalCharge=h,o.chargeCenter.x=s/h,o.chargeCenter.y=n/h})}getRoot(){return this.root}getLeaf(e){return this.leaves.get(e)}processNode(e){if(e.nodeIds.size<2)return this.setLeaf(e),[];const{centerX:t,centerY:o,radius:s}=e.box;if(s<this.areaRadiusThreshold)return this.setLeaf(e),[];this.sortedParentNodes.push(e);const n=new Set,a=new Set,h=new Set,d=new Set,c=s/2;e.nodeIds.forEach(g=>{const{x:p,y:w}=this.coords.get(g);p<t?w<o?d.add(g):h.add(g):w<o?a.add(g):n.add(g),e.nodeIds.delete(g)});const u={parent:e,lb:null,lt:null,rb:null,rt:null},l=[];if(n.size>0){const g={nodeIds:n,totalMass:0,totalCharge:0,chargeCenter:{x:0,y:0},box:{centerX:t+c,centerY:o+c,radius:c},...u};e.rt=g,l.push(g)}if(a.size>0){const g={nodeIds:a,totalMass:0,totalCharge:0,chargeCenter:{x:0,y:0},box:{centerX:t+c,centerY:o-c,radius:c},...u};e.rb=g,l.push(g)}if(h.size>0){const g={nodeIds:h,totalMass:0,totalCharge:0,chargeCenter:{x:0,y:0},box:{centerX:t-c,centerY:o+c,radius:c},...u};e.lt=g,l.push(g)}if(d.size>0){const g={nodeIds:d,totalMass:0,totalCharge:0,chargeCenter:{x:0,y:0},box:{centerX:t-c,centerY:o-c,radius:c},...u};e.lb=g,l.push(g)}return l}setLeaf(e){e.totalMass=this.nodeMass*e.nodeIds.size,e.totalCharge=this.nodeCharge*e.nodeIds.size,e.chargeCenter=this.calculateLeafChargeCenter(e.nodeIds),e.nodeIds.forEach(t=>{this.leaves.set(t,e)})}calculateLeafChargeCenter(e){if(e.size===0)return{x:0,y:0};let t=0,o=0;return e.forEach(s=>{const n=this.coords.get(s);t+=n.x,o+=n.y}),{x:t/e.size,y:o/e.size}}}class xr{constructor(e){i(this,"areaRadiusThreshold");i(this,"nodeMass");i(this,"nodeCharge");i(this,"theta");i(this,"distanceVectorGenerator");i(this,"nodeForceCoefficient");i(this,"maxForce");this.areaRadiusThreshold=e.areaRadiusThreshold,this.nodeMass=e.nodeMass,this.nodeCharge=e.nodeCharge,this.theta=e.theta,this.distanceVectorGenerator=e.distanceVectorGenerator,this.nodeForceCoefficient=e.nodeForceCoefficient,this.maxForce=e.maxForce}apply(e,t){const o=mr(e),s=new Er({box:o,coords:e,areaRadiusThreshold:this.areaRadiusThreshold,nodeMass:this.nodeMass,nodeCharge:this.nodeCharge});e.forEach((n,a)=>{const h=this.calculateForceForNode(s.getLeaf(a),a,e),d=t.get(a);this.applyForce(d,h)})}calculateForceForNode(e,t,o){const s=o.get(t),n={x:0,y:0};e.nodeIds.forEach(h=>{if(h!==t){const d=o.get(h),c=this.calculateNodeRepulsiveForce({sourceCharge:this.nodeCharge,targetCharge:this.nodeCharge,sourceCoords:d,targetCoords:s});this.applyForce(n,c)}});let a=e;for(;a!==null;){const h=a.parent;if(h!==null){const d=this.distanceVectorGenerator.create(h.chargeCenter,s);h.box.radius*2<d.d*this.theta?(this.tryApplyFarForce({totalForce:n,targetCoords:s,target:h.lb,current:a}),this.tryApplyFarForce({totalForce:n,targetCoords:s,target:h.rb,current:a}),this.tryApplyFarForce({totalForce:n,targetCoords:s,target:h.rt,current:a}),this.tryApplyFarForce({totalForce:n,targetCoords:s,target:h.lt,current:a})):(this.tryApplyNearForce({totalForce:n,targetCoords:s,target:h.lb,current:a,nodesCoords:o}),this.tryApplyNearForce({totalForce:n,targetCoords:s,target:h.rb,current:a,nodesCoords:o}),this.tryApplyNearForce({totalForce:n,targetCoords:s,target:h.rt,current:a,nodesCoords:o}),this.tryApplyNearForce({totalForce:n,targetCoords:s,target:h.lt,current:a,nodesCoords:o}))}a=a.parent}return n}calculateExactForce(e,t,o){const s={x:0,y:0},n=[e];for(;n.length>0;){const a=n.pop();a.nodeIds.forEach(h=>{const d=o.get(h),c=this.calculateNodeRepulsiveForce({sourceCharge:this.nodeCharge,targetCharge:this.nodeCharge,sourceCoords:d,targetCoords:t});this.applyForce(s,c)}),a.lb!==null&&n.push(a.lb),a.rb!==null&&n.push(a.rb),a.lt!==null&&n.push(a.lt),a.rt!==null&&n.push(a.rt)}return s}calculateApproximateForce(e,t){return this.calculateNodeRepulsiveForce({sourceCharge:this.nodeCharge,targetCharge:e.totalCharge,sourceCoords:e.chargeCenter,targetCoords:t})}calculateNodeRepulsiveForce(e){const t=this.distanceVectorGenerator.create(e.sourceCoords,e.targetCoords),o=ct({coefficient:this.nodeForceCoefficient,sourceCharge:e.sourceCharge,targetCharge:e.targetCharge,distance:t.d,maxForce:this.maxForce});return{x:o*t.ex,y:o*t.ey}}applyForce(e,t){e.x+=t.x,e.y+=t.y}tryApplyFarForce(e){if(e.target!==null&&e.target!==e.current){const t=this.calculateApproximateForce(e.target,e.targetCoords);this.applyForce(e.totalForce,t)}}tryApplyNearForce(e){if(e.target!==null&&e.target!==e.current){const t=this.calculateExactForce(e.target,e.targetCoords,e.nodesCoords);this.applyForce(e.totalForce,t)}}}const lt=r=>r.theta!==0?new xr({nodeCharge:r.nodeCharge,nodeForceCoefficient:r.nodeForceCoefficient,distanceVectorGenerator:r.distanceVectorGenerator,maxForce:r.maxForce,theta:r.theta,nodeMass:r.nodeMass,areaRadiusThreshold:r.areaRadiusThreshold}):new vr({nodeCharge:r.nodeCharge,nodeForceCoefficient:r.nodeForceCoefficient,distanceVectorGenerator:r.distanceVectorGenerator,maxForce:r.maxForce});class ut{constructor(e){i(this,"rand");i(this,"sparsity");this.rand=e.rand,this.sparsity=e.sparsity}calculateCoordinates(e){const{graph:t,viewport:o}=e,s=new Map,n=t.getAllNodeIds().filter(w=>{const y=t.getNode(w);return y.x===null||y.y===null}),a=Math.sqrt(n.length)*this.sparsity,{width:h,height:d}=o.getDimensions(),c={x:h/2,y:d/2},u=o.createContentCoords(c),l=a/2,g={x:u.x-l,y:u.y-l};return t.getAllNodeIds().forEach(w=>{const y=t.getNode(w);s.set(w,{x:y.x??g.x+a*this.rand(),y:y.y??g.y+a*this.rand()})}),s}}class Ar{constructor(e){i(this,"distanceVectorGenerator");i(this,"nodeForcesApplicationStrategy");i(this,"fillerLayoutAlgorithm");i(this,"maxIterations");i(this,"dtSec");i(this,"nodeMass");i(this,"edgeEquilibriumLength");i(this,"edgeStiffness");i(this,"convergenceVelocity");this.maxIterations=e.maxIterations,this.dtSec=e.dtSec,this.nodeMass=e.nodeMass,this.edgeEquilibriumLength=e.edgeEquilibriumLength,this.edgeStiffness=e.edgeStiffness,this.convergenceVelocity=e.convergenceVelocity,this.distanceVectorGenerator=new dt(e.rand),this.nodeForcesApplicationStrategy=lt({distanceVectorGenerator:this.distanceVectorGenerator,nodeCharge:e.nodeCharge,maxForce:e.maxForce,nodeForceCoefficient:e.nodeForceCoefficient,theta:e.barnesHutTheta,areaRadiusThreshold:e.barnesHutAreaRadiusThreshold,nodeMass:e.nodeMass}),this.fillerLayoutAlgorithm=new ut({rand:e.rand,sparsity:e.edgeEquilibriumLength})}calculateCoordinates(e){const{graph:t,viewport:o}=e,s=this.fillerLayoutAlgorithm.calculateCoordinates({graph:t,viewport:o});for(let n=0;n<this.maxIterations&&!(new ht(t,s,{distanceVectorGenerator:this.distanceVectorGenerator,nodeForcesApplicationStrategy:this.nodeForcesApplicationStrategy,dtSec:this.dtSec,nodeMass:this.nodeMass,edgeEquilibriumLength:this.edgeEquilibriumLength,edgeStiffness:this.edgeStiffness}).apply()<this.convergenceVelocity);n++);return s}}class Sr{constructor(e,t){i(this,"forest",new Set);i(this,"remainingNodeIds");for(this.graph=e,this.nextLayerNodesResolver=t,this.remainingNodeIds=new Set(this.graph.getAllNodeIds());this.remainingNodeIds.size>0;){const[o]=this.remainingNodeIds;this.traverse(o)}}generate(){return this.forest}traverse(e){const t={nodeId:e,children:new Set},o=[];this.forest.add({root:t,sequence:o});let s=[t];for(this.remainingNodeIds.delete(t.nodeId);s.length>0;){const n=[];s.forEach(a=>{o.push(a);const h=this.nextLayerNodesResolver({graph:this.graph,currentNodeId:a.nodeId});for(const d of h){if(!this.remainingNodeIds.has(d))continue;this.remainingNodeIds.delete(d);const c={nodeId:d,children:new Set};a.children.add(c),n.push(c)}}),s=n}}}class br{constructor(e){this.params=e}generate(e){let t=0;const o=[],s=e.length-1,n=[];e.forEach((h,d)=>{if(t+=this.params.spaceAroundRadius,h.forEach((c,u)=>{n[u]===void 0?n[u]={start:t+c.start,end:t+c.end}:n[u].end=t+c.end}),o.push(t),d!==s){const c=e[d+1],u=n.map(l=>({start:l.start-t,end:l.end-t}));t+=this.calculateMaxDiff(u,c)}t+=this.params.spaceAroundRadius});const a=t/2;return{childOffsets:o.map(h=>h-a),subtreeSpans:n.map(h=>({start:h.start-a,end:h.end-a}))}}calculateMaxDiff(e,t){let o=0;const s=Math.min(e.length,t.length);for(let n=0;n<s;n++){const a=e[n].end-t[n].start;o=Math.max(o,a)}return o-2*this.params.spaceAroundRadius}}class Pr{constructor(e,t){i(this,"offsets",new Map);i(this,"treeSpans",new Map);this.tree=e;const o=t.spaceAroundRadius,s=new br({spaceAroundRadius:o});[...this.tree.sequence].reverse().forEach(n=>{const a=Array.from(n.children).map(c=>this.treeSpans.get(c.nodeId)),h=s.generate(a);let d=0;n.children.forEach(c=>{this.offsets.set(c.nodeId,h.childOffsets[d]),d++}),this.treeSpans.set(n.nodeId,[{start:-o,end:o},...h.subtreeSpans]),n.children.forEach(c=>{this.treeSpans.delete(c.nodeId)})}),this.offsets.set(this.tree.root.nodeId,0)}generate(){return this.offsets}}class Nr{constructor(e){this.params=e}calculateCoordinates(e){const t=new Map,s=new Sr(e.graph,this.params.nextLayerNodesResolver).generate();let n=0;return s.forEach(a=>{t.set(a.root.nodeId,{x:n,y:0});const d=new Pr(a,{spaceAroundRadius:this.params.layerSpace/2}).generate();let c=[a.root];for(;c.length>0;){const u=[];n+=this.params.layerWidth,c.forEach(l=>{l.children.forEach(g=>{const p=t.get(l.nodeId).y;t.set(g.nodeId,{y:p+d.get(g.nodeId),x:n}),u.push(g)})}),c=u}}),t.forEach((a,h)=>{t.set(h,this.params.transform(a))}),t}}const Cr=r=>{const{graph:e,currentNodeId:t}=r,o=e.getNodeOutgoingEdgeIds(t).map(n=>{const a=e.getEdge(n);return e.getPort(a.to).nodeId}),s=e.getNodeIncomingEdgeIds(t).map(n=>{const a=e.getEdge(n);return e.getPort(a.from).nodeId});return new Set([...o,...s])},Tr=r=>{const{graph:e,currentNodeId:t}=r,o=e.getNodeOutgoingEdgeIds(t).map(s=>{const n=e.getEdge(s);return e.getPort(n.to).nodeId});return new Set(o)},Mr=r=>{const{graph:e,currentNodeId:t}=r,o=e.getNodeIncomingEdgeIds(t).map(s=>{const n=e.getEdge(s);return e.getPort(n.from).nodeId});return new Set(o)};class Dr{constructor(e){i(this,"distanceVectorGenerator");i(this,"nodeForcesApplicationStrategy");i(this,"convergenceVelocity");i(this,"maxTimeDeltaSec");i(this,"nodeMass");i(this,"edgeEquilibriumLength");i(this,"edgeStiffness");i(this,"fillerLayoutAlgorithm");this.convergenceVelocity=e.convergenceVelocity,this.maxTimeDeltaSec=e.maxTimeDeltaSec,this.nodeMass=e.nodeMass,this.edgeEquilibriumLength=e.edgeEquilibriumLength,this.edgeStiffness=e.edgeStiffness,this.distanceVectorGenerator=new dt(e.rand),this.nodeForcesApplicationStrategy=lt({distanceVectorGenerator:this.distanceVectorGenerator,nodeCharge:e.nodeCharge,maxForce:e.maxForce,nodeForceCoefficient:e.nodeForceCoefficient,theta:e.barnesHutTheta,areaRadiusThreshold:e.barnesHutAreaRadiusThreshold,nodeMass:e.nodeMass}),this.fillerLayoutAlgorithm=new ut({rand:e.rand,sparsity:e.edgeEquilibriumLength})}calculateNextCoordinates(e){const{graph:t,viewport:o,dt:s}=e,n=this.fillerLayoutAlgorithm.calculateCoordinates({graph:t,viewport:o});return new ht(t,n,{distanceVectorGenerator:this.distanceVectorGenerator,nodeForcesApplicationStrategy:this.nodeForcesApplicationStrategy,dtSec:Math.min(s,this.maxTimeDeltaSec),nodeMass:this.nodeMass,edgeEquilibriumLength:this.edgeEquilibriumLength,edgeStiffness:this.edgeStiffness}).apply()<this.convergenceVelocity&&!t.getAllNodeIds().some(c=>{const u=t.getNode(c);return u.x===null||u.y===null})?new Map:n}}const gt=r=>{let e=1779033703,t=3144134277,o=1013904242,s=2773480762;for(let n=0,a;n<r.length;n++)a=r.charCodeAt(n),e=t^Math.imul(e^a,597399067),t=o^Math.imul(t^a,2869860233),o=s^Math.imul(o^a,951274213),s=e^Math.imul(s^a,2716044179);return e=Math.imul(o^e>>>18,597399067),t=Math.imul(s^t>>>22,2869860233),o=Math.imul(e^o>>>17,951274213),s=Math.imul(t^s>>>19,2716044179),e^=t^o^s,t^=e,o^=e,s^=e,[e>>>0,t>>>0,o>>>0,s>>>0]},pt=(r,e,t,o)=>function(){r|=0,e|=0,t|=0,o|=0;const s=(r+e|0)+o|0;return o=o+1|0,r=e^e>>>9,e=t+(t<<3)|0,t=t<<21|t>>>11,t=t+s|0,(s>>>0)/4294967296},Rr=r=>{var e,t;switch(r==null?void 0:r.type){case"custom":return r.instance;default:{const o=gt((r==null?void 0:r.seed)??P.seed),s=pt(o[0],o[1],o[2],o[3]);return new Dr({rand:s,maxTimeDeltaSec:(r==null?void 0:r.maxTimeDeltaSec)??P.maxTimeDeltaSec,nodeCharge:(r==null?void 0:r.nodeCharge)??P.nodeCharge,nodeMass:(r==null?void 0:r.nodeMass)??P.nodeMass,edgeEquilibriumLength:(r==null?void 0:r.edgeEquilibriumLength)??P.edgeEquilibriumLength,edgeStiffness:(r==null?void 0:r.edgeStiffness)??P.edgeStiffness,convergenceVelocity:(r==null?void 0:r.convergenceVelocity)??P.convergenceVelocity,maxForce:(r==null?void 0:r.maxForce)??P.maxForce,nodeForceCoefficient:(r==null?void 0:r.nodeForceCoefficient)??P.nodeForceCoefficient,barnesHutTheta:((e=r==null?void 0:r.barnesHut)==null?void 0:e.theta)??P.barnesHutTheta,barnesHutAreaRadiusThreshold:((t=r==null?void 0:r.barnesHut)==null?void 0:t.areaRadiusThreshold)??P.barnesHutAreaRadiusThreshold})}}},Lr={staticNodeResolver:()=>!1},Vr=r=>{var t,o;return{algorithm:Rr((r==null?void 0:r.algorithm)??{}),staticNodeResolver:(r==null?void 0:r.staticNodeResolver)??Lr.staticNodeResolver,onBeforeApplied:((t=r==null?void 0:r.events)==null?void 0:t.onBeforeApplied)??C,onAfterApplied:((o=r==null?void 0:r.events)==null?void 0:o.onAfterApplied)??C}},Ir=r=>r instanceof k?{type:"trigger",trigger:r}:r==="topologyChangeMicrotask"?{type:"topologyChangeSchedule",schedule:Qe}:(r==null?void 0:r.type)==="topologyChangeMacrotask"?{type:"topologyChangeSchedule",schedule:Jt}:{type:"topologyChangeSchedule",schedule:Qe},xe=Object.freeze({staticNodeResolver:()=>!1,hierarchicalLayout:{layerWidth:300,layerSpace:300}}),Z=(r,e)=>({a:r.a*e.a+r.b*e.d,b:r.a*e.b+r.b*e.e,c:r.a*e.c+r.b*e.f+r.c,d:r.d*e.a+r.e*e.d,e:r.d*e.b+r.e*e.e,f:r.d*e.c+r.e*e.f+r.f}),Fr=r=>{const{a:e,b:t,c:o,d:s,e:n,f:a}=r,h=e*n-t*s;return{a:n/h,b:-t/h,c:(t*a-o*n)/h,d:-s/h,e:e/h,f:(o*s-e*a)/h}};class Ur{resolve(e){if("shift"in e)return this.createShiftBaseMatrix(e.shift);if("scale"in e){const t=e.origin??{x:0,y:0};return this.createScaleRelativeMatrix(e.scale,t)}if("rotate"in e){const t=e.origin??{x:0,y:0};return this.createRotateRelativeMatrix(e.rotate,t)}if("mirror"in e){const t=e.origin??{x:0,y:0};return this.createMirrorRelativeMatrix(e.mirror,t)}return{a:e.a??1,b:e.b??0,c:e.c??0,d:e.d??0,e:e.e??1,f:e.f??0}}createScaleRelativeMatrix(e,t){const o=this.createScaleBaseMatrix(e),s=this.createShiftBaseMatrix(t);return this.createRelativeTransform(o,s)}createRotateRelativeMatrix(e,t){const o=this.createRotateBaseMatrix(e),s=this.createShiftBaseMatrix(t);return this.createRelativeTransform(o,s)}createMirrorRelativeMatrix(e,t){const o=this.createMirrorYBaseMatrix(),s=Z(this.createShiftBaseMatrix(t),this.createRotateBaseMatrix(e));return this.createRelativeTransform(o,s)}createRelativeTransform(e,t){const o=Z(t,e),s=Fr(t);return Z(o,s)}createShiftBaseMatrix(e){return{a:1,b:0,c:e.x,d:0,e:1,f:e.y}}createScaleBaseMatrix(e){return{a:e,b:0,c:0,d:0,e,f:0}}createRotateBaseMatrix(e){const t=Math.sin(e),o=Math.cos(e);return{a:o,b:-t,c:0,d:t,e:o,f:0}}createMirrorYBaseMatrix(){return{a:1,b:0,c:0,d:0,e:-1,f:0}}}const $r=r=>{if(r===void 0)return s=>s;if(typeof r=="function")return r;const e=Array.isArray(r)?r:[r];let t={a:1,b:0,c:0,d:0,e:1,f:0};const o=new Ur;return e.forEach(s=>{const n=o.resolve(s);t=Z(t,n)}),s=>{const{x:n,y:a}=s;return{x:t.a*n+t.b*a+t.c,y:t.d*n+t.e*a+t.f}}},Br=r=>{if(typeof r=="function")return r;switch(r){case"outgoing":return Tr;case"incoming":return Mr;default:return Cr}},Or=r=>{var e,t;switch(r==null?void 0:r.type){case"custom":return r.instance;case"hierarchical":return new Nr({layerWidth:r.layerWidth??xe.hierarchicalLayout.layerWidth,layerSpace:r.layerSpace??xe.hierarchicalLayout.layerSpace,transform:$r(r.transform),nextLayerNodesResolver:Br(r.nextLayerNodesResolver)});default:{const o=gt((r==null?void 0:r.seed)??P.seed),s=pt(o[0],o[1],o[2],o[3]);return new Ar({dtSec:(r==null?void 0:r.dtSec)??P.dtSec,maxIterations:(r==null?void 0:r.maxIterations)??P.maxIterations,rand:s,nodeCharge:(r==null?void 0:r.nodeCharge)??P.nodeCharge,nodeMass:(r==null?void 0:r.nodeMass)??P.nodeMass,edgeEquilibriumLength:(r==null?void 0:r.edgeEquilibriumLength)??P.edgeEquilibriumLength,edgeStiffness:(r==null?void 0:r.edgeStiffness)??P.edgeStiffness,convergenceVelocity:(r==null?void 0:r.convergenceVelocity)??P.convergenceVelocity,maxForce:(r==null?void 0:r.maxForce)??P.maxForce,nodeForceCoefficient:(r==null?void 0:r.nodeForceCoefficient)??P.nodeForceCoefficient,barnesHutTheta:((e=r==null?void 0:r.barnesHut)==null?void 0:e.theta)??P.barnesHutTheta,barnesHutAreaRadiusThreshold:((t=r==null?void 0:r.barnesHut)==null?void 0:t.areaRadiusThreshold)??P.barnesHutAreaRadiusThreshold})}}},Wr=r=>{var e,t;return{algorithm:Or(r.algorithm),applyOn:Ir(r.applyOn),staticNodeResolver:r.staticNodeResolver??xe.staticNodeResolver,onBeforeApplied:((e=r.events)==null?void 0:e.onBeforeApplied)??C,onAfterApplied:((t=r.events)==null?void 0:t.onAfterApplied)??C}},zr=(r,e)=>({...r,onNodeDragStarted:t=>{e.add(t),r.onNodeDragStarted(t)},onNodeDragFinished:t=>{e.delete(t),r.onNodeDragFinished(t)}}),kr=(r,e)=>{r.graph.onBeforeNodeRemoved.subscribe(t=>{e.delete(t)}),r.graph.onBeforeClear.subscribe(()=>{e.clear()}),r.onBeforeDestroy.subscribe(()=>{e.clear()})},Hr=(r,e)=>({...r,staticNodeResolver:t=>r.staticNodeResolver(t)||e.has(t)}),Ae=r=>()=>r,wt=Ae(0),Yr=()=>{let r=0;return()=>r++},Xr=(r,e)=>{let t=wt,o=wt;const s=Yr();return r==="incremental"&&(t=s),e==="incremental"&&(o=s),typeof r=="number"&&(t=Ae(r)),typeof e=="number"&&(o=Ae(e)),typeof r=="function"&&(t=r),typeof e=="function"&&(o=e),{nodesPriorityFn:t,edgesPriorityFn:o}},Gr=r=>{var t,o,s,n,a;const e=Xr((t=r.nodes)==null?void 0:t.priority,(o=r.edges)==null?void 0:o.priority);return{nodes:{centerFn:((s=r.nodes)==null?void 0:s.centerFn)??Re,priorityFn:e.nodesPriorityFn},ports:{direction:((n=r.ports)==null?void 0:n.direction)??0},edges:{shapeFactory:Ee(((a=r.edges)==null?void 0:a.shape)??{}),priorityFn:e.edgesPriorityFn}}},jr=r=>r.applyOn.type==="topologyChangeSchedule"?r.applyOn.schedule:se,Jr=r=>{var o,s,n,a;const{canvasDefaults:e}=r,t=r.hasLayout?jr(r.layoutParams):se;return{focus:{contentPadding:((o=e.focus)==null?void 0:o.contentPadding)??((s=e.focus)==null?void 0:s.contentOffset)??100,minContentScale:((n=e.focus)==null?void 0:n.minContentScale)??0,schedule:t,animationDuration:((a=e.focus)==null?void 0:a.animationDuration)??0}}},Kr=r=>({onNodeSelected:r.onNodeSelected,mouseDownEventVerifier:r.mouseDownEventVerifier??I.mouseDownEventVerifier,mouseUpEventVerifier:r.mouseUpEventVerifier??I.mouseUpEventVerifier,movementThreshold:r.movementThreshold??I.movementThreshold}),Qr=r=>({onCanvasSelected:r.onCanvasSelected,mouseDownEventVerifier:r.mouseDownEventVerifier??I.mouseDownEventVerifier,mouseUpEventVerifier:r.mouseUpEventVerifier??I.mouseUpEventVerifier,movementThreshold:r.movementThreshold??I.movementThreshold}),Zr=r=>({onEdgeSelected:r.onEdgeSelected,mouseDownEventVerifier:r.mouseDownEventVerifier??I.mouseDownEventVerifier,mouseUpEventVerifier:r.mouseUpEventVerifier??I.mouseUpEventVerifier,movementThreshold:r.movementThreshold??I.movementThreshold});class _r{constructor(e){i(this,"used",!1);i(this,"canvasDefaults",{});i(this,"dragConfig",{});i(this,"transformConfig",{});i(this,"backgroundConfig",{});i(this,"connectablePortsConfig",{});i(this,"draggableEdgesConfig",{});i(this,"virtualScrollConfig");i(this,"layoutConfig",{});i(this,"animatedLayoutConfig",{});i(this,"userSelectableNodesConfig");i(this,"userSelectableEdgesConfig");i(this,"userSelectableCanvasConfig");i(this,"hasDraggableNodes",!1);i(this,"hasTransformableViewport",!1);i(this,"hasNodeResizeReactiveEdges",!1);i(this,"hasBackground",!1);i(this,"hasUserConnectablePorts",!1);i(this,"hasUserDraggableEdges",!1);i(this,"hasAnimatedLayout",!1);i(this,"hasLayout",!1);i(this,"boxRenderingTrigger",new k);i(this,"window",window);i(this,"animationStaticNodes",new Set);i(this,"pointInsideVerifier");i(this,"eventTagger",new Qt);this.element=e,this.pointInsideVerifier=new Nt(e,this.window)}setDefaults(e){return this.canvasDefaults=e,this}enableUserDraggableNodes(e){return this.hasDraggableNodes=!0,this.dragConfig=e??{},this}enableUserTransformableViewport(e){return this.hasTransformableViewport=!0,this.transformConfig=e??{},this}enableNodeResizeReactiveEdges(){return this.hasNodeResizeReactiveEdges=!0,this}enableVirtualScroll(e){return this.virtualScrollConfig=e,this}enableBackground(e){return this.hasBackground=!0,this.backgroundConfig=e??{},this}enableUserConnectablePorts(e){return this.hasUserConnectablePorts=!0,this.connectablePortsConfig=e??{},this}enableUserDraggableEdges(e){return this.hasUserDraggableEdges=!0,this.draggableEdgesConfig=e??{},this}enableLayout(e){return this.layoutConfig=e??{},this.hasLayout=!0,this.hasAnimatedLayout=!1,this}enableAnimatedLayout(e){return this.animatedLayoutConfig=e??{},this.hasAnimatedLayout=!0,this.hasLayout=!1,this}enableUserSelectableNodes(e){return this.userSelectableNodesConfig=e,this}enableUserSelectableEdges(e){return this.userSelectableEdgesConfig=e,this}enableUserSelectableCanvas(e){return this.userSelectableCanvasConfig=e,this}build(){if(this.used)throw new at("Failed to build Canvas because CanvasBuilder is a single-use object");this.used=!0;const e=new St(this.element),t=new Pe,o=new nr(this.element),s=this.createHtmlView(o.main,t,e),n=Gr(this.canvasDefaults),a=new Ke(t,s,n),h=Wr(this.layoutConfig),d=Jr({canvasDefaults:this.canvasDefaults,hasLayout:this.hasLayout,layoutParams:h}),c=new et(t,e,d,this.window),u=new Ze(e),l=new Je(t),g=new be(l,u,a,c);if(this.hasBackground&&ce.configure(g,gr(this.backgroundConfig),o.background),this.hasNodeResizeReactiveEdges&&_.configure(g),this.userSelectableEdgesConfig!==void 0){const p=Zr(this.userSelectableEdgesConfig);fe.configure(g,this.window,this.pointInsideVerifier,this.eventTagger,p)}if(this.userSelectableNodesConfig!==void 0){const p=Kr(this.userSelectableNodesConfig);ye.configure(g,this.window,this.pointInsideVerifier,this.eventTagger,p)}if(this.userSelectableCanvasConfig!==void 0){const p=Qr(this.userSelectableCanvasConfig);ve.configure(g,o.main,this.window,this.pointInsideVerifier,this.eventTagger,p)}if(this.hasDraggableNodes){let p=ar(this.dragConfig);this.hasAnimatedLayout&&(p=zr(p,this.animationStaticNodes)),he.configure(g,o.main,this.window,this.pointInsideVerifier,p)}if(this.hasUserConnectablePorts){const p=pr(this.connectablePortsConfig,n.edges.shapeFactory,g.graph);le.configure(g,o.overlayConnectablePorts,e,this.window,this.pointInsideVerifier,p)}if(this.hasUserDraggableEdges){const p=wr(this.draggableEdgesConfig,g.graph);ue.configure(g,o.overlayDraggableEdges,e,this.window,this.pointInsideVerifier,p)}if(this.virtualScrollConfig!==void 0?de.configure(g,o.main,this.window,nt(this.transformConfig),this.boxRenderingTrigger,this.pointInsideVerifier,yr(this.virtualScrollConfig)):this.hasTransformableViewport&&K.configure(g,o.main,this.window,this.pointInsideVerifier,nt(this.transformConfig)),this.hasLayout&&rr.configure(g,h),this.hasAnimatedLayout){let p=Vr(this.animatedLayoutConfig);this.hasDraggableNodes&&(kr(g,this.animationStaticNodes),p=Hr(p,this.animationStaticNodes)),we.configure(g,p,this.window)}return g.onBeforeDestroy.subscribe(()=>{o.destroy()}),g}createHtmlView(e,t,o){let s=new Se(t,o,e);return this.virtualScrollConfig!==void 0&&(s=new Et(s,t,this.boxRenderingTrigger,fr(this.virtualScrollConfig))),s=new xt(s,t),s}}A.BezierEdgeShape=$e,A.CanvasBuilder=_r,A.CanvasBuilderError=at,A.CanvasError=S,A.ConnectionCategory=T,A.DirectEdgeShape=te,A.EventSubject=k,A.HorizontalEdgeShape=Oe,A.InteractiveEdgeError=je,A.InteractiveEdgeShape=re,A.MidpointEdgeShape=jt,A.StraightEdgeShape=We,A.VerticalEdgeShape=He,A.boxPortOffsetFn=Yt,Object.defineProperty(A,Symbol.toStringTag,{value:"Module"})});