import{g as De}from"./_commonjsHelpers-Cpj98o6Y.js";import{h as Qe}from"./Paired-C4wB-ooD.js";function Re(t){We(t);const e=Ve(t);return t.on=e.on,t.off=e.off,t.fire=e.fire,t}function Ve(t){let e=Object.create(null);return{on:function(n,r,u){if(typeof r!="function")throw new Error("callback is expected to be a function");let d=e[n];return d||(d=e[n]=[]),d.push({callback:r,ctx:u}),t},off:function(n,r){if(typeof n>"u")return e=Object.create(null),t;if(e[n])if(typeof r!="function")delete e[n];else{const v=e[n];for(let p=0;p1&&(u=Array.prototype.slice.call(arguments,1));for(let d=0;d0&&(B.fire("changed",v),v.length=0)}function le(s){if(typeof s!="function")throw new Error("Function is expected to iterate over graph nodes. You passed "+s);for(var c=e.values(),g=c.next();!g.done;){if(s(g.value))return!0;g=c.next()}}}function Ue(t,e){this.id=t,this.links=null,this.data=e}function pe(t,e){t.links?t.links.add(e):t.links=new Set([e])}function he(t,e,n,r){this.fromId=t,this.toId=e,this.data=n,this.id=r}function Z(t,e){return t.toString()+"👉 "+e.toString()}var de={exports:{}},Y={exports:{}},xe=function(e){return e===0?"x":e===1?"y":e===2?"z":"c"+(e+1)};const Ge=xe;var G=function(e){return n;function n(r,u){let d=u&&u.indent||0,v=u&&u.join!==void 0?u.join:` `,p=Array(d+1).join(" "),i=[];for(let l=0;l {var}max) {var}max = pos.{var};",{indent:6})} } // Makes the bounds square. var maxSideLength = -Infinity; ${e("if ({var}max - {var}min > maxSideLength) maxSideLength = {var}max - {var}min ;",{indent:4})} currentInCache = 0; root = newNode(); ${e("root.min_{var} = {var}min;",{indent:4})} ${e("root.max_{var} = {var}min + maxSideLength;",{indent:4})} i = bodies.length - 1; if (i >= 0) { root.body = bodies[i]; } while (i--) { insert(bodies[i], root); } } function insert(newBody) { insertStack.reset(); insertStack.push(root, newBody); while (!insertStack.isEmpty()) { var stackItem = insertStack.pop(); var node = stackItem.node; var body = stackItem.body; if (!node.body) { // This is internal node. Update the total mass of the node and center-of-mass. ${e("var {var} = body.pos.{var};",{indent:8})} node.mass += body.mass; ${e("node.mass_{var} += body.mass * {var};",{indent:8})} // Recursively insert the body in the appropriate quadrant. // But first find the appropriate quadrant. var quadIdx = 0; // Assume we are in the 0's quad. ${e("var min_{var} = node.min_{var};",{indent:8})} ${e("var max_{var} = (min_{var} + node.max_{var}) / 2;",{indent:8})} ${u(8)} var child = getChild(node, quadIdx); if (!child) { // The node is internal but this quadrant is not taken. Add // subnode to it. child = newNode(); ${e("child.min_{var} = min_{var};",{indent:10})} ${e("child.max_{var} = max_{var};",{indent:10})} child.body = body; setChild(node, quadIdx, child); } else { // continue searching in this quadrant. insertStack.push(child, body); } } else { // We are trying to add to the leaf node. // We have to convert current leaf into internal node // and continue adding two nodes. var oldBody = node.body; node.body = null; // internal nodes do not cary bodies if (isSamePosition(oldBody.pos, body.pos)) { // Prevent infinite subdivision by bumping one node // anywhere in this quadrant var retriesCount = 3; do { var offset = random.nextDouble(); ${e("var d{var} = (node.max_{var} - node.min_{var}) * offset;",{indent:12})} ${e("oldBody.pos.{var} = node.min_{var} + d{var};",{indent:12})} retriesCount -= 1; // Make sure we don't bump it out of the box. If we do, next iteration should fix it } while (retriesCount > 0 && isSamePosition(oldBody.pos, body.pos)); if (retriesCount === 0 && isSamePosition(oldBody.pos, body.pos)) { // This is very bad, we ran out of precision. // if we do not return from the method we'll get into // infinite loop here. So we sacrifice correctness of layout, and keep the app running // Next layout iteration should get larger bounding box in the first step and fix this return; } } // Next iteration should subdivide node further. insertStack.push(node, oldBody); insertStack.push(node, body); } } } } return createQuadTree; `;function u(p){let i=[],l=Array(p+1).join(" ");for(let h=0;h max_${T(h)}) {`),i.push(l+` quadIdx = quadIdx + ${Math.pow(2,h)};`),i.push(l+` min_${T(h)} = max_${T(h)};`),i.push(l+` max_${T(h)} = node.max_${T(h)};`),i.push(l+"}");return i.join(` `)}function d(){let p=Array(11).join(" "),i=[];for(let l=0;l 0) { return this.stack[--this.popIdx]; } }, reset: function () { this.popIdx = 0; } }; function InsertStackElement(node, body) { this.node = node; // QuadTree node this.body = body; // physical body which needs to be inserted to node } `}var Ke=O.exports,se={exports:{}};se.exports=Ye;se.exports.generateFunctionBody=Pe;const Xe=G;function Ye(t){let e=Pe(t);return new Function("bodies","settings","random",e)}function Pe(t){let e=Xe(t);return` var boundingBox = { ${e("min_{var}: 0, max_{var}: 0,",{indent:4})} }; return { box: boundingBox, update: updateBoundingBox, reset: resetBoundingBox, getBestNewPosition: function (neighbors) { var ${e("base_{var} = 0",{join:", "})}; if (neighbors.length) { for (var i = 0; i < neighbors.length; ++i) { let neighborPos = neighbors[i].pos; ${e("base_{var} += neighborPos.{var};",{indent:10})} } ${e("base_{var} /= neighbors.length;",{indent:8})} } else { ${e("base_{var} = (boundingBox.min_{var} + boundingBox.max_{var}) / 2;",{indent:8})} } var springLength = settings.springLength; return { ${e("{var}: base_{var} + (random.nextDouble() - 0.5) * springLength,",{indent:8})} }; } }; function updateBoundingBox() { var i = bodies.length; if (i === 0) return; // No bodies - no borders. ${e("var max_{var} = -Infinity;",{indent:4})} ${e("var min_{var} = Infinity;",{indent:4})} while(i--) { // this is O(n), it could be done faster with quadtree, if we check the root node bounds var bodyPos = bodies[i].pos; ${e("if (bodyPos.{var} < min_{var}) min_{var} = bodyPos.{var};",{indent:6})} ${e("if (bodyPos.{var} > max_{var}) max_{var} = bodyPos.{var};",{indent:6})} } ${e("boundingBox.min_{var} = min_{var};",{indent:4})} ${e("boundingBox.max_{var} = max_{var};",{indent:4})} } function resetBoundingBox() { ${e("boundingBox.min_{var} = boundingBox.max_{var} = 0;",{indent:4})} } `}var Ze=se.exports,fe={exports:{}};const et=G;fe.exports=tt;fe.exports.generateCreateDragForceFunctionBody=Ne;function tt(t){let e=Ne(t);return new Function("options",e)}function Ne(t){return` if (!Number.isFinite(options.dragCoefficient)) throw new Error('dragCoefficient is not a finite number'); return { update: function(body) { ${et(t)("body.force.{var} -= options.dragCoefficient * body.velocity.{var};",{indent:6})} } }; `}var nt=fe.exports,ce={exports:{}};const rt=G;ce.exports=ot;ce.exports.generateCreateSpringForceFunctionBody=qe;function ot(t){let e=qe(t);return new Function("options","random",e)}function qe(t){let e=rt(t);return` if (!Number.isFinite(options.springCoefficient)) throw new Error('Spring coefficient is not a number'); if (!Number.isFinite(options.springLength)) throw new Error('Spring length is not a number'); return { /** * Updates forces acting on a spring */ update: function (spring) { var body1 = spring.from; var body2 = spring.to; var length = spring.length < 0 ? options.springLength : spring.length; ${e("var d{var} = body2.pos.{var} - body1.pos.{var};",{indent:6})} var r = Math.sqrt(${e("d{var} * d{var}",{join:" + "})}); if (r === 0) { ${e("d{var} = (random.nextDouble() - 0.5) / 50;",{indent:8})} r = Math.sqrt(${e("d{var} * d{var}",{join:" + "})}); } var d = r - length; var coefficient = ((spring.coefficient > 0) ? spring.coefficient : options.springCoefficient) * d / r; ${e("body1.force.{var} += coefficient * d{var}",{indent:6})}; body1.springCount += 1; body1.springLength += r; ${e("body2.force.{var} -= coefficient * d{var}",{indent:6})}; body2.springCount += 1; body2.springLength += r; } }; `}var it=ce.exports,ve={exports:{}};const at=G;ve.exports=dt;ve.exports.generateIntegratorFunctionBody=Le;function dt(t){let e=Le(t);return new Function("bodies","timeStep","adaptiveTimeStepWeight",e)}function Le(t){let e=at(t);return` var length = bodies.length; if (length === 0) return 0; ${e("var d{var} = 0, t{var} = 0;",{indent:2})} for (var i = 0; i < length; ++i) { var body = bodies[i]; if (body.isPinned) continue; if (adaptiveTimeStepWeight && body.springCount) { timeStep = (adaptiveTimeStepWeight * body.springLength/body.springCount); } var coeff = timeStep / body.mass; ${e("body.velocity.{var} += coeff * body.force.{var};",{indent:4})} ${e("var v{var} = body.velocity.{var};",{indent:4})} var v = Math.sqrt(${e("v{var} * v{var}",{join:" + "})}); if (v > 1) { // We normalize it so that we move within timeStep range. // for the case when v <= 1 - we let velocity to fade out. ${e("body.velocity.{var} = v{var} / v;",{indent:6})} } ${e("d{var} = timeStep * body.velocity.{var};",{indent:4})} ${e("body.pos.{var} += d{var};",{indent:4})} ${e("t{var} += Math.abs(d{var});",{indent:4})} } return (${e("t{var} * t{var}",{join:" + "})})/length; `}var ut=ve.exports,te,ge;function st(){if(ge)return te;ge=1,te=t;function t(e,n,r,u){this.from=e,this.to=n,this.length=r,this.coefficient=u}return te}var ne,ye;function ft(){if(ye)return ne;ye=1,ne=t;function t(e,n){var r;if(e||(e={}),n){for(r in n)if(n.hasOwnProperty(r)){var u=e.hasOwnProperty(r),d=typeof n[r],v=!u||typeof e[r]!==d;v?e[r]=n[r]:d==="object"&&(e[r]=t(e[r],n[r]))}}return e}return ne}function ct(t){lt(t);const e=vt(t);return t.on=e.on,t.off=e.off,t.fire=e.fire,t}function vt(t){let e=Object.create(null);return{on:function(n,r,u){if(typeof r!="function")throw new Error("callback is expected to be a function");let d=e[n];return d||(d=e[n]=[]),d.push({callback:r,ctx:u}),t},off:function(n,r){if(typeof n>"u")return e=Object.create(null),t;if(e[n])if(typeof r!="function")delete e[n];else{const u=e[n];for(let d=0;d1&&(u=Array.prototype.slice.call(arguments,1));for(let d=0;d=1||i===0);return l*Math.sqrt(-2*Math.log(i)/i)}e.prototype.levy=r;function r(){var i=1.5,l=Math.pow(u(1+i)*Math.sin(Math.PI*i/2)/(u((1+i)/2)*i*Math.pow(2,(i-1)/2)),1/i);return this.gaussian()*l/Math.pow(Math.abs(this.gaussian()),1/i)}function u(i){return Math.sqrt(2*Math.PI/i)*Math.pow(1/Math.E*(i+1/(12*i-1/(10*i))),i)}function d(){var i=this.seed;return i=i+2127912214+(i<<12)&4294967295,i=(i^3345072700^i>>>19)&4294967295,i=i+374761393+(i<<5)&4294967295,i=(i+3550635116^i<<9)&4294967295,i=i+4251993797+(i<<3)&4294967295,i=(i^3042594569^i>>>16)&4294967295,this.seed=i,(i&268435455)/268435456}function v(i){return Math.floor(this.nextDouble()*i)}function p(i,l){var h=l||t();if(typeof h.next!="function")throw new Error("customRandom does not match expected API: next() function is missing");return{forEach:L,shuffle:B};function B(){var $,y,w;for($=i.length-1;$>0;--$)y=h.next($+1),w=i[y],i[y]=i[$],i[$]=w;return i}function L($){var y,w,m;for(y=i.length-1;y>0;--y)w=h.next(y+1),m=i[w],i[w]=i[y],i[y]=m,$(m);i.length&&$(i[0])}}return K.exports}var je=wt,ht=He,gt=Ke,yt=Ze,bt=nt,mt=it,xt=ut,me={};function wt(t){var e=st(),n=ft(),r=Ie;if(t){if(t.springCoeff!==void 0)throw new Error("springCoeff was renamed to springCoefficient");if(t.dragCoeff!==void 0)throw new Error("dragCoeff was renamed to dragCoefficient")}t=n(t,{springLength:10,springCoefficient:.8,gravity:-12,theta:.8,dragCoefficient:.9,timeStep:.5,adaptiveTimeStepWeight:0,dimensions:2,debug:!1});var u=me[t.dimensions];if(!u){var d=t.dimensions;u={Body:ht(d,t.debug),createQuadTree:gt(d),createBounds:yt(d),createDragForce:bt(d),createSpringForce:mt(d),integrate:xt(d)},me[d]=u}var v=u.Body,p=u.createQuadTree,i=u.createBounds,l=u.createDragForce,h=u.createSpringForce,B=u.integrate,L=o=>new v(o),$=pt().random(42),y=[],w=[],m=p(t,$),q=i(y,t,$),Q=h(t,$),z=l(t),A=0,E=[],k=new Map,R=0;M("nbody",J),M("spring",a);var D={bodies:y,quadTree:m,springs:w,settings:t,addForce:M,removeForce:I,getForces:H,step:function(){for(var o=0;o=0?P:-1);return w.push(j),j},getTotalMovement:function(){return A},removeSpring:function(o){if(o){var f=w.indexOf(o);if(f>-1)return w.splice(f,1),!0}},getBestNewBodyPosition:function(o){return q.getBestNewPosition(o)},getBBox:F,getBoundingBox:F,invalidateBBox:function(){console.warn("invalidateBBox() is deprecated, bounds always recomputed on `getBBox()` call")},gravity:function(o){return o!==void 0?(t.gravity=o,m.options({gravity:o}),this):t.gravity},theta:function(o){return o!==void 0?(t.theta=o,m.options({theta:o}),this):t.theta},random:$};return Bt(t,D),r(D),D;function F(){return q.update(),q.box}function M(o,f){if(k.has(o))throw new Error("Force "+o+" is already added");k.set(o,f),E.push(f)}function I(o){var f=E.indexOf(k.get(o));f<0||(E.splice(f,1),k.delete(o))}function H(){return k}function J(){if(y.length!==0){m.insertBodies(y);for(var o=y.length;o--;){var f=y[o];f.isPinned||(f.reset(),m.updateBodyForce(f),z.update(f))}}}function a(){for(var o=w.length;o--;)Q.update(w[o])}}function Bt(t,e){for(var n in t)$t(t,e,n)}function $t(t,e,n){if(t.hasOwnProperty(n)&&typeof e[n]!="function"){var r=Number.isFinite(t[n]);r?e[n]=function(u){if(u!==void 0){if(!Number.isFinite(u))throw new Error("Value of "+n+" should be a valid number.");return t[n]=u,e}return t[n]}:e[n]=function(u){return u!==void 0?(t[n]=u,e):t[n]}}}de.exports=_t;de.exports.simulator=je;var Ct=Ie;function _t(t,e){if(!t)throw new Error("Graph structure cannot be undefined");var n=e&&e.createSimulator||je,r=n(e);if(Array.isArray(e))throw new Error("Physics settings is expected to be an object");var u=t.version>19?J:H;e&&typeof e.nodeMass=="function"&&(u=e.nodeMass);var d=new Map,v={},p=0,i=r.settings.springTransform||St;z(),m();var l=!1,h={step:function(){if(p===0)return B(!0),!0;var a=r.step();h.lastMove=a,h.fire("step");var o=a/p,f=o<=.01;return B(f),f},getNodePosition:function(a){return I(a).pos},setNodePosition:function(a){var o=I(a);o.setPosition.apply(o,Array.prototype.slice.call(arguments,1))},getLinkPosition:function(a){var o=v[a];if(o)return{from:o.from.pos,to:o.to.pos}},getGraphRect:function(){return r.getBBox()},forEachBody:L,pinNode:function(a,o){var f=I(a.id);f.isPinned=!!o},isNodePinned:function(a){return I(a.id).isPinned},dispose:function(){t.off("changed",Q),h.fire("disposed")},getBody:w,getSpring:y,getForceVectorLength:$,simulator:r,graph:t,lastMove:0};return Ct(h),h;function B(a){l!==a&&(l=a,q(a))}function L(a){d.forEach(a)}function $(){var a=0,o=0;return L(function(f){a+=Math.abs(f.force.x),o+=Math.abs(f.force.y)}),Math.sqrt(a*a+o*o)}function y(a,o){var f;if(o===void 0)typeof a!="object"?f=a:f=a.id;else{var b=t.hasLink(a,o);if(!b)return;f=b.id}return v[f]}function w(a){return d.get(a)}function m(){t.on("changed",Q)}function q(a){h.fire("stable",a)}function Q(a){for(var o=0;ot.length)&&(e=t.length);for(var n=0,r=Array(e);n