#!/usr/bin/env node
import {a,b}from'./chunk-Y6KXTQBG.js';import'./chunk-5TAVYPOV.js';import l from'express';import*as c from'path';import*as h from'fs';import {fileURLToPath}from'url';import a$1 from'chalk';var f=fileURLToPath(import.meta.url),d=c.dirname(f),p={component(s){let t=a$1.magenta(`#${s}`);return {info:(o,e)=>{let r=e?a$1.gray(` ${Object.entries(e).map(([n,i])=>`${n}=${i}`).join(" ")}`):"";console.log(`${a$1.blue("i")} ${t} ${o}${r}`);},success:(o,e)=>{let r=e?a$1.gray(` ${Object.entries(e).map(([n,i])=>`${n}=${i}`).join(" ")}`):"";console.log(`${a$1.green("+")} ${t} ${o}${r}`);},warn:(o,e)=>{let r=e?a$1.gray(` ${Object.entries(e).map(([n,i])=>`${n}=${i}`).join(" ")}`):"";console.log(`${a$1.yellow("!")} ${t} ${o}${r}`);},error:(o,e)=>{let r=e?a$1.gray(` ${Object.entries(e).map(([n,i])=>`${n}=${i}`).join(" ")}`):"";console.error(`${a$1.red("x")} ${t} ${o}${r}`);}}}};function y(s){let t=l();t.use(l.json()),t.use((e,r,n)=>{if(r.header("Access-Control-Allow-Origin","*"),r.header("Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS"),r.header("Access-Control-Allow-Headers","Content-Type"),e.method==="OPTIONS"){r.sendStatus(204);return}n();}),t.use("/api/symbols",a(s.projectDir)),t.use("/api/graphs",b(s.projectDir)),t.get("/api/health",(e,r)=>{r.json({status:"ok",timestamp:new Date().toISOString()});});let o=c.join(d,"..","graph-ui","dist");return h.existsSync(o)||(o=c.join(d,"..","..","graph-ui","dist")),h.existsSync(o)?(t.use(l.static(o)),t.get("{*path}",(e,r)=>{e.path.startsWith("/api")||r.sendFile(c.join(o,"index.html"));})):t.get("/",(e,r)=>{r.send(`
        <html>
        <head><title>Paradigm Graph</title></head>
        <body style="background:#0a0a0f;color:#e2e8f0;font-family:system-ui;display:flex;align-items:center;justify-content:center;height:100vh;margin:0">
          <div style="text-align:center">
            <h1>Paradigm Graph</h1>
            <p style="color:#94a3b8">UI not built yet. Run <code style="background:#1e293b;padding:4px 8px;border-radius:4px">cd graph-ui && npx vite build</code></p>
            <p style="color:#94a3b8">API available at <a href="/api/symbols" style="color:#7dd3fc">/api/symbols</a></p>
          </div>
        </body>
        </html>
      `);}),t}async function x(s){let t=y(s);return p.component("graph-server").info("Starting server",{port:s.port}),p.component("graph-server").info("Project directory",{path:s.projectDir}),new Promise((o,e)=>{t.listen(s.port,()=>{p.component("graph-server").success("Server running",{url:`http://localhost:${s.port}`}),s.open&&import('open').then(n=>{n.default(`http://localhost:${s.port}`),p.component("graph-server").info("Opened browser");}).catch(()=>{p.component("graph-server").warn("Could not open browser automatically");}),o();}).on("error",n=>{n.code==="EADDRINUSE"?p.component("graph-server").error("Port already in use",{port:s.port}):p.component("graph-server").error("Server error",{error:n.message}),e(n);});})}
export{y as createGraphApp,x as startGraphServer};