import*as Common from"../../common/common.js";import*as Platform from"../../platform/platform.js";import{EdgeTypes,EdgeView,generateEdgePortIdsByData}from"./EdgeView.js";import{NodeLabelGenerator,NodeView}from"./NodeView.js";export class GraphView extends Common.ObjectWrapper.ObjectWrapper{constructor(e){super(),this.contextId=e,this._nodes=new Map,this._edges=new Map,this._outboundEdgeMap=new Platform.MapUtilities.Multimap,this._inboundEdgeMap=new Platform.MapUtilities.Multimap,this._nodeLabelGenerator=new NodeLabelGenerator,this._paramIdToNodeIdMap=new Map}addNode(e){const d=this._nodeLabelGenerator.generateLabel(e.nodeType),o=new NodeView(e,d);this._nodes.set(e.nodeId,o),this._notifyShouldRedraw()}removeNode(e){this._outboundEdgeMap.get(e).forEach((e=>this._removeEdge(e))),this._inboundEdgeMap.get(e).forEach((e=>this._removeEdge(e))),this._nodes.delete(e),this._notifyShouldRedraw()}addParam(e){const d=this.getNodeById(e.nodeId);d?(d.addParamPort(e.paramId,e.paramType),this._paramIdToNodeIdMap.set(e.paramId,e.nodeId),this._notifyShouldRedraw()):console.error("AudioNode should be added before AudioParam")}removeParam(e){this._paramIdToNodeIdMap.delete(e)}addNodeToNodeConnection(e){const d=new EdgeView(e,EdgeTypes.NodeToNode);this._addEdge(d)}removeNodeToNodeConnection(e){if(e.destinationId){const d=generateEdgePortIdsByData(e,EdgeTypes.NodeToNode);if(!d)throw new Error("Unable to generate edge port IDs");const{edgeId:o}=d;this._removeEdge(o)}else this._outboundEdgeMap.get(e.sourceId).forEach((e=>this._removeEdge(e)))}addNodeToParamConnection(e){const d=new EdgeView(e,EdgeTypes.NodeToParam);this._addEdge(d)}removeNodeToParamConnection(e){const d=generateEdgePortIdsByData(e,EdgeTypes.NodeToParam);if(!d)throw new Error("Unable to generate edge port IDs");const{edgeId:o}=d;this._removeEdge(o)}getNodeById(e){return this._nodes.get(e)||null}getNodes(){return this._nodes}getEdges(){return this._edges}getNodeIdByParamId(e){return this._paramIdToNodeIdMap.get(e)||null}_addEdge(e){const d=e.sourceId;this._outboundEdgeMap.hasValue(d,e.id)||(this._edges.set(e.id,e),this._outboundEdgeMap.set(d,e.id),this._inboundEdgeMap.set(e.destinationId,e.id),this._notifyShouldRedraw())}_removeEdge(e){const d=this._edges.get(e);d&&(this._outboundEdgeMap.delete(d.sourceId,e),this._inboundEdgeMap.delete(d.destinationId,e),this._edges.delete(e),this._notifyShouldRedraw())}_notifyShouldRedraw(){this.dispatchEventToListeners(Events.ShouldRedraw,this)}}export const Events={ShouldRedraw:Symbol("ShouldRedraw")};