@jalez/react-flow-automated-layout
Version:
A React library for automated layout of nested node graphs with parent-child relationships using React Flow
23 lines (22 loc) • 28.2 kB
JavaScript
(function(P,j){typeof exports=="object"&&typeof module<"u"?j(exports,require("react"),require("@xyflow/react"),require("@dagrejs/dagre")):typeof define=="function"&&define.amd?define(["exports","react","@xyflow/react","@dagrejs/dagre"],j):(P=typeof globalThis<"u"?globalThis:P||self,j(P.reactFlowAutomatedLayout={},P.React,P.ReactFlow,P.dagre))})(this,function(P,j,R,de){"use strict";var te={exports:{}},K={};/**
* @license React
* react-jsx-runtime.production.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/var ue;function Te(){if(ue)return K;ue=1;var r=Symbol.for("react.transitional.element"),o=Symbol.for("react.fragment");function n(c,s,a){var d=null;if(a!==void 0&&(d=""+a),s.key!==void 0&&(d=""+s.key),"key"in s){a={};for(var l in s)l!=="key"&&(a[l]=s[l])}else a=s;return s=a.ref,{$$typeof:r,type:c,key:d,ref:s!==void 0?s:null,props:a}}return K.Fragment=o,K.jsx=n,K.jsxs=n,K}var q={};/**
* @license React
* react-jsx-runtime.development.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/var he;function ke(){return he||(he=1,process.env.NODE_ENV!=="production"&&function(){function r(t){if(t==null)return null;if(typeof t=="function")return t.$$typeof===N?null:t.displayName||t.name||null;if(typeof t=="string")return t;switch(t){case T:return"Fragment";case b:return"Profiler";case f:return"StrictMode";case y:return"Suspense";case C:return"SuspenseList";case Y:return"Activity"}if(typeof t=="object")switch(typeof t.tag=="number"&&console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),t.$$typeof){case v:return"Portal";case g:return(t.displayName||"Context")+".Provider";case E:return(t._context.displayName||"Context")+".Consumer";case x:var w=t.render;return t=t.displayName,t||(t=w.displayName||w.name||"",t=t!==""?"ForwardRef("+t+")":"ForwardRef"),t;case L:return w=t.displayName||null,w!==null?w:r(t.type)||"Memo";case I:w=t._payload,t=t._init;try{return r(t(w))}catch{}}return null}function o(t){return""+t}function n(t){try{o(t);var w=!1}catch{w=!0}if(w){w=console;var S=w.error,_=typeof Symbol=="function"&&Symbol.toStringTag&&t[Symbol.toStringTag]||t.constructor.name||"Object";return S.call(w,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",_),o(t)}}function c(t){if(t===T)return"<>";if(typeof t=="object"&&t!==null&&t.$$typeof===I)return"<...>";try{var w=r(t);return w?"<"+w+">":"<...>"}catch{return"<...>"}}function s(){var t=A.A;return t===null?null:t.getOwner()}function a(){return Error("react-stack-top-frame")}function d(t){if(M.call(t,"key")){var w=Object.getOwnPropertyDescriptor(t,"key").get;if(w&&w.isReactWarning)return!1}return t.key!==void 0}function l(t,w){function S(){J||(J=!0,console.error("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",w))}S.isReactWarning=!0,Object.defineProperty(t,"key",{get:S,configurable:!0})}function u(){var t=r(this.type);return X[t]||(X[t]=!0,console.error("Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.")),t=this.props.ref,t!==void 0?t:null}function h(t,w,S,_,B,D,$,ee){return S=D.ref,t={$$typeof:k,type:t,key:w,props:D,_owner:B},(S!==void 0?S:null)!==null?Object.defineProperty(t,"ref",{enumerable:!1,get:u}):Object.defineProperty(t,"ref",{enumerable:!1,value:null}),t._store={},Object.defineProperty(t._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(t,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(t,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:$}),Object.defineProperty(t,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:ee}),Object.freeze&&(Object.freeze(t.props),Object.freeze(t)),t}function i(t,w,S,_,B,D,$,ee){var O=w.children;if(O!==void 0)if(_)if(V(O)){for(_=0;_<O.length;_++)m(O[_]);Object.freeze&&Object.freeze(O)}else console.error("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else m(O);if(M.call(w,"key")){O=r(t);var F=Object.keys(w).filter(function(re){return re!=="key"});_=0<F.length?"{key: someKey, "+F.join(": ..., ")+": ...}":"{key: someKey}",H[O+_]||(F=0<F.length?"{"+F.join(": ..., ")+": ...}":"{}",console.error(`A props object containing a "key" prop is being spread into JSX:
let props = %s;
<%s {...props} />
React keys must be passed directly to JSX without using spread:
let props = %s;
<%s key={someKey} {...props} />`,_,O,F,O),H[O+_]=!0)}if(O=null,S!==void 0&&(n(S),O=""+S),d(w)&&(n(w.key),O=""+w.key),"key"in w){S={};for(var U in w)U!=="key"&&(S[U]=w[U])}else S=w;return O&&l(S,typeof t=="function"?t.displayName||t.name||"Unknown":t),h(t,O,D,B,s(),S,$,ee)}function m(t){typeof t=="object"&&t!==null&&t.$$typeof===k&&t._store&&(t._store.validated=1)}var p=j,k=Symbol.for("react.transitional.element"),v=Symbol.for("react.portal"),T=Symbol.for("react.fragment"),f=Symbol.for("react.strict_mode"),b=Symbol.for("react.profiler"),E=Symbol.for("react.consumer"),g=Symbol.for("react.context"),x=Symbol.for("react.forward_ref"),y=Symbol.for("react.suspense"),C=Symbol.for("react.suspense_list"),L=Symbol.for("react.memo"),I=Symbol.for("react.lazy"),Y=Symbol.for("react.activity"),N=Symbol.for("react.client.reference"),A=p.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,M=Object.prototype.hasOwnProperty,V=Array.isArray,W=console.createTask?console.createTask:function(){return null};p={"react-stack-bottom-frame":function(t){return t()}};var J,X={},Z=p["react-stack-bottom-frame"].bind(p,a)(),Q=W(c(a)),H={};q.Fragment=T,q.jsx=function(t,w,S,_,B){var D=1e4>A.recentlyCreatedOwnerStacks++;return i(t,w,S,!1,_,B,D?Error("react-stack-top-frame"):Z,D?W(c(t)):Q)},q.jsxs=function(t,w,S,_,B){var D=1e4>A.recentlyCreatedOwnerStacks++;return i(t,w,S,!0,_,B,D?Error("react-stack-top-frame"):Z,D?W(c(t)):Q)}}()),q}var fe;function Re(){return fe||(fe=1,process.env.NODE_ENV==="production"?te.exports=Te():te.exports=ke()),te.exports}var e=Re();const pe=j.createContext(void 0);function G(){const r=j.useContext(pe);if(r===void 0)throw new Error("useLayoutContext must be used within a LayoutProvider");return r}const Se=r=>{switch(r){case"DOWN":return"TB";case"RIGHT":return"LR";case"UP":return"BT";case"LEFT":return"RL";default:return"TB"}},ge=r=>{switch(r){case"TB":return"DOWN";case"LR":return"RIGHT";case"BT":return"UP";case"RL":return"LEFT";default:return"DOWN"}},Ce={enabled:!0,padding:{horizontal:80,vertical:48},respectHeaderHeight:!0,minWidth:200,minHeight:100},Ne=r=>{switch(r){case"DOWN":return R.Position.Bottom;case"RIGHT":return R.Position.Right;case"UP":return R.Position.Top;case"LEFT":return R.Position.Left;default:return R.Position.Bottom}},Le=r=>{switch(r){case"DOWN":return R.Position.Top;case"RIGHT":return R.Position.Left;case"UP":return R.Position.Bottom;case"LEFT":return R.Position.Right;default:return R.Position.Top}};function Oe(r,o,n){if(n)return{nodes:r,edges:o};const c=r.filter(d=>!d.hidden),s=new Set(c.map(d=>d.id)),a=o.filter(d=>s.has(d.source)&&s.has(d.target));return{nodes:c,edges:a}}const _e=172,Pe=36,ne=async(r,o,n,c=0,s=50,a=50,d=_e,l=Pe,u=!1)=>{const h=new de.graphlib.Graph().setDefaultEdgeLabel(()=>({}));h.setGraph({rankdir:n,marginx:c,marginy:c,nodesep:s,ranksep:a,edgesep:Math.max(20,s/4),ranker:"tight-tree"});const i=u?r:r.filter(f=>!f.hidden);if(i.length===0)return{nodes:[],edges:[],width:0,height:0};i.forEach(f=>{var g,x;const b=Number((g=f.style)==null?void 0:g.width)||d,E=Number((x=f.style)==null?void 0:x.height)||l;h.setNode(f.id,{width:b,height:E})}),o.forEach(f=>{const b=f.sourceHandle,E=f.targetHandle;let g={};b==="right"&&E==="left"?g={constraint:!1,minlen:1}:b==="bottom"&&E==="top"?g={constraint:!0,minlen:1}:g={constraint:!0,minlen:2},h.setEdge(f.source,f.target,g)}),de.layout(h);const m=h.graph().width||0,p=h.graph().height||0,k=Ne(ge(n)),v=Le(ge(n));return{nodes:i.map(f=>{var L;const b=h.node(f.id),{width:E,height:g}=b,x={...f,sourcePosition:k,targetPosition:v,selected:!1},y=b.x,C=b.y;switch((L=f.data)==null?void 0:L.positionType){case"center":x.position={x:y,y:C};break;case"topRight":x.position={x:y+E/2,y:C-g/2};break;case"bottomLeft":x.position={x:y-E/2,y:C+g/2};break;case"bottomRight":x.position={x:y+E/2,y:C+g/2};break;case"topLeft":default:x.position={x:y-E/2,y:C-g/2}}return x}),edges:o,width:m,height:p}},xe={calculate:async(r,o,n)=>{const c=n.nodes||r,s=n.edges||o,d=n.dagreDirection||typeof n.direction=="string"&&(n.direction==="RIGHT"||n.direction==="LEFT"?"LR":"TB")||"TB",l=n.margin??(typeof n.padding=="number"?n.padding:0),u=n.nodeSpacing??50,h=n.layerSpacing??50,i=n.nodeWidth??172,m=n.nodeHeight??36,p=n.layoutHidden??!1;return ne(c,s,d,l,u,h,i,m,p)}},ye={dagre:xe};function Ae({initialDirection:r="DOWN",initialAlgorithm:o="layered",initialAutoLayout:n=!0,initialPadding:c=50,initialSpacing:s={node:50,layer:50},initialNodeDimensions:a={width:172,height:36},initialParentResizingOptions:d,includeHidden:l=!1,layoutEngines:u}){const[h,i]=j.useState(r),[m,p]=j.useState(o),[k,v]=j.useState(n),[T,f]=j.useState(!1),[b,E]=j.useState(l),[g,x]=j.useState({...ye,...u}),[y,C]=j.useState({}),[L,I]=j.useState(c),[Y,N]=j.useState(s.node||150),[A,M]=j.useState(s.layer||180),[V,W]=j.useState(a.width||100),[J,X]=j.useState(a.height||100),[Z,Q]=j.useState({...Ce,...d,enabled:n}),[H,t]=j.useState([]);return{direction:h,algorithm:m,autoLayout:k,layoutInProgress:T,layoutHidden:b,layoutEngines:g,layoutEngineOptions:y,padding:L,nodeSpacing:Y,layerSpacing:A,nodeWidth:V,nodeHeight:J,parentResizingOptions:Z,selectedNodes:H,setDirection:i,setAlgorithm:p,setAutoLayout:v,setLayoutInProgress:f,setLayoutHidden:E,setLayoutEngines:x,setLayoutEngineOptions:C,setPadding:I,setNodeSpacing:N,setLayerSpacing:M,setNodeWidth:W,setNodeHeight:X,setParentResizingOptionsState:Q,setSelectedNodes:t}}function De({nodes:r,externalNodeIdWithNode:o,externalNodeParentIdMapWithChildIdSet:n,noParentKey:c="no-parent"}){const[s,a]=j.useState(new Map),[d,l]=j.useState(new Map),[u,h]=j.useState({}),[i,m]=j.useState(r.length),[p,k]=j.useState(!1),v=o||s,T=n||d,f=v.size;return j.useEffect(()=>{if(!o||!n){const b=new Map,E=new Map;r.forEach(g=>{var y;b.set(g.id,g);const x=g.parentId||c;E.has(x)||E.set(x,new Set),(y=E.get(x))==null||y.add(g.id)}),o||a(b),n||l(E)}},[r,o,n,c]),j.useEffect(()=>{r.length!==i&&m(r.length)},[r]),j.useEffect(()=>{v.size>0&&T.size>0&&!p&&k(!0)},[v,T,p]),j.useEffect(()=>{const b={};T.forEach((g,x)=>{b[x]=g.size});let E=!1;for(const g in b)if(!u.hasOwnProperty(g)||b[g]!==u[g]){E=!0;break}E&&h(b)},[T]),{nodeIdWithNode:v,nodeParentIdMapWithChildIdSet:T,numberOfNodes:f,nodesLength:i,childrenInitialized:p,parentChildStructure:u}}function Me({nodes:r,edges:o,selectedNodes:n,layoutHidden:c,calculateLayout:s,updateNodes:a,updateEdges:d,setLayoutInProgress:l,setNodeSpacing:u,setLayerSpacing:h}){const i=R.useReactFlow(),m=j.useRef(null),p=j.useRef(null);return{applyLayout:j.useCallback(async(v=[],T=[])=>{m.current&&m.current.abort();const f=new AbortController;m.current=f;const b=v.length>0?v:r,E=T.length>0?T:o;if(b.length===0)return{nodes:b,edges:E};try{if(l(!0),f.signal.aborted)return{nodes:b,edges:E};const{nodes:g,edges:x}=Oe(b,E,c),y=await s(g,x,n,f.signal);return f.signal.aborted?{nodes:b,edges:E}:(m.current===f&&(a?a(y.nodes):i!=null&&i.setNodes&&i.setNodes(y.nodes),d?d(y.edges):i!=null&&i.setEdges&&i.setEdges(y.edges),p.current&&(p.current.node!==void 0&&u(p.current.node),p.current.layer!==void 0&&h(p.current.layer),p.current=null)),y)}catch(g){return f.signal.aborted||console.error("Error applying layout:",g),{nodes:b,edges:E}}finally{m.current===f&&(l(!1),m.current=null)}},[r,o,n,s,a,d,i,c,l,u,h]),pendingSpacingUpdateRef:p}}const Be=(r,o,n="no-parent")=>{const c=new Set,s=new Map;r.forEach((l,u)=>{const h=o.get(u);h&&s.set(u,{id:u,node:h,children:[],depth:0})});const a=(l,u)=>{if(c.has(l)||!r.has(l)||!o.get(l))return null;c.add(l);const i=s.get(l);i.depth=u;const m=r.get(l)||new Set;for(const p of m)if(r.has(p)&&o.get(p)){const v=a(p,u+1);v&&i.children.push(v)}return i},d=[];return r.forEach((l,u)=>{const h=o.get(u);if(h){const i=h.parentId;if(!i||i===n){const m=a(u,0);m&&d.push(m)}}}),d};function me(r,o,n="no-parent"){const c=[];let s=r;const a=new Set;for(;s&&s!==n;){if(a.has(s)){console.warn(`Cycle detected in ancestor path for node ${r}`);break}a.add(s),c.push(s);const d=o.get(s);if(!d||!d.parentId){s=n;break}s=d.parentId}return s===n&&c.push(n),c}function Ie(r,o,n,c="no-parent"){const s=me(r,n,c),a=me(o,n,c),d=new Set(a);let l=null,u=-1;for(let p=0;p<s.length;p++)if(d.has(s[p])){l=s[p],u=p;break}if(!l)return{lca:null,sourceChild:null,targetChild:null};const h=a.indexOf(l),i=u>0?s[u-1]:r,m=h>0?a[h-1]:o;return{lca:l,sourceChild:i,targetChild:m}}function we(r,o,n="no-parent"){const c=new Map;for(const s of r){const{lca:a,sourceChild:d,targetChild:l}=Ie(s.source,s.target,o,n);if(a&&d&&l&&d!==l){const u=JSON.parse(JSON.stringify(s));u.source=d,u.target=l,u.id=`temp_${u.id}_${a}`,u.data={...u.data,isTemporary:!0,originalSource:s.source,originalTarget:s.target},c.has(a)||c.set(a,[]),c.get(a).push(u)}}return c}const se=10,je=async(r,o,n,c,s,a=se,d=50,l=50,u=172,h=36,i=ne,m=!1,p="no-parent")=>{const k=we(s,c,p),{updatedNodes:v}=await oe(r,o,n,c,s,a,d,l,u,h,i,m,k),T=c.get(r);if(!T)return{updatedNodes:v,updatedEdges:s};const{updatedNodes:f}=await je(T.parentId||p,o,n,c,s,a,d,l,u,h,i,m,p);return{updatedNodes:[...f,...v],updatedEdges:s}},oe=async(r,o,n,c,s,a=se,d=50,l=50,u=172,h=36,i=ne,m=!1,p=new Map)=>{const k=n.get(r);if(!k||k.size===0)return{updatedNodes:[]};const v=[];if(k.forEach(C=>{const L=c.get(C);L&&v.push(L)}),v.length===0)return{updatedNodes:[]};let T=o;const f=c.get(r);f&&f.data.layoutDirection&&(T=f.data.layoutDirection);const b=p.get(r)||[],{nodes:E,edges:g,width:x,height:y}=await i(v,b,T,a,d,l,u,h,m);return f&&x&&y&&Fe(f,x,y),{updatedNodes:[...E],udpatedParentNode:f||void 0}},Fe=(r,o,n)=>(r.style||(r.style={}),r.width=o,r.height=n,r.measured={width:o,height:n},r.style.width=o,r.style.height=n,r),We=async(r,o,n,c,s,a=se,d=50,l=50,u=172,h=36,i=ne,m=!1,p="no-parent")=>{let k=[];const v=we(s,c,p),T=new Map;let f=0;const b=(g,x)=>{x>f&&(f=x);for(const y of g)T.has(x)||T.set(x,[]),T.get(x).push(y.id),y.children.length>0&&b(y.children,x+1)};b(r,0);for(let g=f;g>=0;g--){const x=T.get(g)||[],y=await Promise.all(x.map(C=>oe(C,o,n,c,s,a,d,l,u,h,i,m,v)));for(const{updatedNodes:C}of y)k=[...C,...k]}const{updatedNodes:E}=await oe(p,o,n,c,s,a,d,l,u,h,i,m,v);return k=[...E,...k],{updatedNodes:k,updatedEdges:s}},ze=(r,o,n,c="no-parent")=>{const s=r.map(h=>h.id),a=r.map(h=>{var i;return((i=n.get(h.id))==null?void 0:i.parentId)||h.parentId}).filter(h=>!!h),l=Array.from(new Set([...s,...a])).filter(h=>o.has(h)),u=l.filter(h=>{const i=n.get(h);return i?!i.parentId||!l.includes(i.parentId):!0});return u.length===0?[c]:u},He=async(r,o,n)=>{const{dagreDirection:c,nodeParentIdMapWithChildIdSet:s,nodeIdWithNode:a,nodes:d,edges:l,margin:u,nodeSpacing:h,layerSpacing:i,nodeWidth:m,nodeHeight:p,layoutHidden:k=!1,noParentKey:v="no-parent"}=o,T=ze(r,s,a,v);if(T.length===0)return{nodes:d,edges:l};const f=new Map,b=new Map;if(n!=null&&n.aborted)return{nodes:d,edges:l};(await Promise.all(T.map(async y=>n!=null&&n.aborted?{updatedNodes:[],updatedEdges:[]}:je(y,c,s,a,l,u,h,i,m,p,void 0,k)))).forEach(({updatedNodes:y,updatedEdges:C})=>{y.forEach(L=>{f.set(L.id,L)}),C.forEach(L=>{b.set(L.id,L)})});const g=d.map(y=>f.has(y.id)?f.get(y.id):y),x=l.map(y=>b.has(y.id)?b.get(y.id):y);return{nodes:g,edges:x}},Ue=(r,o,n,c,s,a,d,l,u=172,h=36,i=!1,m="no-parent")=>({calculateLayout:j.useCallback(async(k,v,T,f)=>{if(!(r[n]||r.dagre))return console.error(`Layout engine "${n}" not found`),{nodes:k,edges:v};const E=i?k:k.filter(N=>!N.hidden),g=new Set(E.map(N=>N.id)),x=i?v:v.filter(N=>g.has(N.source)&&g.has(N.target)),y=Se(o),C=c.padding.horizontal;let L=[],I=[];if(T&&T.length>0){const N=i?T:T.filter(M=>!M.hidden),A=await He(N,{dagreDirection:y,nodeParentIdMapWithChildIdSet:s,nodeIdWithNode:a,nodes:E,edges:x,margin:C,nodeSpacing:d,layerSpacing:l,nodeWidth:u,nodeHeight:h,layoutHidden:i,noParentKey:m},f);L=A.nodes,I=A.edges}else{if(f!=null&&f.aborted)return{nodes:k,edges:v};const N=Be(s,a,m),A=await We(N,y,s,a,x,C,d,l,u,h,void 0,i,m);L=A.updatedNodes,I=A.updatedEdges}return{nodes:k.map(N=>!i&&N.hidden?N:L.find(M=>M.id===N.id)||N),edges:I}},[n,o,r,c.padding.horizontal,s,a,d,l,u,h,i,m])});function Ge({children:r,initialDirection:o="DOWN",initialAlgorithm:n="layered",initialAutoLayout:c=!0,initialPadding:s=50,initialSpacing:a={node:50,layer:50},initialNodeDimensions:d={width:172,height:36},initialParentResizingOptions:l,includeHidden:u=!1,layoutEngines:h,updateNodes:i,updateEdges:m,nodeParentIdMapWithChildIdSet:p,nodeIdWithNode:k,noParentKey:v="no-parent",disableAutoLayoutEffect:T=!1}){const f=R.useNodes(),b=R.useEdges(),{direction:E,algorithm:g,autoLayout:x,layoutInProgress:y,layoutHidden:C,layoutEngines:L,layoutEngineOptions:I,padding:Y,nodeSpacing:N,layerSpacing:A,nodeWidth:M,nodeHeight:V,parentResizingOptions:W,selectedNodes:J,setDirection:X,setAlgorithm:Z,setAutoLayout:Q,setLayoutInProgress:H,setLayoutHidden:t,setLayoutEngines:w,setLayoutEngineOptions:S,setPadding:_,setNodeSpacing:B,setLayerSpacing:D,setNodeWidth:$,setNodeHeight:ee,setParentResizingOptionsState:O,setSelectedNodes:F}=Ae({initialDirection:o,initialAlgorithm:n,initialAutoLayout:c,initialPadding:s,initialSpacing:a,initialNodeDimensions:d,initialParentResizingOptions:l,includeHidden:u,layoutEngines:h}),{nodeIdWithNode:U,nodeParentIdMapWithChildIdSet:re,numberOfNodes:Ve,nodesLength:Je,childrenInitialized:Ee,parentChildStructure:Xe}=De({nodes:f,externalNodeIdWithNode:k,externalNodeParentIdMapWithChildIdSet:p,noParentKey:v}),Ze=j.useCallback(({nodes:z})=>{F(z)},[F]);R.useOnSelectionChange({onChange:Ze});const{calculateLayout:Qe}=Ue(L,E,g,W,re,U,N,A,M,V,C,v),{applyLayout:ve}=Me({nodes:f,edges:b,selectedNodes:J,layoutHidden:C,calculateLayout:Qe,updateNodes:i,updateEdges:m,setLayoutInProgress:H,setNodeSpacing:B,setLayerSpacing:D}),Ke=j.useCallback((z,le)=>{w(et=>({...et,[z]:le}))},[w]),qe=j.useCallback(z=>{O(le=>({...le,...z,enabled:x}))},[x,O]);j.useEffect(()=>{var z;T||U.has((z=f[f.length-1])==null?void 0:z.id)&&Ee&&x&&ve()},[Ee,x,E,Ve,N,A,Xe,Je]);const $e={direction:E,algorithm:g,autoLayout:x,layoutInProgress:y,padding:Y,nodeSpacing:N,layerSpacing:A,nodeWidth:M,nodeHeight:V,layoutHidden:C,parentResizingOptions:W,layoutEngines:L,layoutEngineOptions:I,nodeParentIdMapWithChildIdSet:re,nodeIdWithNode:U,noParentKey:v,updateNodes:i,updateEdges:m,setDirection:X,setAlgorithm:Z,setAutoLayout:Q,setLayoutInProgress:H,setPadding:_,setNodeSpacing:B,setLayerSpacing:D,setNodeWidth:$,setNodeHeight:ee,setLayoutHidden:t,setParentResizingOptions:qe,setLayoutEngineOptions:S,applyLayout:ve,clearLayoutCache:()=>{},registerLayoutEngine:Ke};return e.jsx(pe.Provider,{value:$e,children:r})}const ie=({compact:r=!1})=>{const{direction:o,setDirection:n,clearLayoutCache:c}=G(),s=a=>{n(a),c&&c()};return r?e.jsx(R.ControlButton,{onClick:()=>s(o==="DOWN"?"RIGHT":"DOWN"),title:`Switch to ${o==="DOWN"?"horizontal":"vertical"} layout`,children:e.jsx("svg",{viewBox:"0 0 24 24",width:"16",height:"16",stroke:"currentColor",strokeWidth:"2",fill:"none",children:o==="DOWN"?e.jsx("path",{d:"M4 12h16M16 6l6 6-6 6"}):e.jsx("path",{d:"M12 4v16M6 16l6 6 6-6"})})}):e.jsxs(e.Fragment,{children:[e.jsx(R.ControlButton,{onClick:()=>s("DOWN"),className:o==="DOWN"?"selected":"",title:"Top to Bottom",children:e.jsx("svg",{viewBox:"0 0 24 24",width:"16",height:"16",stroke:"currentColor",strokeWidth:"2",fill:"none",children:e.jsx("path",{d:"M12 4v16M6 16l6 6 6-6"})})}),e.jsx(R.ControlButton,{onClick:()=>s("RIGHT"),className:o==="RIGHT"?"selected":"",title:"Left to Right",children:e.jsx("svg",{viewBox:"0 0 24 24",width:"16",height:"16",stroke:"currentColor",strokeWidth:"2",fill:"none",children:e.jsx("path",{d:"M4 12h16M16 6l6 6-6 6"})})}),e.jsx(R.ControlButton,{onClick:()=>s("LEFT"),className:o==="LEFT"?"selected":"",title:"Right to Left",children:e.jsx("svg",{viewBox:"0 0 24 24",width:"16",height:"16",stroke:"currentColor",strokeWidth:"2",fill:"none",children:e.jsx("path",{d:"M20 12H4M8 6L2 12l6 6"})})}),e.jsx(R.ControlButton,{onClick:()=>s("UP"),className:o==="UP"?"selected":"",title:"Bottom to Top",children:e.jsx("svg",{viewBox:"0 0 24 24",width:"16",height:"16",stroke:"currentColor",strokeWidth:"2",fill:"none",children:e.jsx("path",{d:"M12 20V4M6 8l6-6 6 6"})})})]})},ae=({compact:r=!1})=>{const{nodeSpacing:o,layerSpacing:n,setNodeSpacing:c,setLayerSpacing:s,clearLayoutCache:a}=G(),[d,l]=j.useState(!1),u=i=>{c(i),a&&a()},h=i=>{s(i),a&&a()};return r?e.jsxs("div",{style:{position:"relative"},children:[e.jsx(R.ControlButton,{onClick:()=>l(!d),title:"Adjust node and layer spacing",children:e.jsxs("svg",{viewBox:"0 0 24 24",width:"16",height:"16",stroke:"currentColor",strokeWidth:"2",fill:"none",children:[e.jsx("rect",{x:"3",y:"3",width:"7",height:"7",rx:"1"}),e.jsx("rect",{x:"14",y:"3",width:"7",height:"7",rx:"1"}),e.jsx("rect",{x:"3",y:"14",width:"7",height:"7",rx:"1"}),e.jsx("rect",{x:"14",y:"14",width:"7",height:"7",rx:"1"}),e.jsx("path",{d:"M7 10v4M17 10v4M10 7h4M10 17h4"})]})}),d&&e.jsxs("div",{className:"react-flow-spacing-dropdown",style:{position:"absolute",right:"0",top:"100%",marginTop:"8px",background:"white",border:"1px solid #ddd",borderRadius:"4px",padding:"8px",boxShadow:"0 2px 10px rgba(0,0,0,0.1)",zIndex:10,minWidth:"200px"},children:[e.jsxs("div",{style:{marginBottom:"12px"},children:[e.jsxs("div",{style:{fontWeight:"bold",marginBottom:"4px"},children:["Node Spacing: ",o,"px"]}),e.jsx("div",{style:{display:"flex",gap:"4px"},children:[50,100,150,200].map(i=>e.jsx("button",{onClick:()=>u(i),style:{background:o===i?"#0041d0":"#f5f5f5",color:o===i?"white":"inherit",border:"1px solid #ddd",borderRadius:"4px",padding:"4px 8px",cursor:"pointer"},children:i},i))})]}),e.jsxs("div",{children:[e.jsxs("div",{style:{fontWeight:"bold",marginBottom:"4px"},children:["Layer Spacing: ",n,"px"]}),e.jsx("div",{style:{display:"flex",gap:"4px"},children:[50,100,150,200].map(i=>e.jsx("button",{onClick:()=>h(i),style:{background:n===i?"#0041d0":"#f5f5f5",color:n===i?"white":"inherit",border:"1px solid #ddd",borderRadius:"4px",padding:"4px 8px",cursor:"pointer"},children:i},i))})]}),e.jsx("div",{style:{marginTop:"12px",textAlign:"right"},children:e.jsx("button",{onClick:()=>l(!1),style:{background:"#f5f5f5",border:"1px solid #ddd",borderRadius:"4px",padding:"4px 8px",cursor:"pointer"},children:"Close"})})]})]}):e.jsxs("div",{className:"spacing-controls",children:[e.jsxs(R.ControlButton,{onClick:()=>u(Math.max(50,o-25)),title:"Decrease node spacing",children:[e.jsxs("svg",{viewBox:"0 0 24 24",width:"16",height:"16",stroke:"currentColor",strokeWidth:"2",fill:"none",children:[e.jsx("rect",{x:"3",y:"3",width:"7",height:"7",rx:"1"}),e.jsx("rect",{x:"14",y:"3",width:"7",height:"7",rx:"1"}),e.jsx("rect",{x:"3",y:"14",width:"7",height:"7",rx:"1"}),e.jsx("rect",{x:"14",y:"14",width:"7",height:"7",rx:"1"}),e.jsx("line",{x1:"7",y1:"10",x2:"7",y2:"14"}),e.jsx("line",{x1:"17",y1:"10",x2:"17",y2:"14"}),e.jsx("line",{x1:"10",y1:"7",x2:"14",y2:"7"}),e.jsx("line",{x1:"10",y1:"17",x2:"14",y2:"17"})]}),e.jsx("span",{style:{marginLeft:"4px"},children:"-"})]}),e.jsxs("div",{style:{display:"inline-flex",alignItems:"center",padding:"5px 10px",fontSize:"12px",background:"#f8f8f8",borderRadius:"4px",margin:"0 4px",border:"1px solid #ddd"},children:[o,"px"]}),e.jsxs(R.ControlButton,{onClick:()=>u(Math.min(300,o+25)),title:"Increase node spacing",children:[e.jsxs("svg",{viewBox:"0 0 24 24",width:"16",height:"16",stroke:"currentColor",strokeWidth:"2",fill:"none",children:[e.jsx("rect",{x:"3",y:"3",width:"7",height:"7",rx:"1"}),e.jsx("rect",{x:"14",y:"3",width:"7",height:"7",rx:"1"}),e.jsx("rect",{x:"3",y:"14",width:"7",height:"7",rx:"1"}),e.jsx("rect",{x:"14",y:"14",width:"7",height:"7",rx:"1"}),e.jsx("line",{x1:"7",y1:"10",x2:"7",y2:"14"}),e.jsx("line",{x1:"17",y1:"10",x2:"17",y2:"14"}),e.jsx("line",{x1:"10",y1:"7",x2:"14",y2:"7"}),e.jsx("line",{x1:"10",y1:"17",x2:"14",y2:"17"})]}),e.jsx("span",{style:{marginLeft:"4px"},children:"+"})]}),e.jsx("div",{style:{display:"inline-block",width:"1px",height:"24px",background:"#ddd",margin:"0 10px"}}),e.jsxs(R.ControlButton,{onClick:()=>h(Math.max(50,n-25)),title:"Decrease layer spacing",children:[e.jsxs("svg",{viewBox:"0 0 24 24",width:"16",height:"16",stroke:"currentColor",strokeWidth:"2",fill:"none",children:[e.jsx("line",{x1:"4",y1:"6",x2:"20",y2:"6"}),e.jsx("line",{x1:"4",y1:"12",x2:"20",y2:"12"}),e.jsx("line",{x1:"4",y1:"18",x2:"20",y2:"18"})]}),e.jsx("span",{style:{marginLeft:"4px"},children:"-"})]}),e.jsxs("div",{style:{display:"inline-flex",alignItems:"center",padding:"5px 10px",fontSize:"12px",background:"#f8f8f8",borderRadius:"4px",margin:"0 4px",border:"1px solid #ddd"},children:[n,"px"]}),e.jsxs(R.ControlButton,{onClick:()=>h(Math.min(300,n+25)),title:"Increase layer spacing",children:[e.jsxs("svg",{viewBox:"0 0 24 24",width:"16",height:"16",stroke:"currentColor",strokeWidth:"2",fill:"none",children:[e.jsx("line",{x1:"4",y1:"6",x2:"20",y2:"6"}),e.jsx("line",{x1:"4",y1:"12",x2:"20",y2:"12"}),e.jsx("line",{x1:"4",y1:"18",x2:"20",y2:"18"})]}),e.jsx("span",{style:{marginLeft:"4px"},children:"+"})]})]})},ce=({compact:r=!1})=>{const{autoLayout:o,setAutoLayout:n}=G(),c=()=>{n(!o)};return e.jsx(R.ControlButton,{onClick:c,className:o?"selected":"",title:o?"Disable automatic layout":"Enable automatic layout",style:{color:o?"green":"inherit"},children:r?e.jsxs("svg",{viewBox:"0 0 24 24",width:"16",height:"16",stroke:"currentColor",strokeWidth:"1.5",fill:"none",children:[e.jsx("rect",{x:"4",y:"4",width:"4",height:"4",rx:"1"}),e.jsx("rect",{x:"4",y:"16",width:"4",height:"4",rx:"1"}),e.jsx("rect",{x:"16",y:"10",width:"4",height:"4",rx:"1"}),e.jsxs(e.Fragment,{children:[e.jsx("path",{d:"M8 6c6 0 3 8 8 6"}),e.jsx("path",{d:"M8 18c3-3 2-8 6-6"})]})]}):e.jsxs(e.Fragment,{children:["Auto-Layout ",o?"ON":"OFF"]})})},be=({compact:r=!1})=>{const{layoutInProgress:o,setLayoutInProgress:n,applyLayout:c}=G(),s=async()=>{if(!o)try{n(!0),await c()}catch(a){console.error("Error applying layout:",a)}};return e.jsxs(R.ControlButton,{onClick:s,title:"Apply layout",disabled:o,style:{color:o?"#999":"#000"},children:[e.jsxs("svg",{viewBox:"0 0 24 24",width:"16",height:"16",stroke:"currentColor",strokeWidth:"1.5",fill:"none",children:[e.jsx("rect",{x:"3",y:"3",width:"5",height:"5",rx:"1"}),e.jsx("rect",{x:"16",y:"3",width:"5",height:"5",rx:"1"}),e.jsx("rect",{x:"3",y:"16",width:"5",height:"5",rx:"1"}),e.jsx("rect",{x:"16",y:"16",width:"5",height:"5",rx:"1"}),e.jsx("path",{d:"M8 5.5h8M5.5 8v8M18.5 8v8M8 18.5h8"}),e.jsx("circle",{cx:"12",cy:"12",r:"3"}),e.jsx("path",{d:"M11 10.5v3l2-1.5z",fill:"currentColor"})]}),!r&&e.jsx("span",{style:{marginLeft:"4px"},children:"Apply Layout"})]})},Ye=({showDirectionControls:r=!0,showSpacingControls:o=!0,showAutoLayoutToggle:n=!0,showApplyLayoutButton:c=!0,standalone:s=!1,position:a="top-right"})=>{const d=G(),{autoLayout:l}=d;return s?e.jsxs(R.Controls,{position:a,showZoom:!1,showFitView:!1,showInteractive:!1,children:[c&&!l&&e.jsx(be,{compact:!0}),r&&e.jsx(ie,{compact:!0}),o&&e.jsx(ae,{compact:!0}),n&&e.jsx(ce,{compact:!0})]}):e.jsxs(e.Fragment,{children:[c&&!l&&e.jsx(be,{compact:!0}),r&&e.jsx(ie,{compact:!0}),o&&e.jsx(ae,{compact:!0}),n&&e.jsx(ce,{compact:!0})]})};P.AutoLayoutToggle=ce,P.DagreEngine=xe,P.DirectionControls=ie,P.LayoutControls=Ye,P.LayoutProvider=Ge,P.SpacingControls=ae,P.engines=ye,P.useLayoutContext=G,Object.defineProperty(P,Symbol.toStringTag,{value:"Module"})});