#!/usr/bin/env node
import {d,c,e as e$1}from'./chunk-HMQ5BHP2.js';import {e as e$2,d as d$1}from'./chunk-JQKKVAAN.js';import'./chunk-5TAVYPOV.js';import*as e from'fs';import*as t from'path';import {fileURLToPath}from'url';import a from'chalk';import z from'ora';var A=["scan","logger","migrate"];function j(){let l=fileURLToPath(import.meta.url),n=t.dirname(l),s=[t.join(n,"..","..","templates","paradigm"),t.join(n,"..","templates","paradigm")];for(let o of s)if(e.existsSync(o))return o;return t.join(n,"..","templates","paradigm")}async function W(l,n){let s=l?t.resolve(l):process.cwd(),o=t.basename(s),c$1=z();if(console.log(a.yellow("\n  `paradigm upgrade` is deprecated. Use `paradigm migrate` instead.\n")),console.log(a.blue(`
\u{1F504} Paradigm Upgrade
`)),n.fromHorizon){let r=await M(s,o,n,c$1);console.log(a.blue(`
\u{1F4CB} Migration Summary
`));let u=r.status==="added"||r.status==="updated"?a.green("\u2713"):r.status==="skipped"?a.yellow("\u25CB"):a.red("\u2717");console.log(`  ${u} ${a.bold(r.feature)}: ${r.message}`),console.log(),(r.status==="updated"||r.status==="added")&&(console.log(a.blue(`Next steps:
`)),console.log(a.gray("  \u2022 Verify the migration:")),console.log(a.cyan(`    paradigm doctor
`)),console.log(a.gray("  \u2022 Regenerate IDE files:")),console.log(a.cyan(`    paradigm sync
`)));return}let i=[];if(n.all)i=["migrate","scan","logger"];else if(n.features&&n.features.length>0)i=n.features.filter(r=>A.includes(r));else {console.log(a.yellow(`No features specified. Available upgrades:
`)),console.log(a.cyan("  migrate")+a.gray("      - Migrate from .paradigm file to .paradigm/ directory")),console.log(a.cyan("  scan")+a.gray("         - Add visual discovery (paradigm probe) support")),console.log(a.cyan("  logger")+a.gray("       - Add Paradigm logger specification")),console.log(a.cyan("  all")+a.gray("          - Apply all available upgrades")),console.log(a.cyan("  --from-horizon")+a.gray(" - Migrate from Horizon to Paradigm")),console.log(),console.log(a.gray("Usage:")),console.log(a.gray("  paradigm upgrade --features scan")),console.log(a.gray("  paradigm upgrade --features logger")),console.log(a.gray("  paradigm upgrade --all")),console.log(a.gray("  paradigm upgrade --from-horizon")),console.log();return}let g=[],d$1=t.join(s,".paradigm"),f=e.existsSync(d$1),y=f&&e.statSync(d$1).isFile(),h=f&&e.statSync(d$1).isDirectory();(i.includes("migrate")||y&&n.all)&&(y?g.push(await I(s,o,n,c$1)):h?g.push({feature:"migrate",status:"skipped",message:"Already using .paradigm/ directory format"}):g.push({feature:"migrate",status:"error",message:"No .paradigm found to migrate"}));let S=t.join(s,".paradigm");if(!(e.existsSync(S)&&e.statSync(S).isDirectory())&&!n.dryRun&&!y){console.log(a.yellow("\u26A0\uFE0F  No .paradigm/ directory found.")),console.log(a.gray("   Run `paradigm init` first to initialize Paradigm.\n"));return}for(let r of i)if(r!=="migrate")switch(r){case "scan":g.push(await E(s,o,n,c$1));break;case "logger":g.push(await L(s,n,c$1));break}if(!n.dryRun&&g.some(r=>r.status==="added"||r.status==="updated")){c$1.start("Re-syncing IDE files...");let r=d(s);if(r){let u=c(s);u.detected?e$1(s,u.detected,r,true).success?c$1.succeed(`IDE files synced (${u.detected})`):c$1.warn("Could not sync IDE files"):c$1.info("No IDE detected, run `paradigm sync` to generate IDE files");}}console.log(a.blue(`
\u{1F4CB} Upgrade Summary
`));for(let r of g){let u=r.status==="added"||r.status==="updated"?a.green("\u2713"):r.status==="skipped"?a.yellow("\u25CB"):a.red("\u2717");console.log(`  ${u} ${a.bold(r.feature)}: ${r.message}`);}console.log();let m=g.filter(r=>r.status==="added"||r.status==="updated");m.length>0&&(console.log(a.blue(`Next steps:
`)),m.some(r=>r.feature==="scan")&&(console.log(a.gray("  \u2022 Generate the scan index:")),console.log(a.cyan(`    paradigm index
`))),m.some(r=>r.feature==="logger")&&(console.log(a.gray("  \u2022 Review the logger spec:")),console.log(a.cyan(`    .paradigm/specs/logger.md
`))),console.log(a.gray("  \u2022 Verify setup:")),console.log(a.cyan(`    paradigm doctor
`)));}async function I(l,n,s,o){let c=t.join(l,".paradigm"),i=t.join(l,".paradigm"),g=j();if(s.dryRun)return {feature:"migrate",status:"updated",message:"Would migrate .paradigm file to .paradigm/ directory"};o.start("Migrating .paradigm file to directory...");try{let d=e.readFileSync(c,"utf8"),f=null;try{f=e$2(d);}catch{}let y=t.join(l,".paradigm.backup");if(e.copyFileSync(c,y),e.unlinkSync(c),e.mkdirSync(i,{recursive:!0}),e.mkdirSync(t.join(i,"specs"),{recursive:!0}),e.mkdirSync(t.join(i,"docs"),{recursive:!0}),e.mkdirSync(t.join(i,"prompts"),{recursive:!0}),e.existsSync(g))k(g,i,n);else {let p=f||e$2(`
version: "1.0"
project: "${n}"
agent-guidelines:
  overview: |
    This project uses Paradigm for structured AI-assisted development.
symbol-system:
  "@": { name: Feature, description: User-facing capabilities, owner: purpose, examples: ["@login"] }
logging:
  enforce: true
scan:
  enabled: true
conventions: []
`);e.writeFileSync(t.join(i,"config.yaml"),d$1(p),"utf8");}let h=t.join(l,".paradigm-scan-index.json"),S=t.join(i,"scan-index.json");return e.existsSync(h)&&e.renameSync(h,S),o.succeed(a.green("Migrated to .paradigm/ directory")),{feature:"migrate",status:"updated",message:"Converted to directory format (backup at .paradigm.backup)"}}catch(d){return o.fail(a.red("Migration failed")),{feature:"migrate",status:"error",message:d.message}}}async function E(l,n,s,o){let c=t.join(l,".paradigm"),i=t.join(c,"scan-index.json"),g=t.join(c,"specs","scan.md"),d=j();if(e.existsSync(i)&&e.existsSync(g)&&!s.force)return {feature:"scan",status:"skipped",message:"Already configured (use --force to reconfigure)"};if(s.dryRun)return {feature:"scan",status:"added",message:"Would add scan spec and index placeholder"};o.start("Setting up paradigm scan...");try{e.mkdirSync(t.join(c,"specs"),{recursive:!0});let f=t.join(d,"specs","scan.md");if(e.existsSync(f)&&e.copyFileSync(f,g),!e.existsSync(i)){let y={$meta:{version:"1.0.0",project:n,generatedAt:new Date().toISOString(),paradigmVersion:"0.2.0",sources:{purposeFiles:0,portalFiles:0,premiseFiles:0}},components:{},features:{},flows:{},state:{},gates:{},signals:{},screens:{},symbolMap:{},_placeholder:!0,_message:"Run `paradigm index` to populate this index"};e.writeFileSync(i,JSON.stringify(y,null,2),"utf8");}return o.succeed(a.green("Scan feature configured")),{feature:"scan",status:"added",message:"Added scan spec and index placeholder"}}catch(f){return o.fail(a.red("Failed to configure scan")),{feature:"scan",status:"error",message:f.message}}}async function L(l,n,s){let o=t.join(l,".paradigm"),c=t.join(o,"specs","logger.md"),i=j();if(e.existsSync(c)&&!n.force)return {feature:"logger",status:"skipped",message:"Already configured (use --force to reconfigure)"};if(n.dryRun)return {feature:"logger",status:"added",message:"Would add logger specification"};s.start("Setting up logger spec...");try{e.mkdirSync(t.join(o,"specs"),{recursive:!0});let g=t.join(i,"specs","logger.md");return e.existsSync(g)?e.copyFileSync(g,c):e.writeFileSync(c,`# Paradigm Logger Specification

Use the Paradigm logger instead of raw console.log/print statements.

## API

\`\`\`
log.component('#login-handler').info('Starting login', { email })
log.component('#database').debug('Query executed', { duration })
log.gate('^authenticated').warn('Access denied', { userId })
log.signal('!login-success').info('User authenticated')
\`\`\`

## Log Levels

- \`debug\` - Verbose debugging info
- \`info\` - General information
- \`warn\` - Warning conditions
- \`error\` - Error conditions

See full specification for implementation details.
`,"utf8"),s.succeed(a.green("Logger spec configured")),{feature:"logger",status:"added",message:"Added logger specification"}}catch(g){return s.fail(a.red("Failed to configure logger")),{feature:"logger",status:"error",message:g.message}}}function k(l,n,s){let o=e.readdirSync(l,{withFileTypes:true});for(let c of o){let i=t.join(l,c.name),g=t.join(n,c.name);if(c.isDirectory())e.existsSync(g)||e.mkdirSync(g,{recursive:true}),k(i,g,s);else if(!e.existsSync(g)){let d=e.readFileSync(i,"utf8");d=d.replace(/\{\{PROJECT_NAME\}\}/g,s),e.writeFileSync(g,d,"utf8");}}}async function M(l,n,s,o){let c=t.join(l,".horizon"),i=t.join(l,".paradigm");if(!e.existsSync(c))return {feature:"from-horizon",status:"skipped",message:"No .horizon directory found to migrate"};if(e.existsSync(i)&&!s.force)return {feature:"from-horizon",status:"skipped",message:".paradigm already exists (use --force to overwrite)"};if(s.dryRun){console.log(a.cyan(`
Dry run - would perform the following:
`)),console.log(a.gray("  \u2022 Rename .horizon/ \u2192 .paradigm/"));let g=F(l,"gate.yaml");g.length>0&&console.log(a.gray(`  \u2022 Rename ${g.length} gate.yaml \u2192 portal.yaml`));let d=F(l,".dream");return d.length>0&&console.log(a.gray(`  \u2022 Rename ${d.length} .dream \u2192 .premise`)),console.log(a.gray("  \u2022 Update content references (horizon\u2192paradigm, gate\u2192portal, dream\u2192premise)")),console.log(),{feature:"from-horizon",status:"updated",message:"Would migrate Horizon to Paradigm (dry run)"}}o.start("Migrating from Horizon to Paradigm...");try{if(e.existsSync(c)){if(e.existsSync(i)){let p=t.join(l,".paradigm.backup-"+Date.now());e.renameSync(i,p),o.text="Backed up existing .paradigm...";}e.renameSync(c,i),o.text="Renamed .horizon to .paradigm...";}o.text="Renaming gate.yaml files...";let g=F(l,"gate.yaml");for(let p of g){let m=t.dirname(p),r=t.join(m,"portal.yaml");e.renameSync(p,r);let u=e.readFileSync(r,"utf8");u=x(u),e.writeFileSync(r,u,"utf8");}o.text="Renaming .dream files...";let d=F(l,".dream");for(let p of d){let m=t.dirname(p),r=t.join(m,".premise");e.renameSync(p,r);let u=e.readFileSync(r,"utf8");u=x(u),e.writeFileSync(r,u,"utf8");}o.text="Updating file contents...",e.existsSync(i)&&v(i);let f=t.join(i,"scan-index.json"),y=t.join(i,"probe-index.json");if(e.existsSync(f)){let p=e.readFileSync(f,"utf8");p=x(p),e.writeFileSync(y,p,"utf8"),e.unlinkSync(f);}o.text="Updating .purpose file contents...";let h=F(l,".purpose");for(let p of h){let m=e.readFileSync(p,"utf8"),r=x(m);r!==m&&e.writeFileSync(p,r,"utf8");}o.text="Updating IDE instruction files...";let S=[".cursorrules",".windsurfrules",".github/copilot-instructions.md"];for(let p of S){let m=t.join(l,p);if(e.existsSync(m)){let r=e.readFileSync(m,"utf8");r=x(r),e.writeFileSync(m,r,"utf8");}}return o.succeed(a.green("Migration complete!")),{feature:"from-horizon",status:"updated",message:`Migrated: ${g.length} portal files, ${d.length} premise files`}}catch(g){return o.fail(a.red("Migration failed")),{feature:"from-horizon",status:"error",message:g.message}}}function F(l,n){let s=[],o=["node_modules",".git","dist","build",".paradigm"];function c(i){try{let g=e.readdirSync(i,{withFileTypes:!0});for(let d of g){let f=t.join(i,d.name);d.isDirectory()?!o.includes(d.name)&&!d.name.startsWith(".")&&c(f):d.name===n&&s.push(f);}}catch{}}return c(l),s}function x(l){return l.replace(/Horizon/g,"Paradigm").replace(/horizon/g,"paradigm").replace(/Dreamscape/g,"Prism").replace(/dreamscape/g,"prism").replace(/Dream\b/g,"Premise").replace(/dream\b/g,"premise").replace(/\bGate\b/g,"Portal").replace(/\bgate\b/g,"portal").replace(/\bScan\b/g,"Probe").replace(/\bscan\b/g,"probe").replace(/gate\.yaml/g,"portal.yaml").replace(/\.dream\b/g,".premise").replace(/scan-index\.json/g,"probe-index.json").replace(/\.horizon\//g,".paradigm/").replace(/\.horizon$/g,".paradigm").replace(/HORIZON_SYMBOLS/g,"PARADIGM_SYMBOLS")}function v(l){let n=e.readdirSync(l,{withFileTypes:true});for(let s of n){let o=t.join(l,s.name);if(s.isDirectory())v(o);else if(s.name.endsWith(".yaml")||s.name.endsWith(".md")||s.name.endsWith(".json")){let c=e.readFileSync(o,"utf8"),i=x(c);i!==c&&e.writeFileSync(o,i,"utf8");}}}function H(l){let n=[],s=t.join(l,".paradigm");if(!e.existsSync(s))return n;if(e.statSync(s).isFile())return n.push("migrate"),n;let c=s,i=t.join(c,"scan-index.json");e.existsSync(i)||n.push("scan");let g=t.join(c,"specs","logger.md");return e.existsSync(g)||n.push("logger"),n}
export{H as getAvailableUpgrades,W as upgradeCommand};