#!/usr/bin/env node
import {a,e,b,d}from'./chunk-WR6D3SC6.js';import _ from'chalk';import*as g from'fs';import*as $ from'path';async function q(c,l){let o=c||process.cwd(),e$1=parseInt(l.port||"3841",10),h=l.open!==false;a(_.cyan(`
Starting Symbol Graph...
`));try{let{startGraphServer:a$1}=await import('./graph-server-TBHHBFOM.js');e(`Project: ${o}`),e(`Port: ${e$1}`),a(""),await a$1({port:e$1,projectDir:o,open:h}),b(`Symbol Graph is running at http://localhost:${e$1}`),e(`
Press Ctrl+C to stop
`),await new Promise(()=>{});}catch(a){a.code==="EADDRINUSE"?(d(`Port ${e$1} is already in use.`),e(`Try: paradigm graph --port ${e$1+1}
`)):d(`Failed to start Symbol Graph: ${a}`),process.exit(1);}}var F=".paradigm/graphs",N={component:"#",flow:"$",gate:"^",signal:"!",aspect:"~"},P=200,j=60,x=20,I=40,R=50,B=60,L={components:"component",flows:"flow",gates:"gate",signals:"signal",aspects:"aspect"};function T(c){let l=$.join(c,".paradigm","scan-index.json");if(!g.existsSync(l))return [];let o=JSON.parse(g.readFileSync(l,"utf8")),e=[];for(let[h,a]of Object.entries(L)){let m=o[h];if(!(!m||typeof m!="object"))for(let[p,f]of Object.entries(m)){let d=f;e.push({id:p,name:p,category:a,prefix:N[a]||"#",description:d.description,path:d.path});}}return e}function A(c,l){let o=c.replace(/^[#$^!~]/,"");return l.find(e=>e.id===o||e.name===o||e.id===c||e.name===c)}function H(c,l,o,e,h="Generated Graph"){let a=T(c),m=l&&l.length>0?l.map(t=>A(t,a)).filter(Boolean):a,p=[],f=[],d=new Map,b=new Set,G=0;if(o&&o.length>0)for(let t of o){let i=`group-${Date.now()}-${Math.random().toString(36).slice(2,6)}`;d.set(t.label,i);let n=t.symbols.map(y=>A(y,m)).filter(Boolean),r=Math.max(Math.ceil(Math.sqrt(n.length)),1),M=Math.max(Math.ceil(n.length/r),1);for(let y=0;y<n.length;y++){let w=n[y],v=y%r,C=Math.floor(y/r);p.push({id:`sym-${w.id}`,type:"symbolNode",position:{x:I+v*(P+x),y:R+I+C*(j+x)},parentId:i,data:{type:"symbol",symbol:w,label:`${N[w.category]||"#"}${w.name}`}}),b.add(w.id);}let D=I*2+r*P+(r-1)*x,k=R+I*2+M*j+(M-1)*x;p.unshift({id:i,type:"groupNode",position:{x:G,y:0},style:{width:D,height:k},data:{type:"group",label:t.label}}),G+=D+B;}let s=m.filter(t=>!b.has(t.id));if(s.length>0){let t=o&&o.length>0?400:0,i=Math.max(Math.ceil(Math.sqrt(s.length)),1);for(let n=0;n<s.length;n++){let r=s[n],M=n%i,D=Math.floor(n/i);p.push({id:`sym-${r.id}`,type:"symbolNode",position:{x:M*(P+x),y:t+D*(j+x)},data:{type:"symbol",symbol:r,label:`${N[r.category]||"#"}${r.name}`}});}}if(e&&e.length>0)for(let t of e){let i=d.get(t.source),n=d.get(t.target);i&&n&&f.push({id:`e-${i}-${n}`,source:i,target:n,type:"default",label:t.label,data:{label:t.label}});}return {version:"1.0",name:h,projectId:$.basename(c),lastModified:new Date().toISOString(),nodes:p,edges:f}}async function K(c,l,o){let e$1=l||process.cwd(),h=c.toLowerCase().replace(/[^a-z0-9-]+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"");try{let a=o.symbols?o.symbols.split(",").map(s=>s.trim()):void 0,m=o.group?.map(s=>{let t=s.indexOf(":");return t===-1&&(d(`Invalid group format: "${s}". Expected "Label:#sym1,#sym2"`),process.exit(1)),{label:s.slice(0,t),symbols:s.slice(t+1).split(",").map(i=>i.trim())}}),p=o.link?.map(s=>{let t=s.indexOf(">");t===-1&&(d(`Invalid link format: "${s}". Expected "Source>Target:label"`),process.exit(1));let i=s.slice(0,t),n=s.slice(t+1),r=n.indexOf(":");return {source:i,target:r===-1?n:n.slice(0,r),label:r===-1?void 0:n.slice(r+1)}}),f=H(e$1,a,m,p,c),d$1=JSON.stringify(f,null,2),b$1=$.join(e$1,F);g.existsSync(b$1)||g.mkdirSync(b$1,{recursive:!0});let G=$.join(b$1,`${h}.graph.json`);g.writeFileSync(G,d$1,"utf8"),b(`Graph saved to ${G}`),e(`${f.nodes.length} nodes, ${f.edges.length} edges, ${(d$1.length/1024).toFixed(1)} KB`),e(`
View: paradigm graph`);}catch(a){d(`Failed to generate graph: ${a.message}`),process.exit(1);}}export{q as a,H as b,K as c};