#!/usr/bin/env node
import {Router}from'express';import*as e from'fs';import*as j from'path';function b(h){let c=Router();return c.get("/",(u,p)=>{let s=j.join(h,".paradigm","scan-index.json");if(!e.existsSync(s)){p.status(404).json({error:"scan-index.json not found",hint:"Run `paradigm scan` to generate the symbol index."});return}try{let n=e.readFileSync(s,"utf-8"),o=JSON.parse(n),t={components:"component",features:"component",flows:"flow",state:"component",gates:"gate",signals:"signal",aspects:"aspect",screens:"component"},g={component:"#",flow:"$",gate:"^",signal:"!",aspect:"~"},d=[],f=new Set;for(let[y,l]of Object.entries(t)){let r=o[y];if(!r||typeof r!="object")continue;let a=Array.isArray(r)?r:Object.values(r);for(let i of a){let m=i.name||i.id||"";!m||f.has(m)||(f.add(m),d.push({id:m,name:m,category:l,prefix:g[l]||"#",description:i.description||"",path:i.path||i.file||"",tags:i.tags||[],related:i.related||[]}));}}p.json({symbols:d,meta:{total:d.length,projectDir:h,generatedAt:new Date().toISOString()}});}catch(n){p.status(500).json({error:"Failed to read scan-index.json",details:n.message});}}),c}var A=".paradigm/graphs";function P(h){let c=Router(),u=j.join(h,A);return c.get("/",(p,s)=>{if(!e.existsSync(u)){s.json({graphs:[]});return}try{let o=e.readdirSync(u).filter(t=>t.endsWith(".graph.json")).map(t=>{let g=j.join(u,t),d=e.statSync(g),f=t.replace(/\.graph\.json$/,""),y=f,l=0,r=0;try{let a=JSON.parse(e.readFileSync(g,"utf8"));y=a.name||f,l=Array.isArray(a.nodes)?a.nodes.length:0,r=Array.isArray(a.edges)?a.edges.length:0;}catch{}return {slug:f,file:t,name:y,nodes:l,edges:r,size:d.size,modified:d.mtime.toISOString()}});o.sort((t,g)=>g.modified.localeCompare(t.modified)),s.json({graphs:o});}catch(n){s.status(500).json({error:"Failed to list graphs",details:n.message});}}),c.get("/:slug",(p,s)=>{let n=p.params.slug,o=j.join(u,`${n}.graph.json`);if(!e.existsSync(o)){s.status(404).json({error:`Graph "${n}" not found`});return}try{let t=e.readFileSync(o,"utf8");s.type("application/json").send(t);}catch(t){s.status(500).json({error:"Failed to read graph",details:t.message});}}),c}export{b as a,P as b};