#!/usr/bin/env node
import {m as m$1}from'./chunk-T6IDXUUA.js';import'./chunk-JIXHEBGK.js';import'./chunk-QT2LKB3P.js';import'./chunk-XHJ27CER.js';import*as m from'fs';import*as a from'path';import i from'chalk';async function R(e={}){let s=process.cwd(),n=(await m$1(s)).symbols,o=S(n),r=k(n),c=w(n),d=O(n,s),g=B(n,s),f=o.length,u=r.length,y=c.length,h=d.length,b=g.length;if(e.json){let t={brokenCount:f,duplicateCount:u,orphanedCount:y,missingAnchors:h,outOfBoundsAnchors:b,details:{brokenReferences:o,duplicateSymbols:r,orphanedSymbols:c.slice(0,20),missingAnchorFiles:d,anchorOutOfBounds:g}};console.log(JSON.stringify(t));return}if(console.log(i.blue(`
\u{1F50D} Paradigm Integrity Check
`)),f>0){console.log(i.red(`  \u2717 ${f} broken reference${f>1?"s":""}`));for(let t of o.slice(0,10))console.log(i.gray(`    \u2502 ${t.from} \u2192 ${t.to} (${t.refType}) in ${t.file}`));f>10&&console.log(i.gray(`    \u2502 ... and ${f-10} more`));}else console.log(i.green("  \u2713 No broken references"));if(u>0){console.log(i.red(`  \u2717 ${u} duplicate symbol${u>1?"s":""}`));for(let t of r.slice(0,10))console.log(i.gray(`    \u2502 ${t.symbol} defined in: ${t.files.join(", ")}`));}else console.log(i.green("  \u2713 No duplicate symbols"));if(y>0){console.log(i.yellow(`  \u26A0 ${y} isolated symbol${y>1?"s":""} (no connections to other symbols)`));for(let t of c.slice(0,5))console.log(i.gray(`    \u2502 ${t.symbol} in ${t.file}`));y>5&&console.log(i.gray(`    \u2502 ... and ${y-5} more`));}else console.log(i.green("  \u2713 All symbols are connected"));if(h>0){console.log(i.red(`  \u2717 ${h} anchor${h>1?"s":""} pointing to missing files`));for(let t of d.slice(0,5))console.log(i.gray(`    \u2502 ${t.symbol}: ${t.anchor}`));}else console.log(i.green("  \u2713 All anchor files exist"));if(b>0){console.log(i.yellow(`  \u26A0 ${b} anchor${b>1?"s":""} with line ranges exceeding file length`));for(let t of g.slice(0,5))console.log(i.gray(`    \u2502 ${t.symbol}: ${t.anchor} (file has ${t.fileLines} lines)`));}else console.log(i.green("  \u2713 All anchor line ranges valid"));let p=f+u+h+b;console.log(""),console.log(p===0?i.green(`\u2728 All integrity checks passed!
`):`${p} issue${p>1?"s":""} found. Run 'paradigm doctor' for full diagnostics.
`);}function S(e){let s=[],l=new Set(e.map(o=>o.symbol));for(let o of e)if(o.parentSymbol){let r=o.parentSymbol.trim(),c=/^[#$^!~]/.test(r)?r:`#${r}`;l.has(c)||s.push({from:o.symbol,to:c,file:o.filePath,refType:"parent"});}let n=new Set;return s.filter(o=>{let r=`${o.from}->${o.to}`;return n.has(r)?false:(n.add(r),true)})}function k(e){let s=new Map;for(let n of e){let o=s.get(n.symbol)||[];o.find(r=>r.file===n.filePath)||o.push({file:n.filePath,source:n.source}),s.set(n.symbol,o);}let l=[];for(let[n,o]of s)if(!(o.length<=1)){if(o.length===2){let r=o.map(c=>c.source).sort();if(r[0]==="portal"&&r[1]==="purpose")continue}l.push({symbol:n,files:o.map(r=>r.file)});}return l}function w(e){return e.filter(s=>s.referencedBy.length===0&&s.references.length===0).map(s=>({symbol:s.symbol,file:s.filePath}))}function A(e,s,l){if(a.isAbsolute(e))return e;if(l){let n=a.dirname(l),o=a.join(n,e);if(m.existsSync(o))return o}return a.join(s,e)}function O(e,s){let l=[];for(let n of e)if(!(!n.anchors||n.anchors.length===0))for(let o of n.anchors){let r=A(o.path,s,n.filePath);m.existsSync(r)||l.push({symbol:n.symbol,anchor:o.raw});}return l}function B(e,s){let l=[];for(let n of e)if(!(!n.anchors||n.anchors.length===0))for(let o of n.anchors){let r=A(o.path,s,n.filePath);if(!m.existsSync(r))continue;let c=C(o);if(!(c<=0))try{let g=m.readFileSync(r,"utf8").split(`
`).length;c>g&&l.push({symbol:n.symbol,anchor:o.raw,fileLines:g});}catch{}}return l}function C(e){return typeof e.lines=="number"?e.lines:Array.isArray(e.lines)?Math.max(...e.lines):0}export{R as integrityCommand};