#!/usr/bin/env node
import {b}from'./chunk-LBQBWIEX.js';import {a}from'./chunk-LKAT7IAK.js';import*as r from'fs';import*as i from'path';import e from'chalk';import W from'ora';import*as u from'js-yaml';async function v(l={}){let c=process.cwd(),o=i.join(c,".paradigm-workspace");console.log(e.blue(`
\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510`)),console.log(e.blue("\u2502")+e.white.bold("  paradigm workspace init                         ")+e.blue("\u2502")),console.log(e.blue("\u2502")+e.gray("  Create a multi-project workspace               ")+e.blue("\u2502")),console.log(e.blue(`\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518
`));let n=a.command("workspace-init").start("Creating workspace",{cwd:c});if(r.existsSync(o)&&!l.force){console.log(e.yellow(`  .paradigm-workspace already exists. Use --force to overwrite.
`)),n.success("Workspace already exists");return}let s=c,t=r.readdirSync(s,{withFileTypes:true}),a$1=[];for(let p of t){if(!p.isDirectory()||p.name.startsWith(".")||p.name==="node_modules")continue;let g=i.join(s,p.name),k=r.existsSync(i.join(g,".paradigm")),y=r.existsSync(i.join(g,".purpose"));if(k||y){let w=j(p.name,g);a$1.push({name:p.name,path:`./${p.name}`,...w&&{role:w}});}}if(a$1.length===0){console.log(e.yellow("  No sibling projects with .paradigm/ or .purpose found.")),console.log(e.gray(`  Create .purpose files in sibling directories first.
`)),n.error("No projects found");return}let d={version:"1.0",name:l.name||i.basename(c),members:a$1},f=u.dump(d,{indent:2,lineWidth:120,noRefs:true,sortKeys:false,quotingType:'"'});r.writeFileSync(o,f,"utf8"),console.log(e.green(`  Created .paradigm-workspace with ${a$1.length} members:
`));for(let p of a$1){let g=p.role?e.gray(` [${p.role}]`):"";console.log(e.white(`    ${p.name}`)+g+e.gray(` \u2192 ${p.path}`));}console.log(""),console.log(e.white("  Next steps:")),console.log(e.gray("  \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500")),console.log(e.white("  1. ")+e.gray("In each member project, add to ")+e.cyan(".paradigm/config.yaml")+e.gray(":")),console.log(e.gray("     ")+e.cyan("workspace: ../.paradigm-workspace")),console.log(e.white("  2. ")+e.gray("Run ")+e.cyan("paradigm workspace reindex")+e.gray(" to build all indices")),console.log(e.white("  3. ")+e.gray("Use ")+e.cyan("includeWorkspace: true")+e.gray(" in MCP tools for cross-project search")),console.log(""),n.success("Workspace created",{members:a$1.length});}async function I(l={}){let c=process.cwd(),o=a.command("workspace-status").start("Getting workspace status"),n=S(c);if(!n){console.log(e.yellow(`
  No .paradigm-workspace found in current or parent directory.
`)),console.log(e.gray("  Run `paradigm workspace init` to create one.\n")),o.error("No workspace found");return}let s=u.load(r.readFileSync(n,"utf8")),t=i.dirname(n);if(l.json){let a=s.members.map(m=>x(t,m));console.log(JSON.stringify({workspace:s.name,members:a},null,2)),o.success("Workspace status (JSON)");return}console.log(e.blue(`
  Workspace: ${e.white.bold(s.name)}`)),console.log(e.gray(`  File: ${n}`)),console.log(e.gray(`  \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
`));for(let a of s.members){let m=x(t,a),d=m.hasIndex?e.green("\u2713"):e.yellow("\u25CB"),f=a.role?e.gray(` [${a.role}]`):"";console.log(`  ${d} ${e.white.bold(a.name)}${f}`),console.log(e.gray(`    Path: ${a.path}`)),m.hasIndex?(console.log(e.gray(`    Symbols: ${m.symbolCount}`)),console.log(e.gray(`    Last indexed: ${m.lastIndexed||"unknown"}`))):console.log(e.yellow("    No scan-index.json \u2014 run paradigm scan in this project")),console.log("");}o.success("Workspace status shown",{members:s.members.length});}async function P(l={}){let c=process.cwd(),o=a.command("workspace-reindex").start("Reindexing workspace"),n=S(c);if(!n){console.log(e.yellow(`
  No .paradigm-workspace found.
`)),o.error("No workspace found");return}let s=u.load(r.readFileSync(n,"utf8")),t=i.dirname(n);console.log(e.blue(`
  Reindexing workspace: ${e.white.bold(s.name)}
`));let a$1=W(),m=0;for(let d of s.members){let f=i.resolve(t,d.path);if(!r.existsSync(f)){l.quiet||console.log(e.yellow(`  \u26A0 ${d.name}: directory not found at ${d.path}`));continue}l.quiet||a$1.start(`Indexing ${d.name}...`);try{await b(f,{quiet:!0});let p=i.join(f,".paradigm","scan-index.json"),g=0;if(r.existsSync(p))try{let y=JSON.parse(r.readFileSync(p,"utf8")).$meta;y?.sources&&(g=(y.sources.purposeFiles||0)+(y.sources.portalFiles||0));}catch{}m+=g,l.quiet||a$1.succeed(e.green(`${d.name}: indexed`));}catch(p){l.quiet||a$1.fail(e.red(`${d.name}: ${p.message}`));}}console.log(e.gray(`
  \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500`)),console.log(e.green("  All members reindexed.")),console.log(""),o.success("Workspace reindexed",{members:s.members.length,totalSymbols:m});}function S(l){let c=l;for(let n=0;n<5;n++){let s=i.join(c,".paradigm-workspace");if(r.existsSync(s))return s;let t=i.dirname(c);if(t===c)break;c=t;}let o=i.join(l,".paradigm","config.yaml");if(r.existsSync(o))try{let n=u.load(r.readFileSync(o,"utf8"));if(typeof n?.workspace=="string"){let s=i.resolve(l,n.workspace);if(r.existsSync(s))return s}}catch{}return null}function j(l,c){let o=l.toLowerCase();if(o.includes("api")||o.includes("backend")||o.includes("server"))return "api";if(o.includes("client")||o.includes("frontend")||o.includes("web")||o.includes("app"))return "client";if(o.includes("shared")||o.includes("common")||o.includes("core"))return "shared";if(o.includes("lib")||o.includes("packages"))return "lib";if(o.includes("service")||o.includes("svc"))return "service";let n=i.join(c,"package.json");if(r.existsSync(n))try{let s=JSON.parse(r.readFileSync(n,"utf8")),t={...s.dependencies,...s.devDependencies};if(t.react||t.vue||t["@angular/core"]||t.svelte)return "client";if(t.express||t.fastify||t.koa||t.hono)return "api"}catch{}}function x(l,c){let o=i.resolve(l,c.path),n=i.join(o,".paradigm","scan-index.json"),s={name:c.name,path:c.path,role:c.role,exists:r.existsSync(o),hasIndex:false,symbolCount:0,lastIndexed:null};if(s.exists&&r.existsSync(n)){s.hasIndex=true;try{let t=JSON.parse(r.readFileSync(n,"utf8")),a=t.$meta;if(a){s.lastIndexed=a.generatedAt||null;for(let m of ["components","flows","gates","signals","aspects"]){let d=t[m];d&&typeof d=="object"&&(s.symbolCount+=Object.keys(d).length);}}}catch{}}return s}export{v as a,I as b,P as c,j as d};