var an=Object.defineProperty;var ln=(e,t,n)=>t in e?an(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var E=(e,t,n)=>ln(e,typeof t!="symbol"?t+"":t,n);import{j as u,u as ge,H as ue,P as q,N as dn,d as lt,R as cn,i as un,B as hn,C as fn,e as Y}from"./reactflow-vendor-BDofclgK.js";import{r as m,g as pn}from"./react-vendor-4ldeB94U.js";import{e as mn,h as S,b as $,d as D,i as j,f as he,j as U,c as dt,k as gn,l as bn,m as wn,n as vn,P as ct,o as De,p as xn,q as En,r as yn,t as Ae}from"./index-D-ULYqgV.js";import{c as A,a as C,F as O,B as J,I as kn,S as ut}from"./Stack-2Xb3rVeY.js";import{D as Nn,a as _n,b as jn}from"./dialog-BbYNcTN7.js";/**
 * @license lucide-react v0.446.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Cn=mn("Check",[["path",{d:"M20 6 9 17l-5-5",key:"1gmf2c"}]]);/**
 * @license @tabler/icons-react v3.18.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var On=A("outline","arrow-back-up","IconArrowBackUp",[["path",{d:"M9 14l-4 -4l4 -4",key:"svg-0"}],["path",{d:"M5 10h11a4 4 0 1 1 0 8h-1",key:"svg-1"}]]);/**
 * @license @tabler/icons-react v3.18.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var In=A("outline","arrow-forward-up","IconArrowForwardUp",[["path",{d:"M15 14l4 -4l-4 -4",key:"svg-0"}],["path",{d:"M19 10h-11a4 4 0 1 0 0 8h1",key:"svg-1"}]]);/**
 * @license @tabler/icons-react v3.18.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var Sn=A("outline","arrows-diagonal-2","IconArrowsDiagonal2",[["path",{d:"M16 20l4 0l0 -4",key:"svg-0"}],["path",{d:"M14 14l6 6",key:"svg-1"}],["path",{d:"M8 4l-4 0l0 4",key:"svg-2"}],["path",{d:"M4 4l6 6",key:"svg-3"}]]);/**
 * @license @tabler/icons-react v3.18.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var Ge=A("outline","arrows-split-2","IconArrowsSplit2",[["path",{d:"M21 17h-5.397a5 5 0 0 1 -4.096 -2.133l-.514 -.734a5 5 0 0 0 -4.096 -2.133h-3.897",key:"svg-0"}],["path",{d:"M21 7h-5.395a5 5 0 0 0 -4.098 2.135l-.51 .73a5 5 0 0 1 -4.097 2.135h-3.9",key:"svg-1"}],["path",{d:"M18 10l3 -3l-3 -3",key:"svg-2"}],["path",{d:"M18 20l3 -3l-3 -3",key:"svg-3"}]]);/**
 * @license @tabler/icons-react v3.18.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var Rn=A("outline","circle-check","IconCircleCheck",[["path",{d:"M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0",key:"svg-0"}],["path",{d:"M9 12l2 2l4 -4",key:"svg-1"}]]);/**
 * @license @tabler/icons-react v3.18.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var Ln=A("outline","x","IconX",[["path",{d:"M18 6l-12 12",key:"svg-0"}],["path",{d:"M6 6l12 12",key:"svg-1"}]]);/**
 * @license @tabler/icons-react v3.18.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var Tn=A("outline","xbox-x","IconXboxX",[["path",{d:"M12 21a9 9 0 0 0 9 -9a9 9 0 0 0 -9 -9a9 9 0 0 0 -9 9a9 9 0 0 0 9 9z",key:"svg-0"}],["path",{d:"M9 8l6 8",key:"svg-1"}],["path",{d:"M15 8l-6 8",key:"svg-2"}]]);const ht=m.forwardRef(({className:e,...t},n)=>u.jsx("textarea",{className:S("flex min-h-[20px] w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",e),ref:n,...t}));ht.displayName="Textarea";const be=m.forwardRef(({className:e,type:t,...n},r)=>u.jsx("input",{type:t,className:S("flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",e),ref:r,...n}));be.displayName="Input";function Mn({input:e,nodeID:t}){const{updateValues:n,values:r}=$(D(s=>({values:s.values,updateValues:s.updateValues})));return u.jsx(be,{type:"number",value:r[j("input",t,e.id)]??"",onChange:s=>{n({[j("input",t,e.id)]:s.target.value===""?void 0:Number(s.target.value)})},className:"w-full border-zinc-700 [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none"})}function Fn({extensions:e,defaultPath:t="",onSelect:n}){const[r,s]=m.useState([]),[o,i]=m.useState("."),[a,l]=m.useState(t);m.useEffect(()=>{he("/get_os_sep",{method:"GET"}).then(h=>h.text()).then(h=>{i(h)}),d(a)},[]);const d=h=>{he("/file_picker/list_files",{method:"POST",body:JSON.stringify({path:h,extensions:e})}).then(f=>f.json()).then(f=>{l(f.path),s(f.items)})},c=h=>{const f=a.split(o).slice(0,h+1).join(o);d(f)};return u.jsxs(C,{children:[u.jsxs(O,{className:"gap-4 mb-3",children:[u.jsx("p",{className:"text-xl font-semibold",children:"Choose File"}),u.jsx("p",{className:"text-sm text-muted-foreground",children:e==null?void 0:e.join(", ")})]}),u.jsx(O,{children:a.split(o).map((h,f)=>u.jsxs(O,{className:"mb-2",children:[u.jsx(J,{variant:"link",className:"px-0 text-lg",onClick:()=>c(f),children:h}),u.jsx("span",{className:"px-1",children:f<a.split(o).length-1&&o})]},f))}),u.jsx(C,{className:"overflow-y-auto h-[70vh]",children:r.map(h=>h.type==="file"?u.jsx(Pn,{file:h,onClick:()=>n(h)},h.name):u.jsxs(O,{className:"cursor-pointer gap-1 hover:bg-secondary py-[1px]",onClick:()=>d(h.path),children:[u.jsx(kn,{size:16}),u.jsx("span",{children:h.name})]},h.name))})]})}const $n=["jpg","jpeg","png","gif","bmp","tiff","webp"];function Pn({file:e,onClick:t}){const n=$n.includes(e.name.split(".").pop()||"");return u.jsxs(O,{className:"gap-1 cursor-pointer hover:bg-secondary",onClick:t,children:[n&&u.jsx("img",{src:U+`/preview_image?path=${encodeURIComponent(e.path)}`,alt:e.name,className:"w-10 h-10 object-contain",style:{width:"50px",height:"50px",objectFit:"contain"}}),u.jsx("span",{children:e.name})]})}function Dn({onClose:e,extensions:t,defaultPath:n="",onSelect:r}){return u.jsx(Nn,{onOpenChange:e,open:!0,children:u.jsx(_n,{className:"sm:max-w-[50vw]","aria-describedby":"Choose File",children:u.jsx(jn,{children:u.jsx(Fn,{extensions:t,defaultPath:n,onSelect:r})})})})}const An=["png","jpg","jpeg","gif","bmp","tiff","webp"];function Gn(e){return An.includes(e.split(".").pop()??"")}function Vn(e){return/[\/\\]([^\/\\]+\.(png|jpg|jpeg|gif|bmp|tiff|webp))$/i.test(e)}function zn({input:e,extensions:t,nodeID:n}){const{updateValues:r,values:s}=$(D(d=>({values:d.values,updateValues:d.updateValues})));ge();const[o,i]=m.useState(!1),a=s[j("input",n,e.id)],l=a==null?void 0:a.split(dt.osSep).pop();return u.jsxs(C,{className:"px-1 w-full",children:[u.jsx(J,{onClick:()=>i(!0),className:"mb-2",children:"Choose file"}),a&&u.jsxs(C,{children:[Gn(a)&&u.jsx("img",{src:U+`/preview_image?path=${encodeURIComponent(a)}`,alt:l,className:"object-contain max-w-[360px] max-h-[360px]",style:{objectFit:"contain"}}),u.jsxs(O,{className:"gap-2",children:[u.jsx("p",{className:"text-sm break-all",children:l}),u.jsx(J,{variant:"secondary",className:"p-[1px] w-5 h-5",onClick:()=>{r({[j("input",n,e.id)]:void 0})},children:u.jsx(Ln,{size:15})})]})]}),o&&u.jsx(Dn,{extensions:t,onClose:()=>i(!1),onSelect:d=>{i(!1),r({[j("input",n,e.id)]:d.rel_path})}})]})}function Hn(e){const t=m.useRef({value:e,previous:e});return m.useMemo(()=>(t.current.value!==e&&(t.current.previous=t.current.value,t.current.value=e),t.current.previous),[e])}function Bn(e){const[t,n]=m.useState(void 0);return gn(()=>{if(e){n({width:e.offsetWidth,height:e.offsetHeight});const r=new ResizeObserver(s=>{if(!Array.isArray(s)||!s.length)return;const o=s[0];let i,a;if("borderBoxSize"in o){const l=o.borderBoxSize,d=Array.isArray(l)?l[0]:l;i=d.inlineSize,a=d.blockSize}else i=e.offsetWidth,a=e.offsetHeight;n({width:i,height:a})});return r.observe(e,{box:"border-box"}),()=>r.unobserve(e)}else n(void 0)},[e]),t}var we="Checkbox",[Wn,ma]=bn(we),[Un,Yn]=Wn(we),ft=m.forwardRef((e,t)=>{const{__scopeCheckbox:n,name:r,checked:s,defaultChecked:o,required:i,disabled:a,value:l="on",onCheckedChange:d,form:c,...h}=e,[f,p]=m.useState(null),b=wn(t,N=>p(N)),w=m.useRef(!1),_=f?c||!!f.closest("form"):!0,[k=!1,T]=vn({prop:s,defaultProp:o,onChange:d}),V=m.useRef(k);return m.useEffect(()=>{const N=f==null?void 0:f.form;if(N){const L=()=>T(V.current);return N.addEventListener("reset",L),()=>N.removeEventListener("reset",L)}},[f,T]),u.jsxs(Un,{scope:n,state:k,disabled:a,children:[u.jsx(ct.button,{type:"button",role:"checkbox","aria-checked":F(k)?"mixed":k,"aria-required":i,"data-state":gt(k),"data-disabled":a?"":void 0,disabled:a,value:l,...h,ref:b,onKeyDown:De(e.onKeyDown,N=>{N.key==="Enter"&&N.preventDefault()}),onClick:De(e.onClick,N=>{T(L=>F(L)?!0:!L),_&&(w.current=N.isPropagationStopped(),w.current||N.stopPropagation())})}),_&&u.jsx(Xn,{control:f,bubbles:!w.current,name:r,value:l,checked:k,required:i,disabled:a,form:c,style:{transform:"translateX(-100%)"},defaultChecked:F(o)?!1:o})]})});ft.displayName=we;var pt="CheckboxIndicator",mt=m.forwardRef((e,t)=>{const{__scopeCheckbox:n,forceMount:r,...s}=e,o=Yn(pt,n);return u.jsx(xn,{present:r||F(o.state)||o.state===!0,children:u.jsx(ct.span,{"data-state":gt(o.state),"data-disabled":o.disabled?"":void 0,...s,ref:t,style:{pointerEvents:"none",...e.style}})})});mt.displayName=pt;var Xn=e=>{const{control:t,checked:n,bubbles:r=!0,defaultChecked:s,...o}=e,i=m.useRef(null),a=Hn(n),l=Bn(t);m.useEffect(()=>{const c=i.current,h=window.HTMLInputElement.prototype,p=Object.getOwnPropertyDescriptor(h,"checked").set;if(a!==n&&p){const b=new Event("click",{bubbles:r});c.indeterminate=F(n),p.call(c,F(n)?!1:n),c.dispatchEvent(b)}},[a,n,r]);const d=m.useRef(F(n)?!1:n);return u.jsx("input",{type:"checkbox","aria-hidden":!0,defaultChecked:s??d.current,...o,tabIndex:-1,ref:i,style:{...e.style,...l,position:"absolute",pointerEvents:"none",opacity:0,margin:0}})};function F(e){return e==="indeterminate"}function gt(e){return F(e)?"indeterminate":e?"checked":"unchecked"}var bt=ft,qn=mt;const wt=m.forwardRef(({className:e,...t},n)=>u.jsx(bt,{ref:n,className:S("peer h-4 w-4 shrink-0 rounded-sm border border-primary ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground",e),...t,children:u.jsx(qn,{className:S("flex items-center justify-center text-current"),children:u.jsx(Cn,{className:"h-4 w-4"})})}));wt.displayName=bt.displayName;const Ve="8",fe="000";function vt(e){return u.jsx(ue,{style:{width:`${Ve}px`,height:`${Ve}px`},...e})}function Kn({input:e,nodeID:t,isConnected:n}){const r=e.type==="int"||e.type==="float",s=e.widget||e.type==="filepath";return u.jsx(C,{className:`flex w-full ${r?"h-10":""}`,children:u.jsxs(O,{className:"gap-2 w-full",children:[!e.hide_handle&&u.jsxs("div",{className:"relative flex items-center gap-1",children:[u.jsx(vt,{type:"target",position:q.Left,id:e.id}),u.jsx("span",{className:"text-sm font-semibold ml-2",children:e.name}),typeof e.type=="string"&&u.jsx("span",{className:"text-[10px] text-zinc-500 italic",children:e.type})]}),s?u.jsx(Jn,{input:e,nodeID:t}):!n&&u.jsx(Qn,{input:e,nodeID:t})]})},"input"+e.id)}function Jn({input:e,nodeID:t}){var n,r,s;if(((n=e.widget)==null?void 0:n.type)==="server_file_picker"||e.type==="filepath")return u.jsx(zn,{input:e,extensions:((s=(r=e.widget)==null?void 0:r.options)==null?void 0:s.extensions)??e.accept,nodeID:t})}function Qn({input:e,nodeID:t}){const{updateValues:n,values:r}=$(D(i=>({values:i.values,updateValues:i.updateValues}))),s=m.useRef(null),o=ge();if(m.useEffect(()=>{if(e.type!=="str")return;const i=s.current;if(!i)return;const a=()=>{i.style.height="auto",i.style.height=`${i.scrollHeight}px`,o(t)};return a(),i.addEventListener("input",a),()=>{i.removeEventListener("input",a)}},[e.type,t,o,r[j("input",t,e.id)]]),e.type==="int"||e.type==="float")return u.jsx(Mn,{input:e,nodeID:t});if(e.type==="bool")return u.jsx(wt,{className:"mx-4",checked:r[j("input",t,e.id)]??!1,onCheckedChange:i=>{console.log("checked",i),n({[j("input",t,e.id)]:i.valueOf()})}});if(Array.isArray(e.type))return u.jsx("select",{id:"countries",value:r[j("input",t,e.id)]??"",onChange:i=>{n({[j("input",t,e.id)]:i.target.value})},className:"bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg block w-full p-2 dark:bg-zinc-700 dark:border-zinc-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500",children:e.type.map(i=>u.jsx("option",{value:i,children:i},i))});if(e.type==="str")return u.jsx(ht,{ref:s,value:r[j("input",t,e.id)]??"",className:"border-zinc-700 py-1 flex-1 flex-grow nodrag nopan resize-none",style:{height:"auto",overflow:"hidden"},onChange:i=>{n({[j("input",t,e.id)]:i.target.value})}})}function Zn({children:e}){const t=m.useRef(null),[n,r]=m.useState(100);return m.useEffect(()=>{var s;(s=t.current)!=null&&s.offsetHeight&&r(t.current.offsetHeight)},[]),u.jsxs("div",{className:"group h-full",children:[u.jsx(dn,{className:"!w-3 !h-3 !translate-x-[-30%] !translate-y-[-30%] !bg-transparent !border-none group",minWidth:150,minHeight:n,children:u.jsx(Sn,{size:10,className:"hidden group-hover:block absolute bottom-0 right-0"})}),u.jsx("div",{className:"h-full",ref:t,children:e})]})}var K=(e=>(e.RUNNING="RUNNING",e.SUCCESS="SUCCESS",e.FAIL="FAIL",e))(K||{});function er({output:e,nodeID:t,status:n}){const r=n==null?void 0:n.results;return r?u.jsxs(C,{className:"text-left m-2 gap-[1px]",children:[u.jsxs(O,{className:"gap-1 items-center mb-1",children:[u.jsx("p",{className:"text-[10px] text-muted-foreground ",children:"Outputs"}),(n==null?void 0:n.status)===K.RUNNING&&u.jsx(ut,{className:"w-4 h-4"}),(n==null?void 0:n.status)===K.SUCCESS&&u.jsx(Rn,{className:"w-4 h-4 text-green-500"}),(n==null?void 0:n.status)===K.FAIL&&u.jsx(Tn,{className:"w-4 h-4 text-red-500"})]}),r==null?void 0:r.map((s,o)=>u.jsx(C,{children:u.jsxs(C,{className:"text-sm p-1 gap-1 rounded-md border border-zinc-600 bg-zinc-700",children:[typeof s=="object"?u.jsx("pre",{className:"whitespace-pre-wrap",children:JSON.stringify(s,null,2)}):u.jsx("pre",{className:"whitespace-pre-wrap break-all",children:s}),typeof s=="string"&&Vn(s)&&u.jsxs(C,{className:"gap-1 justify-center items-center",children:[u.jsx("img",{src:U+`/preview_image?path=${encodeURIComponent(s)}`,alt:"image",className:"object-contain cursor-pointer w-full max-w-[500px] max-h-[500px]",style:{flex:1,objectFit:"contain"},onClick:()=>{window.open(U+`/preview_image?path=${encodeURIComponent(s)}`,"_blank")}}),u.jsx(J,{className:"w-full",onClick:()=>{window.open(U+`/preview_image?path=${encodeURIComponent(s)}&download=1`,"_blank")},children:"Download"})]})]})},o)),(n==null?void 0:n.error)&&u.jsx(C,{children:u.jsx("div",{className:"text-sm p-1 rounded-md border bg-destructive",children:u.jsx("pre",{className:"whitespace-pre-wrap break-all",children:n==null?void 0:n.error})})})]}):null}function tr({data:e,id:t}){var i,a;const{selectedNodeIDs:n,edges:r,status:s}=$(D(l=>{var d,c;return{selectedNodeIDs:l.selectedNodeIDs,edges:l.edges,status:(c=(d=l.job_status)==null?void 0:d.nodes)==null?void 0:c[t]}})),o=ge();return m.useEffect(()=>{o(t)},[s,r,t,o]),u.jsx(Zn,{children:u.jsxs("div",{className:"h-full flex flex-col min-w-[120px]",children:[u.jsxs("div",{className:"h-4 flex justify-end",children:[e.import_error&&u.jsxs("p",{className:"text-[10px] font-bold px-2",children:[u.jsx("span",{className:"text-red-500",children:" ❌ Import Failed: "}),u.jsx("span",{children:e.import_error})]}),u.jsxs("p",{className:"text-[10px] text-muted-foreground italic px-2 w-fit text-right",children:["#",t]})]}),u.jsxs(C,{className:`flex-1 bg-zinc-800 rounded-md ${n.includes(t)?"outline outline-[0.8px] outline-zinc-300":(s==null?void 0:s.status)==="RUNNING"?"outline outline-2 outline-purple-500":""}`,children:[u.jsxs(O,{className:`relative py-1 ${e.import_error?"bg-red-500":"bg-gray-700"} rounded-t-md justify-between`,children:[u.jsx(ue,{type:"target",position:q.Left,id:"000"}),u.jsx(O,{className:"justify-between  flex-grow",children:u.jsx("p",{className:"px-2 font-semibold",children:e.node_title??e.name})}),u.jsx(ue,{type:"source",position:q.Right,id:fe})]}),u.jsxs(O,{className:"flex justify-between py-2 h-full",children:[u.jsx("div",{className:"flex flex-col gap-2 h-full w-full pr-2",children:(i=e.input)==null?void 0:i.map(l=>{const d=r.some(c=>c.target===t&&c.targetHandle===l.id);return u.jsx(Kn,{input:l,nodeID:t,isConnected:d},l.id)})}),u.jsx(C,{className:"flex flex-col justify-center ml-2 gap-2",children:(a=e.output)==null?void 0:a.map(l=>{var d;return u.jsxs("div",{className:"relative flex items-center ml-3",children:[u.jsx("p",{className:"text-[10px] text-zinc-500 italic mr-[8px]",children:(d=l.type)==null?void 0:d.split("[")[0]}),u.jsx(vt,{type:"source",position:q.Right,id:l.id})]},"output"+l.id)})})]}),u.jsx(er,{nodeID:t,output:e.output??null,status:s??null})]})]})})}var nr="\0",P="\0",ze="";let rr=class{constructor(t){E(this,"_isDirected",!0);E(this,"_isMultigraph",!1);E(this,"_isCompound",!1);E(this,"_label");E(this,"_defaultNodeLabelFn",()=>{});E(this,"_defaultEdgeLabelFn",()=>{});E(this,"_nodes",{});E(this,"_in",{});E(this,"_preds",{});E(this,"_out",{});E(this,"_sucs",{});E(this,"_edgeObjs",{});E(this,"_edgeLabels",{});E(this,"_nodeCount",0);E(this,"_edgeCount",0);E(this,"_parent");E(this,"_children");t&&(this._isDirected=Object.hasOwn(t,"directed")?t.directed:!0,this._isMultigraph=Object.hasOwn(t,"multigraph")?t.multigraph:!1,this._isCompound=Object.hasOwn(t,"compound")?t.compound:!1),this._isCompound&&(this._parent={},this._children={},this._children[P]={})}isDirected(){return this._isDirected}isMultigraph(){return this._isMultigraph}isCompound(){return this._isCompound}setGraph(t){return this._label=t,this}graph(){return this._label}setDefaultNodeLabel(t){return this._defaultNodeLabelFn=t,typeof t!="function"&&(this._defaultNodeLabelFn=()=>t),this}nodeCount(){return this._nodeCount}nodes(){return Object.keys(this._nodes)}sources(){var t=this;return this.nodes().filter(n=>Object.keys(t._in[n]).length===0)}sinks(){var t=this;return this.nodes().filter(n=>Object.keys(t._out[n]).length===0)}setNodes(t,n){var r=arguments,s=this;return t.forEach(function(o){r.length>1?s.setNode(o,n):s.setNode(o)}),this}setNode(t,n){return Object.hasOwn(this._nodes,t)?(arguments.length>1&&(this._nodes[t]=n),this):(this._nodes[t]=arguments.length>1?n:this._defaultNodeLabelFn(t),this._isCompound&&(this._parent[t]=P,this._children[t]={},this._children[P][t]=!0),this._in[t]={},this._preds[t]={},this._out[t]={},this._sucs[t]={},++this._nodeCount,this)}node(t){return this._nodes[t]}hasNode(t){return Object.hasOwn(this._nodes,t)}removeNode(t){var n=this;if(Object.hasOwn(this._nodes,t)){var r=s=>n.removeEdge(n._edgeObjs[s]);delete this._nodes[t],this._isCompound&&(this._removeFromParentsChildList(t),delete this._parent[t],this.children(t).forEach(function(s){n.setParent(s)}),delete this._children[t]),Object.keys(this._in[t]).forEach(r),delete this._in[t],delete this._preds[t],Object.keys(this._out[t]).forEach(r),delete this._out[t],delete this._sucs[t],--this._nodeCount}return this}setParent(t,n){if(!this._isCompound)throw new Error("Cannot set parent in a non-compound graph");if(n===void 0)n=P;else{n+="";for(var r=n;r!==void 0;r=this.parent(r))if(r===t)throw new Error("Setting "+n+" as parent of "+t+" would create a cycle");this.setNode(n)}return this.setNode(t),this._removeFromParentsChildList(t),this._parent[t]=n,this._children[n][t]=!0,this}_removeFromParentsChildList(t){delete this._children[this._parent[t]][t]}parent(t){if(this._isCompound){var n=this._parent[t];if(n!==P)return n}}children(t=P){if(this._isCompound){var n=this._children[t];if(n)return Object.keys(n)}else{if(t===P)return this.nodes();if(this.hasNode(t))return[]}}predecessors(t){var n=this._preds[t];if(n)return Object.keys(n)}successors(t){var n=this._sucs[t];if(n)return Object.keys(n)}neighbors(t){var n=this.predecessors(t);if(n){const s=new Set(n);for(var r of this.successors(t))s.add(r);return Array.from(s.values())}}isLeaf(t){var n;return this.isDirected()?n=this.successors(t):n=this.neighbors(t),n.length===0}filterNodes(t){var n=new this.constructor({directed:this._isDirected,multigraph:this._isMultigraph,compound:this._isCompound});n.setGraph(this.graph());var r=this;Object.entries(this._nodes).forEach(function([i,a]){t(i)&&n.setNode(i,a)}),Object.values(this._edgeObjs).forEach(function(i){n.hasNode(i.v)&&n.hasNode(i.w)&&n.setEdge(i,r.edge(i))});var s={};function o(i){var a=r.parent(i);return a===void 0||n.hasNode(a)?(s[i]=a,a):a in s?s[a]:o(a)}return this._isCompound&&n.nodes().forEach(i=>n.setParent(i,o(i))),n}setDefaultEdgeLabel(t){return this._defaultEdgeLabelFn=t,typeof t!="function"&&(this._defaultEdgeLabelFn=()=>t),this}edgeCount(){return this._edgeCount}edges(){return Object.values(this._edgeObjs)}setPath(t,n){var r=this,s=arguments;return t.reduce(function(o,i){return s.length>1?r.setEdge(o,i,n):r.setEdge(o,i),i}),this}setEdge(){var t,n,r,s,o=!1,i=arguments[0];typeof i=="object"&&i!==null&&"v"in i?(t=i.v,n=i.w,r=i.name,arguments.length===2&&(s=arguments[1],o=!0)):(t=i,n=arguments[1],r=arguments[3],arguments.length>2&&(s=arguments[2],o=!0)),t=""+t,n=""+n,r!==void 0&&(r=""+r);var a=W(this._isDirected,t,n,r);if(Object.hasOwn(this._edgeLabels,a))return o&&(this._edgeLabels[a]=s),this;if(r!==void 0&&!this._isMultigraph)throw new Error("Cannot set a named edge when isMultigraph = false");this.setNode(t),this.setNode(n),this._edgeLabels[a]=o?s:this._defaultEdgeLabelFn(t,n,r);var l=sr(this._isDirected,t,n,r);return t=l.v,n=l.w,Object.freeze(l),this._edgeObjs[a]=l,He(this._preds[n],t),He(this._sucs[t],n),this._in[n][a]=l,this._out[t][a]=l,this._edgeCount++,this}edge(t,n,r){var s=arguments.length===1?oe(this._isDirected,arguments[0]):W(this._isDirected,t,n,r);return this._edgeLabels[s]}edgeAsObj(){const t=this.edge(...arguments);return typeof t!="object"?{label:t}:t}hasEdge(t,n,r){var s=arguments.length===1?oe(this._isDirected,arguments[0]):W(this._isDirected,t,n,r);return Object.hasOwn(this._edgeLabels,s)}removeEdge(t,n,r){var s=arguments.length===1?oe(this._isDirected,arguments[0]):W(this._isDirected,t,n,r),o=this._edgeObjs[s];return o&&(t=o.v,n=o.w,delete this._edgeLabels[s],delete this._edgeObjs[s],Be(this._preds[n],t),Be(this._sucs[t],n),delete this._in[n][s],delete this._out[t][s],this._edgeCount--),this}inEdges(t,n){var r=this._in[t];if(r){var s=Object.values(r);return n?s.filter(o=>o.v===n):s}}outEdges(t,n){var r=this._out[t];if(r){var s=Object.values(r);return n?s.filter(o=>o.w===n):s}}nodeEdges(t,n){var r=this.inEdges(t,n);if(r)return r.concat(this.outEdges(t,n))}};function He(e,t){e[t]?e[t]++:e[t]=1}function Be(e,t){--e[t]||delete e[t]}function W(e,t,n,r){var s=""+t,o=""+n;if(!e&&s>o){var i=s;s=o,o=i}return s+ze+o+ze+(r===void 0?nr:r)}function sr(e,t,n,r){var s=""+t,o=""+n;if(!e&&s>o){var i=s;s=o,o=i}var a={v:s,w:o};return r&&(a.name=r),a}function oe(e,t){return W(e,t.v,t.w,t.name)}var ve=rr,or="2.2.4",ir={Graph:ve,version:or},ar=ve,lr={write:dr,read:hr};function dr(e){var t={options:{directed:e.isDirected(),multigraph:e.isMultigraph(),compound:e.isCompound()},nodes:cr(e),edges:ur(e)};return e.graph()!==void 0&&(t.value=structuredClone(e.graph())),t}function cr(e){return e.nodes().map(function(t){var n=e.node(t),r=e.parent(t),s={v:t};return n!==void 0&&(s.value=n),r!==void 0&&(s.parent=r),s})}function ur(e){return e.edges().map(function(t){var n=e.edge(t),r={v:t.v,w:t.w};return t.name!==void 0&&(r.name=t.name),n!==void 0&&(r.value=n),r})}function hr(e){var t=new ar(e.options).setGraph(e.value);return e.nodes.forEach(function(n){t.setNode(n.v,n.value),n.parent&&t.setParent(n.v,n.parent)}),e.edges.forEach(function(n){t.setEdge({v:n.v,w:n.w,name:n.name},n.value)}),t}var fr=pr;function pr(e){var t={},n=[],r;function s(o){Object.hasOwn(t,o)||(t[o]=!0,r.push(o),e.successors(o).forEach(s),e.predecessors(o).forEach(s))}return e.nodes().forEach(function(o){r=[],s(o),r.length&&n.push(r)}),n}let mr=class{constructor(){E(this,"_arr",[]);E(this,"_keyIndices",{})}size(){return this._arr.length}keys(){return this._arr.map(function(t){return t.key})}has(t){return Object.hasOwn(this._keyIndices,t)}priority(t){var n=this._keyIndices[t];if(n!==void 0)return this._arr[n].priority}min(){if(this.size()===0)throw new Error("Queue underflow");return this._arr[0].key}add(t,n){var r=this._keyIndices;if(t=String(t),!Object.hasOwn(r,t)){var s=this._arr,o=s.length;return r[t]=o,s.push({key:t,priority:n}),this._decrease(o),!0}return!1}removeMin(){this._swap(0,this._arr.length-1);var t=this._arr.pop();return delete this._keyIndices[t.key],this._heapify(0),t.key}decrease(t,n){var r=this._keyIndices[t];if(n>this._arr[r].priority)throw new Error("New priority is greater than current priority. Key: "+t+" Old: "+this._arr[r].priority+" New: "+n);this._arr[r].priority=n,this._decrease(r)}_heapify(t){var n=this._arr,r=2*t,s=r+1,o=t;r<n.length&&(o=n[r].priority<n[o].priority?r:o,s<n.length&&(o=n[s].priority<n[o].priority?s:o),o!==t&&(this._swap(t,o),this._heapify(o)))}_decrease(t){for(var n=this._arr,r=n[t].priority,s;t!==0&&(s=t>>1,!(n[s].priority<r));)this._swap(t,s),t=s}_swap(t,n){var r=this._arr,s=this._keyIndices,o=r[t],i=r[n];r[t]=i,r[n]=o,s[i.key]=t,s[o.key]=n}};var xt=mr,gr=xt,Et=wr,br=()=>1;function wr(e,t,n,r){return vr(e,String(t),n||br,r||function(s){return e.outEdges(s)})}function vr(e,t,n,r){var s={},o=new gr,i,a,l=function(d){var c=d.v!==i?d.v:d.w,h=s[c],f=n(d),p=a.distance+f;if(f<0)throw new Error("dijkstra does not allow negative edge weights. Bad edge: "+d+" Weight: "+f);p<h.distance&&(h.distance=p,h.predecessor=i,o.decrease(c,p))};for(e.nodes().forEach(function(d){var c=d===t?0:Number.POSITIVE_INFINITY;s[d]={distance:c},o.add(d,c)});o.size()>0&&(i=o.removeMin(),a=s[i],a.distance!==Number.POSITIVE_INFINITY);)r(i).forEach(l);return s}var xr=Et,Er=yr;function yr(e,t,n){return e.nodes().reduce(function(r,s){return r[s]=xr(e,s,t,n),r},{})}var yt=kr;function kr(e){var t=0,n=[],r={},s=[];function o(i){var a=r[i]={onStack:!0,lowlink:t,index:t++};if(n.push(i),e.successors(i).forEach(function(c){Object.hasOwn(r,c)?r[c].onStack&&(a.lowlink=Math.min(a.lowlink,r[c].index)):(o(c),a.lowlink=Math.min(a.lowlink,r[c].lowlink))}),a.lowlink===a.index){var l=[],d;do d=n.pop(),r[d].onStack=!1,l.push(d);while(i!==d);s.push(l)}}return e.nodes().forEach(function(i){Object.hasOwn(r,i)||o(i)}),s}var Nr=yt,_r=jr;function jr(e){return Nr(e).filter(function(t){return t.length>1||t.length===1&&e.hasEdge(t[0],t[0])})}var Cr=Ir,Or=()=>1;function Ir(e,t,n){return Sr(e,t||Or,n||function(r){return e.outEdges(r)})}function Sr(e,t,n){var r={},s=e.nodes();return s.forEach(function(o){r[o]={},r[o][o]={distance:0},s.forEach(function(i){o!==i&&(r[o][i]={distance:Number.POSITIVE_INFINITY})}),n(o).forEach(function(i){var a=i.v===o?i.w:i.v,l=t(i);r[o][a]={distance:l,predecessor:o}})}),s.forEach(function(o){var i=r[o];s.forEach(function(a){var l=r[a];s.forEach(function(d){var c=l[o],h=i[d],f=l[d],p=c.distance+h.distance;p<f.distance&&(f.distance=p,f.predecessor=h.predecessor)})})}),r}function kt(e){var t={},n={},r=[];function s(o){if(Object.hasOwn(n,o))throw new pe;Object.hasOwn(t,o)||(n[o]=!0,t[o]=!0,e.predecessors(o).forEach(s),delete n[o],r.push(o))}if(e.sinks().forEach(s),Object.keys(t).length!==e.nodeCount())throw new pe;return r}class pe extends Error{constructor(){super(...arguments)}}var Nt=kt;kt.CycleException=pe;var We=Nt,Rr=Lr;function Lr(e){try{We(e)}catch(t){if(t instanceof We.CycleException)return!1;throw t}return!0}var _t=Tr;function Tr(e,t,n){Array.isArray(t)||(t=[t]);var r=e.isDirected()?a=>e.successors(a):a=>e.neighbors(a),s=n==="post"?Mr:Fr,o=[],i={};return t.forEach(a=>{if(!e.hasNode(a))throw new Error("Graph does not have node: "+a);s(a,r,i,o)}),o}function Mr(e,t,n,r){for(var s=[[e,!1]];s.length>0;){var o=s.pop();o[1]?r.push(o[0]):Object.hasOwn(n,o[0])||(n[o[0]]=!0,s.push([o[0],!0]),jt(t(o[0]),i=>s.push([i,!1])))}}function Fr(e,t,n,r){for(var s=[e];s.length>0;){var o=s.pop();Object.hasOwn(n,o)||(n[o]=!0,r.push(o),jt(t(o),i=>s.push(i)))}}function jt(e,t){for(var n=e.length;n--;)t(e[n],n,e);return e}var $r=_t,Pr=Dr;function Dr(e,t){return $r(e,t,"post")}var Ar=_t,Gr=Vr;function Vr(e,t){return Ar(e,t,"pre")}var zr=ve,Hr=xt,Br=Wr;function Wr(e,t){var n=new zr,r={},s=new Hr,o;function i(l){var d=l.v===o?l.w:l.v,c=s.priority(d);if(c!==void 0){var h=t(l);h<c&&(r[d]=o,s.decrease(d,h))}}if(e.nodeCount()===0)return n;e.nodes().forEach(function(l){s.add(l,Number.POSITIVE_INFINITY),n.setNode(l)}),s.decrease(e.nodes()[0],0);for(var a=!1;s.size()>0;){if(o=s.removeMin(),Object.hasOwn(r,o))n.setEdge(o,r[o]);else{if(a)throw new Error("Input graph is not connected: "+e);a=!0}e.nodeEdges(o).forEach(i)}return n}var Ur={components:fr,dijkstra:Et,dijkstraAll:Er,findCycles:_r,floydWarshall:Cr,isAcyclic:Rr,postorder:Pr,preorder:Gr,prim:Br,tarjan:yt,topsort:Nt},Ue=ir,R={Graph:Ue.Graph,json:lr,alg:Ur,version:Ue.version};let Yr=class{constructor(){let t={};t._next=t._prev=t,this._sentinel=t}dequeue(){let t=this._sentinel,n=t._prev;if(n!==t)return Ye(n),n}enqueue(t){let n=this._sentinel;t._prev&&t._next&&Ye(t),t._next=n._next,n._next._prev=t,n._next=t,t._prev=n}toString(){let t=[],n=this._sentinel,r=n._prev;for(;r!==n;)t.push(JSON.stringify(r,Xr)),r=r._prev;return"["+t.join(", ")+"]"}};function Ye(e){e._prev._next=e._next,e._next._prev=e._prev,delete e._next,delete e._prev}function Xr(e,t){if(e!=="_next"&&e!=="_prev")return t}var qr=Yr;let Kr=R.Graph,Jr=qr;var Qr=es;let Zr=()=>1;function es(e,t){if(e.nodeCount()<=1)return[];let n=ns(e,t||Zr);return ts(n.graph,n.buckets,n.zeroIdx).flatMap(s=>e.outEdges(s.v,s.w))}function ts(e,t,n){let r=[],s=t[t.length-1],o=t[0],i;for(;e.nodeCount();){for(;i=o.dequeue();)ie(e,t,n,i);for(;i=s.dequeue();)ie(e,t,n,i);if(e.nodeCount()){for(let a=t.length-2;a>0;--a)if(i=t[a].dequeue(),i){r=r.concat(ie(e,t,n,i,!0));break}}}return r}function ie(e,t,n,r,s){let o=s?[]:void 0;return e.inEdges(r.v).forEach(i=>{let a=e.edge(i),l=e.node(i.v);s&&o.push({v:i.v,w:i.w}),l.out-=a,me(t,n,l)}),e.outEdges(r.v).forEach(i=>{let a=e.edge(i),l=i.w,d=e.node(l);d.in-=a,me(t,n,d)}),e.removeNode(r.v),o}function ns(e,t){let n=new Kr,r=0,s=0;e.nodes().forEach(a=>{n.setNode(a,{v:a,in:0,out:0})}),e.edges().forEach(a=>{let l=n.edge(a.v,a.w)||0,d=t(a),c=l+d;n.setEdge(a.v,a.w,c),s=Math.max(s,n.node(a.v).out+=d),r=Math.max(r,n.node(a.w).in+=d)});let o=rs(s+r+3).map(()=>new Jr),i=r+1;return n.nodes().forEach(a=>{me(o,i,n.node(a))}),{graph:n,buckets:o,zeroIdx:i}}function me(e,t,n){n.out?n.in?e[n.out-n.in+t].enqueue(n):e[e.length-1].enqueue(n):e[0].enqueue(n)}function rs(e){const t=[];for(let n=0;n<e;n++)t.push(n);return t}let Ct=R.Graph;var y={addBorderNode:hs,addDummyNode:Ot,applyWithChunking:ee,asNonCompoundGraph:os,buildLayerMatrix:ds,intersectRect:ls,mapValues:vs,maxRank:St,normalizeRanks:cs,notime:gs,partition:ps,pick:ws,predecessorWeights:as,range:Lt,removeEmptyRanks:us,simplify:ss,successorWeights:is,time:ms,uniqueId:Rt,zipObject:xe};function Ot(e,t,n,r){let s;do s=Rt(r);while(e.hasNode(s));return n.dummy=t,e.setNode(s,n),s}function ss(e){let t=new Ct().setGraph(e.graph());return e.nodes().forEach(n=>t.setNode(n,e.node(n))),e.edges().forEach(n=>{let r=t.edge(n.v,n.w)||{weight:0,minlen:1},s=e.edge(n);t.setEdge(n.v,n.w,{weight:r.weight+s.weight,minlen:Math.max(r.minlen,s.minlen)})}),t}function os(e){let t=new Ct({multigraph:e.isMultigraph()}).setGraph(e.graph());return e.nodes().forEach(n=>{e.children(n).length||t.setNode(n,e.node(n))}),e.edges().forEach(n=>{t.setEdge(n,e.edge(n))}),t}function is(e){let t=e.nodes().map(n=>{let r={};return e.outEdges(n).forEach(s=>{r[s.w]=(r[s.w]||0)+e.edge(s).weight}),r});return xe(e.nodes(),t)}function as(e){let t=e.nodes().map(n=>{let r={};return e.inEdges(n).forEach(s=>{r[s.v]=(r[s.v]||0)+e.edge(s).weight}),r});return xe(e.nodes(),t)}function ls(e,t){let n=e.x,r=e.y,s=t.x-n,o=t.y-r,i=e.width/2,a=e.height/2;if(!s&&!o)throw new Error("Not possible to find intersection inside of the rectangle");let l,d;return Math.abs(o)*i>Math.abs(s)*a?(o<0&&(a=-a),l=a*s/o,d=a):(s<0&&(i=-i),l=i,d=i*o/s),{x:n+l,y:r+d}}function ds(e){let t=Lt(St(e)+1).map(()=>[]);return e.nodes().forEach(n=>{let r=e.node(n),s=r.rank;s!==void 0&&(t[s][r.order]=n)}),t}function cs(e){let t=e.nodes().map(r=>{let s=e.node(r).rank;return s===void 0?Number.MAX_VALUE:s}),n=ee(Math.min,t);e.nodes().forEach(r=>{let s=e.node(r);Object.hasOwn(s,"rank")&&(s.rank-=n)})}function us(e){let t=e.nodes().map(i=>e.node(i).rank),n=ee(Math.min,t),r=[];e.nodes().forEach(i=>{let a=e.node(i).rank-n;r[a]||(r[a]=[]),r[a].push(i)});let s=0,o=e.graph().nodeRankFactor;Array.from(r).forEach((i,a)=>{i===void 0&&a%o!==0?--s:i!==void 0&&s&&i.forEach(l=>e.node(l).rank+=s)})}function hs(e,t,n,r){let s={width:0,height:0};return arguments.length>=4&&(s.rank=n,s.order=r),Ot(e,"border",s,t)}function fs(e,t=It){const n=[];for(let r=0;r<e.length;r+=t){const s=e.slice(r,r+t);n.push(s)}return n}const It=65535;function ee(e,t){if(t.length>It){const n=fs(t);return e.apply(null,n.map(r=>e.apply(null,r)))}else return e.apply(null,t)}function St(e){const n=e.nodes().map(r=>{let s=e.node(r).rank;return s===void 0?Number.MIN_VALUE:s});return ee(Math.max,n)}function ps(e,t){let n={lhs:[],rhs:[]};return e.forEach(r=>{t(r)?n.lhs.push(r):n.rhs.push(r)}),n}function ms(e,t){let n=Date.now();try{return t()}finally{console.log(e+" time: "+(Date.now()-n)+"ms")}}function gs(e,t){return t()}let bs=0;function Rt(e){var t=++bs;return toString(e)+t}function Lt(e,t,n=1){t==null&&(t=e,e=0);let r=o=>o<t;n<0&&(r=o=>t<o);const s=[];for(let o=e;r(o);o+=n)s.push(o);return s}function ws(e,t){const n={};for(const r of t)e[r]!==void 0&&(n[r]=e[r]);return n}function vs(e,t){let n=t;return typeof t=="string"&&(n=r=>r[t]),Object.entries(e).reduce((r,[s,o])=>(r[s]=n(o,s),r),{})}function xe(e,t){return e.reduce((n,r,s)=>(n[r]=t[s],n),{})}let xs=Qr,Es=y.uniqueId;var ys={run:ks,undo:_s};function ks(e){(e.graph().acyclicer==="greedy"?xs(e,n(e)):Ns(e)).forEach(r=>{let s=e.edge(r);e.removeEdge(r),s.forwardName=r.name,s.reversed=!0,e.setEdge(r.w,r.v,s,Es("rev"))});function n(r){return s=>r.edge(s).weight}}function Ns(e){let t=[],n={},r={};function s(o){Object.hasOwn(r,o)||(r[o]=!0,n[o]=!0,e.outEdges(o).forEach(i=>{Object.hasOwn(n,i.w)?t.push(i):s(i.w)}),delete n[o])}return e.nodes().forEach(s),t}function _s(e){e.edges().forEach(t=>{let n=e.edge(t);if(n.reversed){e.removeEdge(t);let r=n.forwardName;delete n.reversed,delete n.forwardName,e.setEdge(t.w,t.v,n,r)}})}let js=y;var Cs={run:Os,undo:Ss};function Os(e){e.graph().dummyChains=[],e.edges().forEach(t=>Is(e,t))}function Is(e,t){let n=t.v,r=e.node(n).rank,s=t.w,o=e.node(s).rank,i=t.name,a=e.edge(t),l=a.labelRank;if(o===r+1)return;e.removeEdge(t);let d,c,h;for(h=0,++r;r<o;++h,++r)a.points=[],c={width:0,height:0,edgeLabel:a,edgeObj:t,rank:r},d=js.addDummyNode(e,"edge",c,"_d"),r===l&&(c.width=a.width,c.height=a.height,c.dummy="edge-label",c.labelpos=a.labelpos),e.setEdge(n,d,{weight:a.weight},i),h===0&&e.graph().dummyChains.push(d),n=d;e.setEdge(n,s,{weight:a.weight},i)}function Ss(e){e.graph().dummyChains.forEach(t=>{let n=e.node(t),r=n.edgeLabel,s;for(e.setEdge(n.edgeObj,r);n.dummy;)s=e.successors(t)[0],e.removeNode(t),r.points.push({x:n.x,y:n.y}),n.dummy==="edge-label"&&(r.x=n.x,r.y=n.y,r.width=n.width,r.height=n.height),t=s,n=e.node(t)})}const{applyWithChunking:Rs}=y;var te={longestPath:Ls,slack:Ts};function Ls(e){var t={};function n(r){var s=e.node(r);if(Object.hasOwn(t,r))return s.rank;t[r]=!0;let o=e.outEdges(r).map(a=>a==null?Number.POSITIVE_INFINITY:n(a.w)-e.edge(a).minlen);var i=Rs(Math.min,o);return i===Number.POSITIVE_INFINITY&&(i=0),s.rank=i}e.sources().forEach(n)}function Ts(e,t){return e.node(t.w).rank-e.node(t.v).rank-e.edge(t).minlen}var Ms=R.Graph,Q=te.slack,Tt=Fs;function Fs(e){var t=new Ms({directed:!1}),n=e.nodes()[0],r=e.nodeCount();t.setNode(n,{});for(var s,o;$s(t,e)<r;)s=Ps(t,e),o=t.hasNode(s.v)?Q(e,s):-Q(e,s),Ds(t,e,o);return t}function $s(e,t){function n(r){t.nodeEdges(r).forEach(s=>{var o=s.v,i=r===o?s.w:o;!e.hasNode(i)&&!Q(t,s)&&(e.setNode(i,{}),e.setEdge(r,i,{}),n(i))})}return e.nodes().forEach(n),e.nodeCount()}function Ps(e,t){return t.edges().reduce((r,s)=>{let o=Number.POSITIVE_INFINITY;return e.hasNode(s.v)!==e.hasNode(s.w)&&(o=Q(t,s)),o<r[0]?[o,s]:r},[Number.POSITIVE_INFINITY,null])[1]}function Ds(e,t,n){e.nodes().forEach(r=>t.node(r).rank+=n)}var As=Tt,Xe=te.slack,Gs=te.longestPath,Vs=R.alg.preorder,zs=R.alg.postorder,Hs=y.simplify,Bs=G;G.initLowLimValues=ye;G.initCutValues=Ee;G.calcCutValue=Mt;G.leaveEdge=$t;G.enterEdge=Pt;G.exchangeEdges=Dt;function G(e){e=Hs(e),Gs(e);var t=As(e);ye(t),Ee(t,e);for(var n,r;n=$t(t);)r=Pt(t,e,n),Dt(t,e,n,r)}function Ee(e,t){var n=zs(e,e.nodes());n=n.slice(0,n.length-1),n.forEach(r=>Ws(e,t,r))}function Ws(e,t,n){var r=e.node(n),s=r.parent;e.edge(n,s).cutvalue=Mt(e,t,n)}function Mt(e,t,n){var r=e.node(n),s=r.parent,o=!0,i=t.edge(n,s),a=0;return i||(o=!1,i=t.edge(s,n)),a=i.weight,t.nodeEdges(n).forEach(l=>{var d=l.v===n,c=d?l.w:l.v;if(c!==s){var h=d===o,f=t.edge(l).weight;if(a+=h?f:-f,Ys(e,n,c)){var p=e.edge(n,c).cutvalue;a+=h?-p:p}}}),a}function ye(e,t){arguments.length<2&&(t=e.nodes()[0]),Ft(e,{},1,t)}function Ft(e,t,n,r,s){var o=n,i=e.node(r);return t[r]=!0,e.neighbors(r).forEach(a=>{Object.hasOwn(t,a)||(n=Ft(e,t,n,a,r))}),i.low=o,i.lim=n++,s?i.parent=s:delete i.parent,n}function $t(e){return e.edges().find(t=>e.edge(t).cutvalue<0)}function Pt(e,t,n){var r=n.v,s=n.w;t.hasEdge(r,s)||(r=n.w,s=n.v);var o=e.node(r),i=e.node(s),a=o,l=!1;o.lim>i.lim&&(a=i,l=!0);var d=t.edges().filter(c=>l===qe(e,e.node(c.v),a)&&l!==qe(e,e.node(c.w),a));return d.reduce((c,h)=>Xe(t,h)<Xe(t,c)?h:c)}function Dt(e,t,n,r){var s=n.v,o=n.w;e.removeEdge(s,o),e.setEdge(r.v,r.w,{}),ye(e),Ee(e,t),Us(e,t)}function Us(e,t){var n=e.nodes().find(s=>!t.node(s).parent),r=Vs(e,n);r=r.slice(1),r.forEach(s=>{var o=e.node(s).parent,i=t.edge(s,o),a=!1;i||(i=t.edge(o,s),a=!0),t.node(s).rank=t.node(o).rank+(a?i.minlen:-i.minlen)})}function Ys(e,t,n){return e.hasEdge(t,n)}function qe(e,t,n){return n.low<=t.lim&&t.lim<=n.lim}var Xs=te,At=Xs.longestPath,qs=Tt,Ks=Bs,Js=Qs;function Qs(e){switch(e.graph().ranker){case"network-simplex":Ke(e);break;case"tight-tree":eo(e);break;case"longest-path":Zs(e);break;default:Ke(e)}}var Zs=At;function eo(e){At(e),qs(e)}function Ke(e){Ks(e)}var to=no;function no(e){let t=so(e);e.graph().dummyChains.forEach(n=>{let r=e.node(n),s=r.edgeObj,o=ro(e,t,s.v,s.w),i=o.path,a=o.lca,l=0,d=i[l],c=!0;for(;n!==s.w;){if(r=e.node(n),c){for(;(d=i[l])!==a&&e.node(d).maxRank<r.rank;)l++;d===a&&(c=!1)}if(!c){for(;l<i.length-1&&e.node(d=i[l+1]).minRank<=r.rank;)l++;d=i[l]}e.setParent(n,d),n=e.successors(n)[0]}})}function ro(e,t,n,r){let s=[],o=[],i=Math.min(t[n].low,t[r].low),a=Math.max(t[n].lim,t[r].lim),l,d;l=n;do l=e.parent(l),s.push(l);while(l&&(t[l].low>i||a>t[l].lim));for(d=l,l=r;(l=e.parent(l))!==d;)o.push(l);return{path:s.concat(o.reverse()),lca:d}}function so(e){let t={},n=0;function r(s){let o=n;e.children(s).forEach(r),t[s]={low:o,lim:n++}}return e.children().forEach(r),t}let Z=y;var oo={run:io,cleanup:co};function io(e){let t=Z.addDummyNode(e,"root",{},"_root"),n=ao(e),r=Object.values(n),s=Z.applyWithChunking(Math.max,r)-1,o=2*s+1;e.graph().nestingRoot=t,e.edges().forEach(a=>e.edge(a).minlen*=o);let i=lo(e)+1;e.children().forEach(a=>Gt(e,t,o,i,s,n,a)),e.graph().nodeRankFactor=o}function Gt(e,t,n,r,s,o,i){let a=e.children(i);if(!a.length){i!==t&&e.setEdge(t,i,{weight:0,minlen:n});return}let l=Z.addBorderNode(e,"_bt"),d=Z.addBorderNode(e,"_bb"),c=e.node(i);e.setParent(l,i),c.borderTop=l,e.setParent(d,i),c.borderBottom=d,a.forEach(h=>{Gt(e,t,n,r,s,o,h);let f=e.node(h),p=f.borderTop?f.borderTop:h,b=f.borderBottom?f.borderBottom:h,w=f.borderTop?r:2*r,_=p!==b?1:s-o[i]+1;e.setEdge(l,p,{weight:w,minlen:_,nestingEdge:!0}),e.setEdge(b,d,{weight:w,minlen:_,nestingEdge:!0})}),e.parent(i)||e.setEdge(t,l,{weight:0,minlen:s+o[i]})}function ao(e){var t={};function n(r,s){var o=e.children(r);o&&o.length&&o.forEach(i=>n(i,s+1)),t[r]=s}return e.children().forEach(r=>n(r,1)),t}function lo(e){return e.edges().reduce((t,n)=>t+e.edge(n).weight,0)}function co(e){var t=e.graph();e.removeNode(t.nestingRoot),delete t.nestingRoot,e.edges().forEach(n=>{var r=e.edge(n);r.nestingEdge&&e.removeEdge(n)})}let uo=y;var ho=fo;function fo(e){function t(n){let r=e.children(n),s=e.node(n);if(r.length&&r.forEach(t),Object.hasOwn(s,"minRank")){s.borderLeft=[],s.borderRight=[];for(let o=s.minRank,i=s.maxRank+1;o<i;++o)Je(e,"borderLeft","_bl",n,s,o),Je(e,"borderRight","_br",n,s,o)}}e.children().forEach(t)}function Je(e,t,n,r,s,o){let i={width:0,height:0,rank:o,borderType:t},a=s[t][o-1],l=uo.addDummyNode(e,"border",i,n);s[t][o]=l,e.setParent(l,r),a&&e.setEdge(a,l,{weight:1})}var po={adjust:mo,undo:go};function mo(e){let t=e.graph().rankdir.toLowerCase();(t==="lr"||t==="rl")&&Vt(e)}function go(e){let t=e.graph().rankdir.toLowerCase();(t==="bt"||t==="rl")&&bo(e),(t==="lr"||t==="rl")&&(wo(e),Vt(e))}function Vt(e){e.nodes().forEach(t=>Qe(e.node(t))),e.edges().forEach(t=>Qe(e.edge(t)))}function Qe(e){let t=e.width;e.width=e.height,e.height=t}function bo(e){e.nodes().forEach(t=>ae(e.node(t))),e.edges().forEach(t=>{let n=e.edge(t);n.points.forEach(ae),Object.hasOwn(n,"y")&&ae(n)})}function ae(e){e.y=-e.y}function wo(e){e.nodes().forEach(t=>le(e.node(t))),e.edges().forEach(t=>{let n=e.edge(t);n.points.forEach(le),Object.hasOwn(n,"x")&&le(n)})}function le(e){let t=e.x;e.x=e.y,e.y=t}let Ze=y;var vo=xo;function xo(e){let t={},n=e.nodes().filter(l=>!e.children(l).length),r=n.map(l=>e.node(l).rank),s=Ze.applyWithChunking(Math.max,r),o=Ze.range(s+1).map(()=>[]);function i(l){if(t[l])return;t[l]=!0;let d=e.node(l);o[d.rank].push(l),e.successors(l).forEach(i)}return n.sort((l,d)=>e.node(l).rank-e.node(d).rank).forEach(i),o}let Eo=y.zipObject;var yo=ko;function ko(e,t){let n=0;for(let r=1;r<t.length;++r)n+=No(e,t[r-1],t[r]);return n}function No(e,t,n){let r=Eo(n,n.map((d,c)=>c)),s=t.flatMap(d=>e.outEdges(d).map(c=>({pos:r[c.w],weight:e.edge(c).weight})).sort((c,h)=>c.pos-h.pos)),o=1;for(;o<n.length;)o<<=1;let i=2*o-1;o-=1;let a=new Array(i).fill(0),l=0;return s.forEach(d=>{let c=d.pos+o;a[c]+=d.weight;let h=0;for(;c>0;)c%2&&(h+=a[c+1]),c=c-1>>1,a[c]+=d.weight;l+=d.weight*h}),l}var _o=jo;function jo(e,t=[]){return t.map(n=>{let r=e.inEdges(n);if(r.length){let s=r.reduce((o,i)=>{let a=e.edge(i),l=e.node(i.v);return{sum:o.sum+a.weight*l.order,weight:o.weight+a.weight}},{sum:0,weight:0});return{v:n,barycenter:s.sum/s.weight,weight:s.weight}}else return{v:n}})}let Co=y;var Oo=Io;function Io(e,t){let n={};e.forEach((s,o)=>{let i=n[s.v]={indegree:0,in:[],out:[],vs:[s.v],i:o};s.barycenter!==void 0&&(i.barycenter=s.barycenter,i.weight=s.weight)}),t.edges().forEach(s=>{let o=n[s.v],i=n[s.w];o!==void 0&&i!==void 0&&(i.indegree++,o.out.push(n[s.w]))});let r=Object.values(n).filter(s=>!s.indegree);return So(r)}function So(e){let t=[];function n(s){return o=>{o.merged||(o.barycenter===void 0||s.barycenter===void 0||o.barycenter>=s.barycenter)&&Ro(s,o)}}function r(s){return o=>{o.in.push(s),--o.indegree===0&&e.push(o)}}for(;e.length;){let s=e.pop();t.push(s),s.in.reverse().forEach(n(s)),s.out.forEach(r(s))}return t.filter(s=>!s.merged).map(s=>Co.pick(s,["vs","i","barycenter","weight"]))}function Ro(e,t){let n=0,r=0;e.weight&&(n+=e.barycenter*e.weight,r+=e.weight),t.weight&&(n+=t.barycenter*t.weight,r+=t.weight),e.vs=t.vs.concat(e.vs),e.barycenter=n/r,e.weight=r,e.i=Math.min(t.i,e.i),t.merged=!0}let Lo=y;var To=Mo;function Mo(e,t){let n=Lo.partition(e,c=>Object.hasOwn(c,"barycenter")),r=n.lhs,s=n.rhs.sort((c,h)=>h.i-c.i),o=[],i=0,a=0,l=0;r.sort(Fo(!!t)),l=et(o,s,l),r.forEach(c=>{l+=c.vs.length,o.push(c.vs),i+=c.barycenter*c.weight,a+=c.weight,l=et(o,s,l)});let d={vs:o.flat(!0)};return a&&(d.barycenter=i/a,d.weight=a),d}function et(e,t,n){let r;for(;t.length&&(r=t[t.length-1]).i<=n;)t.pop(),e.push(r.vs),n++;return n}function Fo(e){return(t,n)=>t.barycenter<n.barycenter?-1:t.barycenter>n.barycenter?1:e?n.i-t.i:t.i-n.i}let $o=_o,Po=Oo,Do=To;var Ao=zt;function zt(e,t,n,r){let s=e.children(t),o=e.node(t),i=o?o.borderLeft:void 0,a=o?o.borderRight:void 0,l={};i&&(s=s.filter(f=>f!==i&&f!==a));let d=$o(e,s);d.forEach(f=>{if(e.children(f.v).length){let p=zt(e,f.v,n,r);l[f.v]=p,Object.hasOwn(p,"barycenter")&&Vo(f,p)}});let c=Po(d,n);Go(c,l);let h=Do(c,r);if(i&&(h.vs=[i,h.vs,a].flat(!0),e.predecessors(i).length)){let f=e.node(e.predecessors(i)[0]),p=e.node(e.predecessors(a)[0]);Object.hasOwn(h,"barycenter")||(h.barycenter=0,h.weight=0),h.barycenter=(h.barycenter*h.weight+f.order+p.order)/(h.weight+2),h.weight+=2}return h}function Go(e,t){e.forEach(n=>{n.vs=n.vs.flatMap(r=>t[r]?t[r].vs:r)})}function Vo(e,t){e.barycenter!==void 0?(e.barycenter=(e.barycenter*e.weight+t.barycenter*t.weight)/(e.weight+t.weight),e.weight+=t.weight):(e.barycenter=t.barycenter,e.weight=t.weight)}let zo=R.Graph,Ho=y;var Bo=Wo;function Wo(e,t,n){let r=Uo(e),s=new zo({compound:!0}).setGraph({root:r}).setDefaultNodeLabel(o=>e.node(o));return e.nodes().forEach(o=>{let i=e.node(o),a=e.parent(o);(i.rank===t||i.minRank<=t&&t<=i.maxRank)&&(s.setNode(o),s.setParent(o,a||r),e[n](o).forEach(l=>{let d=l.v===o?l.w:l.v,c=s.edge(d,o),h=c!==void 0?c.weight:0;s.setEdge(d,o,{weight:e.edge(l).weight+h})}),Object.hasOwn(i,"minRank")&&s.setNode(o,{borderLeft:i.borderLeft[t],borderRight:i.borderRight[t]}))}),s}function Uo(e){for(var t;e.hasNode(t=Ho.uniqueId("_root")););return t}var Yo=Xo;function Xo(e,t,n){let r={},s;n.forEach(o=>{let i=e.parent(o),a,l;for(;i;){if(a=e.parent(i),a?(l=r[a],r[a]=i):(l=s,s=i),l&&l!==i){t.setEdge(l,i);return}i=a}})}let qo=vo,Ko=yo,Jo=Ao,Qo=Bo,Zo=Yo,ei=R.Graph,X=y;var ti=Ht;function Ht(e,t){if(t&&typeof t.customOrder=="function"){t.customOrder(e,Ht);return}let n=X.maxRank(e),r=tt(e,X.range(1,n+1),"inEdges"),s=tt(e,X.range(n-1,-1,-1),"outEdges"),o=qo(e);if(nt(e,o),t&&t.disableOptimalOrderHeuristic)return;let i=Number.POSITIVE_INFINITY,a;for(let l=0,d=0;d<4;++l,++d){ni(l%2?r:s,l%4>=2),o=X.buildLayerMatrix(e);let c=Ko(e,o);c<i&&(d=0,a=Object.assign({},o),i=c)}nt(e,a)}function tt(e,t,n){return t.map(function(r){return Qo(e,r,n)})}function ni(e,t){let n=new ei;e.forEach(function(r){let s=r.graph().root,o=Jo(r,s,n,t);o.vs.forEach((i,a)=>r.node(i).order=a),Zo(r,n,o.vs)})}function nt(e,t){Object.values(t).forEach(n=>n.forEach((r,s)=>e.node(r).order=s))}let ri=R.Graph,M=y;var si={positionX:ai,findType1Conflicts:Bt,findType2Conflicts:Wt,addConflict:ke,hasConflict:Ut,verticalAlignment:Yt,horizontalCompaction:Xt,alignCoordinates:Kt,findSmallestWidthAlignment:qt,balance:Jt};function Bt(e,t){let n={};function r(s,o){let i=0,a=0,l=s.length,d=o[o.length-1];return o.forEach((c,h)=>{let f=oi(e,c),p=f?e.node(f).order:l;(f||c===d)&&(o.slice(a,h+1).forEach(b=>{e.predecessors(b).forEach(w=>{let _=e.node(w),k=_.order;(k<i||p<k)&&!(_.dummy&&e.node(b).dummy)&&ke(n,w,b)})}),a=h+1,i=p)}),o}return t.length&&t.reduce(r),n}function Wt(e,t){let n={};function r(o,i,a,l,d){let c;M.range(i,a).forEach(h=>{c=o[h],e.node(c).dummy&&e.predecessors(c).forEach(f=>{let p=e.node(f);p.dummy&&(p.order<l||p.order>d)&&ke(n,f,c)})})}function s(o,i){let a=-1,l,d=0;return i.forEach((c,h)=>{if(e.node(c).dummy==="border"){let f=e.predecessors(c);f.length&&(l=e.node(f[0]).order,r(i,d,h,a,l),d=h,a=l)}r(i,d,i.length,l,o.length)}),i}return t.length&&t.reduce(s),n}function oi(e,t){if(e.node(t).dummy)return e.predecessors(t).find(n=>e.node(n).dummy)}function ke(e,t,n){if(t>n){let s=t;t=n,n=s}let r=e[t];r||(e[t]=r={}),r[n]=!0}function Ut(e,t,n){if(t>n){let r=t;t=n,n=r}return!!e[t]&&Object.hasOwn(e[t],n)}function Yt(e,t,n,r){let s={},o={},i={};return t.forEach(a=>{a.forEach((l,d)=>{s[l]=l,o[l]=l,i[l]=d})}),t.forEach(a=>{let l=-1;a.forEach(d=>{let c=r(d);if(c.length){c=c.sort((f,p)=>i[f]-i[p]);let h=(c.length-1)/2;for(let f=Math.floor(h),p=Math.ceil(h);f<=p;++f){let b=c[f];o[d]===d&&l<i[b]&&!Ut(n,d,b)&&(o[b]=d,o[d]=s[d]=s[b],l=i[b])}}})}),{root:s,align:o}}function Xt(e,t,n,r,s){let o={},i=ii(e,t,n,s),a=s?"borderLeft":"borderRight";function l(h,f){let p=i.nodes(),b=p.pop(),w={};for(;b;)w[b]?h(b):(w[b]=!0,p.push(b),p=p.concat(f(b))),b=p.pop()}function d(h){o[h]=i.inEdges(h).reduce((f,p)=>Math.max(f,o[p.v]+i.edge(p)),0)}function c(h){let f=i.outEdges(h).reduce((b,w)=>Math.min(b,o[w.w]-i.edge(w)),Number.POSITIVE_INFINITY),p=e.node(h);f!==Number.POSITIVE_INFINITY&&p.borderType!==a&&(o[h]=Math.max(o[h],f))}return l(d,i.predecessors.bind(i)),l(c,i.successors.bind(i)),Object.keys(r).forEach(h=>o[h]=o[n[h]]),o}function ii(e,t,n,r){let s=new ri,o=e.graph(),i=li(o.nodesep,o.edgesep,r);return t.forEach(a=>{let l;a.forEach(d=>{let c=n[d];if(s.setNode(c),l){var h=n[l],f=s.edge(h,c);s.setEdge(h,c,Math.max(i(e,d,l),f||0))}l=d})}),s}function qt(e,t){return Object.values(t).reduce((n,r)=>{let s=Number.NEGATIVE_INFINITY,o=Number.POSITIVE_INFINITY;Object.entries(r).forEach(([a,l])=>{let d=di(e,a)/2;s=Math.max(l+d,s),o=Math.min(l-d,o)});const i=s-o;return i<n[0]&&(n=[i,r]),n},[Number.POSITIVE_INFINITY,null])[1]}function Kt(e,t){let n=Object.values(t),r=M.applyWithChunking(Math.min,n),s=M.applyWithChunking(Math.max,n);["u","d"].forEach(o=>{["l","r"].forEach(i=>{let a=o+i,l=e[a];if(l===t)return;let d=Object.values(l),c=r-M.applyWithChunking(Math.min,d);i!=="l"&&(c=s-M.applyWithChunking(Math.max,d)),c&&(e[a]=M.mapValues(l,h=>h+c))})})}function Jt(e,t){return M.mapValues(e.ul,(n,r)=>{if(t)return e[t.toLowerCase()][r];{let s=Object.values(e).map(o=>o[r]).sort((o,i)=>o-i);return(s[1]+s[2])/2}})}function ai(e){let t=M.buildLayerMatrix(e),n=Object.assign(Bt(e,t),Wt(e,t)),r={},s;["u","d"].forEach(i=>{s=i==="u"?t:Object.values(t).reverse(),["l","r"].forEach(a=>{a==="r"&&(s=s.map(h=>Object.values(h).reverse()));let l=(i==="u"?e.predecessors:e.successors).bind(e),d=Yt(e,s,n,l),c=Xt(e,s,d.root,d.align,a==="r");a==="r"&&(c=M.mapValues(c,h=>-h)),r[i+a]=c})});let o=qt(e,r);return Kt(r,o),Jt(r,e.graph().align)}function li(e,t,n){return(r,s,o)=>{let i=r.node(s),a=r.node(o),l=0,d;if(l+=i.width/2,Object.hasOwn(i,"labelpos"))switch(i.labelpos.toLowerCase()){case"l":d=-i.width/2;break;case"r":d=i.width/2;break}if(d&&(l+=n?d:-d),d=0,l+=(i.dummy?t:e)/2,l+=(a.dummy?t:e)/2,l+=a.width/2,Object.hasOwn(a,"labelpos"))switch(a.labelpos.toLowerCase()){case"l":d=a.width/2;break;case"r":d=-a.width/2;break}return d&&(l+=n?d:-d),d=0,l}}function di(e,t){return e.node(t).width}let Qt=y,ci=si.positionX;var ui=hi;function hi(e){e=Qt.asNonCompoundGraph(e),fi(e),Object.entries(ci(e)).forEach(([t,n])=>e.node(t).x=n)}function fi(e){let t=Qt.buildLayerMatrix(e),n=e.graph().ranksep,r=0;t.forEach(s=>{const o=s.reduce((i,a)=>{const l=e.node(a).height;return i>l?i:l},0);s.forEach(i=>e.node(i).y=r+o/2),r+=o+n})}let rt=ys,st=Cs,pi=Js,mi=y.normalizeRanks,gi=to,bi=y.removeEmptyRanks,ot=oo,wi=ho,it=po,vi=ti,xi=ui,I=y,Ei=R.Graph;var yi=ki;function ki(e,t){let n=t&&t.debugTiming?I.time:I.notime;n("layout",()=>{let r=n("  buildLayoutGraph",()=>Ti(e));n("  runLayout",()=>Ni(r,n,t)),n("  updateInputGraph",()=>_i(e,r))})}function Ni(e,t,n){t("    makeSpaceForEdgeLabels",()=>Mi(e)),t("    removeSelfEdges",()=>Hi(e)),t("    acyclic",()=>rt.run(e)),t("    nestingGraph.run",()=>ot.run(e)),t("    rank",()=>pi(I.asNonCompoundGraph(e))),t("    injectEdgeLabelProxies",()=>Fi(e)),t("    removeEmptyRanks",()=>bi(e)),t("    nestingGraph.cleanup",()=>ot.cleanup(e)),t("    normalizeRanks",()=>mi(e)),t("    assignRankMinMax",()=>$i(e)),t("    removeEdgeLabelProxies",()=>Pi(e)),t("    normalize.run",()=>st.run(e)),t("    parentDummyChains",()=>gi(e)),t("    addBorderSegments",()=>wi(e)),t("    order",()=>vi(e,n)),t("    insertSelfEdges",()=>Bi(e)),t("    adjustCoordinateSystem",()=>it.adjust(e)),t("    position",()=>xi(e)),t("    positionSelfEdges",()=>Wi(e)),t("    removeBorderNodes",()=>zi(e)),t("    normalize.undo",()=>st.undo(e)),t("    fixupEdgeLabelCoords",()=>Gi(e)),t("    undoCoordinateSystem",()=>it.undo(e)),t("    translateGraph",()=>Di(e)),t("    assignNodeIntersects",()=>Ai(e)),t("    reversePoints",()=>Vi(e)),t("    acyclic.undo",()=>rt.undo(e))}function _i(e,t){e.nodes().forEach(n=>{let r=e.node(n),s=t.node(n);r&&(r.x=s.x,r.y=s.y,r.rank=s.rank,t.children(n).length&&(r.width=s.width,r.height=s.height))}),e.edges().forEach(n=>{let r=e.edge(n),s=t.edge(n);r.points=s.points,Object.hasOwn(s,"x")&&(r.x=s.x,r.y=s.y)}),e.graph().width=t.graph().width,e.graph().height=t.graph().height}let ji=["nodesep","edgesep","ranksep","marginx","marginy"],Ci={ranksep:50,edgesep:20,nodesep:50,rankdir:"tb"},Oi=["acyclicer","ranker","rankdir","align"],Ii=["width","height"],at={width:0,height:0},Si=["minlen","weight","width","height","labeloffset"],Ri={minlen:1,weight:1,width:0,height:0,labeloffset:10,labelpos:"r"},Li=["labelpos"];function Ti(e){let t=new Ei({multigraph:!0,compound:!0}),n=ce(e.graph());return t.setGraph(Object.assign({},Ci,de(n,ji),I.pick(n,Oi))),e.nodes().forEach(r=>{let s=ce(e.node(r));const o=de(s,Ii);Object.keys(at).forEach(i=>{o[i]===void 0&&(o[i]=at[i])}),t.setNode(r,o),t.setParent(r,e.parent(r))}),e.edges().forEach(r=>{let s=ce(e.edge(r));t.setEdge(r,Object.assign({},Ri,de(s,Si),I.pick(s,Li)))}),t}function Mi(e){let t=e.graph();t.ranksep/=2,e.edges().forEach(n=>{let r=e.edge(n);r.minlen*=2,r.labelpos.toLowerCase()!=="c"&&(t.rankdir==="TB"||t.rankdir==="BT"?r.width+=r.labeloffset:r.height+=r.labeloffset)})}function Fi(e){e.edges().forEach(t=>{let n=e.edge(t);if(n.width&&n.height){let r=e.node(t.v),o={rank:(e.node(t.w).rank-r.rank)/2+r.rank,e:t};I.addDummyNode(e,"edge-proxy",o,"_ep")}})}function $i(e){let t=0;e.nodes().forEach(n=>{let r=e.node(n);r.borderTop&&(r.minRank=e.node(r.borderTop).rank,r.maxRank=e.node(r.borderBottom).rank,t=Math.max(t,r.maxRank))}),e.graph().maxRank=t}function Pi(e){e.nodes().forEach(t=>{let n=e.node(t);n.dummy==="edge-proxy"&&(e.edge(n.e).labelRank=n.rank,e.removeNode(t))})}function Di(e){let t=Number.POSITIVE_INFINITY,n=0,r=Number.POSITIVE_INFINITY,s=0,o=e.graph(),i=o.marginx||0,a=o.marginy||0;function l(d){let c=d.x,h=d.y,f=d.width,p=d.height;t=Math.min(t,c-f/2),n=Math.max(n,c+f/2),r=Math.min(r,h-p/2),s=Math.max(s,h+p/2)}e.nodes().forEach(d=>l(e.node(d))),e.edges().forEach(d=>{let c=e.edge(d);Object.hasOwn(c,"x")&&l(c)}),t-=i,r-=a,e.nodes().forEach(d=>{let c=e.node(d);c.x-=t,c.y-=r}),e.edges().forEach(d=>{let c=e.edge(d);c.points.forEach(h=>{h.x-=t,h.y-=r}),Object.hasOwn(c,"x")&&(c.x-=t),Object.hasOwn(c,"y")&&(c.y-=r)}),o.width=n-t+i,o.height=s-r+a}function Ai(e){e.edges().forEach(t=>{let n=e.edge(t),r=e.node(t.v),s=e.node(t.w),o,i;n.points?(o=n.points[0],i=n.points[n.points.length-1]):(n.points=[],o=s,i=r),n.points.unshift(I.intersectRect(r,o)),n.points.push(I.intersectRect(s,i))})}function Gi(e){e.edges().forEach(t=>{let n=e.edge(t);if(Object.hasOwn(n,"x"))switch((n.labelpos==="l"||n.labelpos==="r")&&(n.width-=n.labeloffset),n.labelpos){case"l":n.x-=n.width/2+n.labeloffset;break;case"r":n.x+=n.width/2+n.labeloffset;break}})}function Vi(e){e.edges().forEach(t=>{let n=e.edge(t);n.reversed&&n.points.reverse()})}function zi(e){e.nodes().forEach(t=>{if(e.children(t).length){let n=e.node(t),r=e.node(n.borderTop),s=e.node(n.borderBottom),o=e.node(n.borderLeft[n.borderLeft.length-1]),i=e.node(n.borderRight[n.borderRight.length-1]);n.width=Math.abs(i.x-o.x),n.height=Math.abs(s.y-r.y),n.x=o.x+n.width/2,n.y=r.y+n.height/2}}),e.nodes().forEach(t=>{e.node(t).dummy==="border"&&e.removeNode(t)})}function Hi(e){e.edges().forEach(t=>{if(t.v===t.w){var n=e.node(t.v);n.selfEdges||(n.selfEdges=[]),n.selfEdges.push({e:t,label:e.edge(t)}),e.removeEdge(t)}})}function Bi(e){var t=I.buildLayerMatrix(e);t.forEach(n=>{var r=0;n.forEach((s,o)=>{var i=e.node(s);i.order=o+r,(i.selfEdges||[]).forEach(a=>{I.addDummyNode(e,"selfedge",{width:a.label.width,height:a.label.height,rank:i.rank,order:o+ ++r,e:a.e,label:a.label},"_se")}),delete i.selfEdges})})}function Wi(e){e.nodes().forEach(t=>{var n=e.node(t);if(n.dummy==="selfedge"){var r=e.node(n.e.v),s=r.x+r.width/2,o=r.y,i=n.x-s,a=r.height/2;e.setEdge(n.e,n.label),e.removeNode(t),n.label.points=[{x:s+2*i/3,y:o-a},{x:s+5*i/6,y:o-a},{x:s+i,y:o},{x:s+5*i/6,y:o+a},{x:s+2*i/3,y:o+a}],n.label.x=n.x,n.label.y=n.y}})}function de(e,t){return I.mapValues(I.pick(e,t),Number)}function ce(e){var t={};return e&&Object.entries(e).forEach(([n,r])=>{typeof n=="string"&&(n=n.toLowerCase()),t[n]=r}),t}let Ui=y,Yi=R.Graph;var Xi={debugOrdering:qi};function qi(e){let t=Ui.buildLayerMatrix(e),n=new Yi({compound:!0,multigraph:!0}).setGraph({});return e.nodes().forEach(r=>{n.setNode(r,{label:r}),n.setParent(r,"layer"+e.node(r).rank)}),e.edges().forEach(r=>n.setEdge(r.v,r.w,{},r.name)),t.forEach((r,s)=>{let o="layer"+s;n.setNode(o,{rank:"same"}),r.reduce((i,a)=>(n.setEdge(i,a,{style:"invis"}),a))}),n}var Ki="1.1.4",Ji={graphlib:R,layout:yi,debug:Xi,util:{time:y.time,notime:y.notime},version:Ki};const Zt=pn(Ji),B=new Zt.graphlib.Graph().setDefaultEdgeLabel(()=>({})),Qi=(e,t,n="TB")=>{const r=n==="LR";return B.setGraph({rankdir:n}),e.forEach(o=>{const{width:i=300,height:a=200}=o.measured??{};B.setNode(o.id,{width:i,height:a})}),t.forEach(o=>{B.setEdge(o.source,o.target)}),Zt.layout(B),{nodes:e.map(o=>{const{width:i=300,height:a=200}=o.measured??{},l=B.node(o.id);return{...o,targetPosition:r?"left":"top",sourcePosition:r?"right":"bottom",position:{x:l.x-i/2,y:l.y-a/2}}}),edges:t}};function Zi(e,t){const[n,r]=m.useState(e);return m.useEffect(()=>{const s=setTimeout(()=>{r(e)},t);return()=>{clearTimeout(s)}},[e,t]),n}const en=m.forwardRef(({className:e,...t},n)=>u.jsx("div",{ref:n,className:S("rounded-lg border bg-card text-card-foreground shadow-sm",e),...t}));en.displayName="Card";const tn=m.forwardRef(({className:e,...t},n)=>u.jsx("div",{ref:n,className:S("flex flex-col space-y-1.5 p-6",e),...t}));tn.displayName="CardHeader";const ea=m.forwardRef(({className:e,...t},n)=>u.jsx("h3",{ref:n,className:S("text-2xl font-semibold leading-none tracking-tight",e),...t}));ea.displayName="CardTitle";const ta=m.forwardRef(({className:e,...t},n)=>u.jsx("p",{ref:n,className:S("text-sm text-muted-foreground",e),...t}));ta.displayName="CardDescription";const nn=m.forwardRef(({className:e,...t},n)=>u.jsx("div",{ref:n,className:S("p-6 pt-0",e),...t}));nn.displayName="CardContent";const na=m.forwardRef(({className:e,...t},n)=>u.jsx("div",{ref:n,className:S("flex items-center p-6 pt-0",e),...t}));na.displayName="CardFooter";const rn=m.forwardRef((e,t)=>u.jsx("div",{ref:t,className:S("text-xs rounded-lg bg-zinc-800 px-2 py-1 font-semibold",e.className),...e}));rn.displayName="Tag";function ra({showSearch:e}){const{addNode:t,setShowSearch:n,addEdge:r}=$(D(f=>({addNode:f.addNode,setShowSearch:f.setShowSearch,addEdge:f.addEdge}))),[s,o]=m.useState(""),i=Zi(s,300),[a,l]=m.useState([]),[d,c]=m.useState(!1);m.useEffect(()=>{c(!0),he("/search_functions?query="+encodeURIComponent(i)).then(f=>f.json()).then(f=>{l(f)}).finally(()=>c(!1))},[i]);const h=f=>{var _,k,T,V,N,L,z,x;const p=t({data:f,position:{x:e.mouseX,y:e.mouseY},type:"astFunction"}),b=((k=(_=p.data.input)==null?void 0:_.filter(g=>{var v;return g.type===((v=e.connectFrom)==null?void 0:v.handleType)&&!g.hide_handle}))==null?void 0:k[0])??((V=(T=p.data.input)==null?void 0:T.filter(g=>!g.hide_handle))==null?void 0:V[0]),w=((L=(N=p.data.output)==null?void 0:N.filter(g=>{var v;return g.type===((v=e.connectFrom)==null?void 0:v.handleType)&&!g.hide_handle}))==null?void 0:L[0])??((x=(z=p.data.output)==null?void 0:z.filter(g=>!g.hide_handle))==null?void 0:x[0]);if(e.connectFrom){const g={source:e.connectFrom.source??p.id,target:e.connectFrom.target??p.id,sourceHandle:e.connectFrom.sourceHandle??(w==null?void 0:w.id)??fe,targetHandle:e.connectFrom.targetHandle??(b==null?void 0:b.id)??fe};console.log("adding edge",g),r({id:g.source+"_"+g.target+"_"+g.sourceHandle+"_"+g.targetHandle,...g})}n(null)};return u.jsxs(en,{className:"sm:w-[60vw] w-[90vw] max-w-[700px]",style:{position:"fixed",top:"50%",left:"50%",transform:"translate(-50%, -50%)"},children:[u.jsxs(tn,{className:"p-2 text-lg mb-2 relative",children:[u.jsx(be,{placeholder:"Search for functions and nodes",autoFocus:!0,value:s,onChange:f=>o(f.target.value)}),d&&u.jsx(ut,{className:"w-4 h-4 absolute top-2 right-2"})]}),u.jsxs(nn,{className:"h-[60vh] overflow-y-auto px-3",children:[u.jsx(C,{className:"",children:a.map((f,p)=>f&&u.jsxs(O,{className:"justify-between flex-wrap hover:bg-muted py-2 cursor-pointer",onClick:()=>h(f),children:[u.jsx("p",{children:f.name}),u.jsxs(O,{className:"gap-2",children:[u.jsx("p",{className:"text-muted-foreground text-sm",children:f.file_path??f.module}),u.jsx(rn,{children:f.type})]})]},`${f.type}-${f.name}-${p}`))}),a.length===0&&u.jsx("p",{children:"No results found"})]})]})}function sa(e,t){const n=m.useRef(null);return m.useCallback((...r)=>{n.current&&clearTimeout(n.current),n.current=setTimeout(()=>{e(...r)},t)},[e,t])}function oa(){const{screenToFlowPosition:e}=lt(),t=m.useRef(null),n=m.useRef(),r=sa(s=>{n.current={x:s.clientX,y:s.clientY}},100);m.useEffect(()=>{const s=o=>{var i,a,l;if(!["INPUT","TEXTAREA"].includes(o.target.nodeName)&&(o.ctrlKey||o.metaKey)&&["c","v"].includes(o.key)){const{nodes:d,addNode:c,selectedNodeIDs:h,values:f}=$.getState();if(o.key==="c"){o.preventDefault();const p=d.find(b=>b.id===h[0]);p&&(t.current={...p,selected:!1})}else if(o.key==="v"&&t.current){o.preventDefault();const p=e({x:((i=n.current)==null?void 0:i.x)??t.current.position.x+100,y:((a=n.current)==null?void 0:a.y)??t.current.position.y+100}),b={...t.current,position:p};(l=b.data.input)==null||l.forEach(w=>{w.default=f[j("input",b.id,w.id)]??null}),delete b.id,c(b)}}};return window.addEventListener("keydown",s),document.addEventListener("mousemove",r),()=>{window.removeEventListener("keydown",s),document.removeEventListener("mousemove",r)}},[])}const ia=e=>({nodes:e.nodes,edges:e.edges,onNodesChange:e.onNodesChange,onEdgesChange:e.onEdgesChange,setJobStatus:e.setJobStatus,onConnect:e.onConnect,addNode:e.addNode,loadGraph:e.loadGraph,setSelectedNodeIDs:e.setSelectedNodeIDs,setNodes:e.setNodes,setEdges:e.setEdges,setShowSearch:e.setShowSearch}),aa={astFunction:tr};function la(){const{theme:e}=En(),{screenToFlowPosition:t}=lt(),{dropingNode:n,setDropingNode:r}=yn(),{nodes:s,edges:o,onNodesChange:i,onEdgesChange:a,onConnect:l,addNode:d,loadGraph:c,setSelectedNodeIDs:h,setJobStatus:f,setNodes:p,setEdges:b,setShowSearch:w}=$(D(ia));oa(),m.useEffect(()=>{const x=new WebSocket(dt.wsUrl);return x.onopen=()=>{console.log("websocket connected")},x.onmessage=g=>{const v=JSON.parse(g.data);window.dispatchEvent(new CustomEvent("message",{detail:v})),console.log("message",v),v.type==="job_status"&&v.data&&f(v.data)},()=>{x.close()}},[]);const _=m.useCallback(x=>{x.preventDefault(),x.dataTransfer.dropEffect="move"},[]),k=m.useRef(0),T=m.useRef(0),V=m.useCallback(x=>{if(x.preventDefault(),!n)return;const g=t({x:x.clientX,y:x.clientY});d({data:n,position:g,type:"astFunction"}),r(null)},[t,n]),N=m.useCallback(x=>{const g=t({x:x.clientX,y:x.clientY});h([]);const v=Date.now();k.current&&v-k.current<300?w({mouseX:g.x,mouseY:g.y}):v-T.current>300&&w(null),k.current=v},[]),L=m.useCallback((x,g)=>{var Ne,_e,je,Ce,Oe,Ie,Se,Re,Le,Te,Me,Fe,$e,Pe;if(T.current=Date.now(),g.isValid)return;if(!((Ne=g.to)!=null&&Ne.x)||!((_e=g.to)!=null&&_e.x))throw new Error("onConnectEnd No position found!");const v=t({x:(je=g.to)==null?void 0:je.x,y:(Ce=g.to)==null?void 0:Ce.y}),ne=(Oe=g.fromNode)==null?void 0:Oe.data,re=(Ie=g.toNode)==null?void 0:Ie.data,sn=(Re=(Se=ne==null?void 0:ne.output)==null?void 0:Se.find(se=>{var H;return se.id===((H=g.fromHandle)==null?void 0:H.id)}))==null?void 0:Re.type,on=(Te=(Le=re==null?void 0:re.input)==null?void 0:Le.find(se=>{var H;return se.id===((H=g.toHandle)==null?void 0:H.id)}))==null?void 0:Te.type;console.log("onconnectend",g),w({mouseX:v.x,mouseY:v.y,connectFrom:{source:(Me=g.fromNode)==null?void 0:Me.id,sourceHandle:((Fe=g.fromHandle)==null?void 0:Fe.id)??void 0,target:($e=g.toNode)==null?void 0:$e.id,targetHandle:((Pe=g.toHandle)==null?void 0:Pe.id)??void 0,handleType:sn??on}})},[]),z=x=>{const{nodes:g,edges:v}=Qi(s,o,x);p([...g]),b([...v])};return u.jsxs(un,{nodes:s,nodeTypes:aa,edges:o,colorMode:e,onNodesChange:i,onEdgesChange:a,onDrop:V,onDragOver:_,onConnect:l,fitView:!0,fitViewOptions:{padding:.2},defaultEdgeOptions:{style:{strokeWidth:2,stroke:"#9049CF"}},onNodeClick:(x,g)=>{h([g.id])},onPaneClick:N,onConnectEnd:L,zoomOnDoubleClick:!1,children:[u.jsx(hn,{}),u.jsxs(fn,{orientation:"horizontal",children:[u.jsx(Y,{onClick:Ae.undo,title:"Undo",children:u.jsx(On,{size:16,className:"!fill-[none] !max-w-4 !max-h-4"})}),u.jsx(Y,{onClick:Ae.redo,title:"Redo",children:u.jsx(In,{size:16,className:"!fill-[none] !max-w-4 !max-h-4"})}),u.jsx(Y,{onClick:()=>z("LR"),title:"Layout nodes horizontally",children:u.jsx(Ge,{size:16,className:"!fill-[none] !max-w-4 !max-h-4"})}),u.jsx(Y,{onClick:()=>z("TB"),title:"Layout nodes vertically",children:u.jsx(Ge,{size:16,className:"!fill-[none] !max-w-4 !max-h-4 rotate-90"})})]})]})}function va(){const{showSearch:e}=$(D(t=>({showSearch:t.showSearch})));return u.jsxs(cn,{children:[u.jsx(la,{}),e&&u.jsx(ra,{showSearch:e})]})}export{la as FlowCanvas,va as default};
