#!/usr/bin/env node
import'./chunk-5TAVYPOV.js';import*as d from'path';import*as f from'fs';import*as y from'js-yaml';import o from'chalk';import h from'ora';import v from'open';async function x(m,r={}){let w=m?d.resolve(m):process.cwd(),c=parseInt(r.port||"42196",10),s=parseInt(r.uiPort||"42195",10),i=r.config||d.join(w,"portal.yaml");console.log(o.magenta(`
\u{1F6AA} Portal Viewer
`));let a=h("Starting Portal Viewer...").start();try{f.existsSync(i)||(a.warn(`No portal.yaml found at ${o.gray(i)}`),console.log(o.gray(`   The viewer will start but won't have any predefined gates.
`)));let t;try{t=(await import('@a-company/portal-viewer')).ViewerServer;}catch{a.fail("Portal Viewer package not found"),console.log(""),console.log(o.yellow("To use the Portal Viewer, install the package:")),console.log(o.cyan("  npm install @a-company/portal-viewer")),console.log(""),console.log(o.gray("Or build from source in the monorepo:")),console.log(o.gray("  cd packages/portal/viewer && npm run build")),process.exit(1);}await new t({port:c,uiPort:s,configPath:i}).start(),a.succeed("Portal Viewer started"),console.log(""),console.log(o.blue("   \u{1F310} UI:        ")+o.cyan.bold(`http://localhost:${s}`)),console.log(o.blue("   \u{1F50C} WebSocket: ")+o.cyan(`ws://localhost:${c}`)),console.log(o.blue("   \u{1F4CB} Config:    ")+o.gray(i)),console.log(""),console.log(o.gray("   Connect your app's Portal SDK to the WebSocket URL above.")),console.log(o.gray("   Press Ctrl+C to stop.")),console.log(""),r.open!==!1&&await v(`http://localhost:${s}`),await new Promise(()=>{});}catch(t){a.fail("Failed to start Portal Viewer"),console.error(o.red(t.message)),process.exit(1);}}async function $(m,r={}){m||(console.log(o.red("Error: Session path required")),console.log(""),console.log("Usage: paradigm portal report <session.json> [options]"),console.log(""),console.log("Options:"),console.log("  --format <format>  Output format: json, markdown, slack, discord"),console.log("  --output <path>    Output file path"),process.exit(1));let w=r.format||"markdown",c=r.output;console.log(o.magenta(`
\u{1F4CA} Portal Report Generator
`));let s=h("Generating report...").start();try{let i=f.readFileSync(m,"utf-8"),a=JSON.parse(i),t;try{t=await import('@a-company/portal-viewer/session');}catch{s.fail("Portal Viewer package not found"),console.log(o.yellow("Install @a-company/portal-viewer to use the reporter.")),process.exit(1);}let l=t.generateReport(a,[]),e;switch(w){case "json":e=JSON.stringify(l,null,2);break;case "markdown":e=t.formatMarkdown(l);break;case "slack":e=JSON.stringify(t.formatSlack(l),null,2);break;case "discord":e=JSON.stringify(t.formatDiscord(l),null,2);break;default:e=t.formatMarkdown(l);}c?(f.writeFileSync(c,e,"utf-8"),s.succeed(`Report saved to ${o.cyan(c)}`)):(s.succeed("Report generated"),console.log(""),console.log(e));}catch(i){s.fail("Failed to generate report"),console.error(o.red(i.message)),process.exit(1);}}async function R(m,r={}){let w=m?d.resolve(m):process.cwd(),c=r.config||d.join(w,"portal.yaml"),s=r.format||"json";f.existsSync(c)||(console.error(o.red(`portal.yaml not found at ${c}`)),process.exit(1));let i=f.readFileSync(c,"utf8"),a=y.load(i),t=a.gates||{},l=a.routes||{},e;switch(s){case "csv":{let g=["route,method,gates"];for(let[p,n]of Object.entries(l)){let u=p.split(" "),k=u.length>1?u[0]:"ANY",S=u.length>1?u.slice(1).join(" "):p,b=Array.isArray(n)?n.join(";"):String(n);g.push(`${S},${k},"${b}"`);}e=g.join(`
`);break}case "markdown":{let g=["# Portal Configuration","",`**Version:** ${a.version||"unknown"}`,"","## Gates","","| Gate | Description | Check |","|------|-------------|-------|"];for(let[p,n]of Object.entries(t)){let u=n;g.push(`| ^${p} | ${u.description||""} | \`${u.check||""}\` |`);}g.push("","## Routes","","| Route | Gates |","|-------|-------|");for(let[p,n]of Object.entries(l)){let u=Array.isArray(n)?n.join(", "):String(n);g.push(`| ${p} | ${u} |`);}e=g.join(`
`);break}default:{let g={version:a.version,exportedAt:new Date().toISOString(),gates:Object.entries(t).map(([p,n])=>({id:p,...n})),routes:Object.entries(l).map(([p,n])=>({route:p,gates:n}))};e=JSON.stringify(g,null,2);break}}r.output?(f.writeFileSync(r.output,e,"utf8"),console.log(o.green(`Exported portal config to ${o.cyan(r.output)} (${s})`))):console.log(e);}export{R as portalExportCommand,$ as portalReportCommand,x as portalWatchCommand};