#!/usr/bin/env node
import'./chunk-SHD27BQX.js';import {a}from'./chunk-LKAT7IAK.js';import'./chunk-5TAVYPOV.js';import*as g from'fs';import*as u from'path';import l from'chalk';import*as m from'js-yaml';import {execSync}from'child_process';function y(e){return Object.entries(e.flows).map(([s,t])=>({id:s,...t}))}function w(e){let s=u.join(e,".paradigm","flows.yaml");if(!g.existsSync(s))return null;try{let t=g.readFileSync(s,"utf-8");return m.load(t)}catch{return null}}function b(e){let s=[];try{let t=execSync(`find "${e}" -name ".purpose" -not -path "*/node_modules/*" -not -path "*/.git/*" 2>/dev/null || true`,{encoding:"utf-8"});for(let i of t.split(`
`).filter(Boolean))try{let n=g.readFileSync(i,"utf-8").match(/flows:\s*\n([\s\S]*?)(?=\n[a-z_]+:|$)/);if(n){let r=m.load(`flows:
${n[1]}`);if(r&&typeof r=="object"&&"flows"in r){let c=r.flows;for(let[a,d]of Object.entries(c))s.push({id:a,...d,definedIn:u.relative(e,i)});}}}catch{}}catch{}return s}function F(e){let s=[],t=w(e);if(t){let o=y(t);for(let n of o)n.definedIn=".paradigm/flows.yaml",s.push(n);}let i=b(e);for(let o of i)s.some(n=>n.id===o.id)||s.push(o);return s}function $(e){let s=[];s.push("```mermaid"),s.push("flowchart TD"),s.push(`  START([${p(e.trigger)}])`);let t="START";for(let i=0;i<e.steps.length;i++){let o=e.steps[i],n=`S${i}`,r=p(o.symbol);switch(o.type){case "gate":{let c=o;if(s.push(`  ${n}{${r}}`),s.push(`  ${t} --> ${n}`),c.failResponse||o.errorSignal){let a=`DENY${i}`,d=c.failResponse||o.errorSignal||"Denied";s.push(`  ${a}[/${p(d)}/]`),s.push(`  ${n} -->|deny| ${a}`);}break}case "action":s.push(`  ${n}[${r}]`),s.push(`  ${t} -->|${o.optional?"optional":"allow"}| ${n}`);break;case "signal":s.push(`  ${n}([${r}])`),s.push(`  ${t} --> ${n}`);break}t=n;}e.successSignal&&(s.push(`  SUCCESS([${p(e.successSignal)}])`),s.push(`  ${t} --> SUCCESS`)),s.push(""),s.push("  classDef gate fill:#f9d71c,stroke:#333,color:#000"),s.push("  classDef action fill:#4a90d9,stroke:#333,color:#fff"),s.push("  classDef signal fill:#50c878,stroke:#333,color:#fff");for(let i=0;i<e.steps.length;i++){let o=e.steps[i];s.push(`  class S${i} ${o.type}`);}return s.push("```"),s.join(`
`)}function p(e){return e.replace(/"/g,'\\"').replace(/[[\]{}()]/g,"")}async function L(e,s){let t=process.cwd();w(t)||(console.log(l.red(`
\u274C No flows.yaml found`)),console.log(l.gray(`Create .paradigm/flows.yaml to define flows.
`)),process.exit(1));let o=F(t),n=e.startsWith("$")?e:`$${e}`,r=o.find(a=>a.id===n||a.id===e);if(!r){console.log(l.red(`
\u274C Flow not found: ${e}`)),console.log(l.gray(`
Available flows:`));for(let a of o)console.log(l.gray(`  ${a.id} \u2014 ${a.name}`));console.log(""),process.exit(1);}a.command("flow-diagram").info("Generating Mermaid diagram",{flowId:r.id});let c=$(r);if(s.output){let a=u.resolve(t,s.output);g.writeFileSync(a,c,"utf-8"),console.log(l.green(`
\u2713 Diagram written to ${u.relative(t,a)}
`));}else console.log(""),console.log(l.blue(`Flow: ${r.name} (${r.id})`)),console.log(l.gray(`Trigger: ${r.trigger}`)),console.log(""),console.log(c),console.log("");}export{L as flowDiagramCommand};