#!/usr/bin/env node
import {a,b,c as c$1}from'./chunk-XMAV5AG6.js';import'./chunk-XHJ27CER.js';import l from'express';import*as c from'path';import*as m from'fs';import {fileURLToPath}from'url';import i from'chalk';var $=fileURLToPath(import.meta.url),g=c.dirname($),p={component(s){let o=i.magenta(`#${s}`);return {info:(t,e)=>{let r=e?i.gray(` ${Object.entries(e).map(([n,a])=>`${n}=${a}`).join(" ")}`):"";console.log(`${i.blue("i")} ${o} ${t}${r}`);},success:(t,e)=>{let r=e?i.gray(` ${Object.entries(e).map(([n,a])=>`${n}=${a}`).join(" ")}`):"";console.log(`${i.green("+")} ${o} ${t}${r}`);},warn:(t,e)=>{let r=e?i.gray(` ${Object.entries(e).map(([n,a])=>`${n}=${a}`).join(" ")}`):"";console.log(`${i.yellow("!")} ${o} ${t}${r}`);},error:(t,e)=>{let r=e?i.gray(` ${Object.entries(e).map(([n,a])=>`${n}=${a}`).join(" ")}`):"";console.error(`${i.red("x")} ${o} ${t}${r}`);}}}};function y(s){let o=l();o.use(l.json()),o.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();}),o.use("/api/lore",a(s.projectDir)),o.use("/api/info",b(s.projectDir)),o.use("/api/sessions",c$1(s.projectDir)),o.get("/api/health",(e,r)=>{r.json({status:"ok",timestamp:new Date().toISOString()});});let t=c.join(g,"..","lore-ui","dist");return m.existsSync(t)||(t=c.join(g,"..","..","lore-ui","dist")),m.existsSync(t)?(o.use(l.static(t)),o.get("{*path}",(e,r)=>{e.path.startsWith("/api")||r.sendFile(c.join(t,"index.html"));})):o.get("/",(e,r)=>{r.send(`
        <html>
        <head><title>Paradigm Lore</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 Lore</h1>
            <p style="color:#94a3b8">UI not built yet. Run <code style="background:#1e293b;padding:4px 8px;border-radius:4px">cd lore-ui && npx vite build</code></p>
            <p style="color:#94a3b8">API available at <a href="/api/lore" style="color:#7dd3fc">/api/lore</a></p>
          </div>
        </body>
        </html>
      `);}),o}async function O(s){let o=y(s);return p.component("lore-server").info("Starting server",{port:s.port}),p.component("lore-server").info("Project directory",{path:s.projectDir}),new Promise((t,e)=>{o.listen(s.port,()=>{p.component("lore-server").success("Server running",{url:`http://localhost:${s.port}`}),s.open&&import('open').then(n=>{n.default(`http://localhost:${s.port}`),p.component("lore-server").info("Opened browser");}).catch(()=>{p.component("lore-server").warn("Could not open browser automatically");}),t();}).on("error",n=>{n.code==="EADDRINUSE"?p.component("lore-server").error("Port already in use",{port:s.port}):p.component("lore-server").error("Server error",{error:n.message}),e(n);});})}
export{y as createLoreApp,O as startLoreServer};