#!/usr/bin/env node
import*as l from'fs';import*as a from'path';function C(o,s){let n=o.symbols;return {brokenReferences:A(n),duplicateSymbols:x(n),orphanedSymbols:w(n),missingAnchorFiles:P(n,s),anchorOutOfBounds:I(n,s),timestamp:new Date().toISOString()}}function A(o){let s=[],n=new Set(o.map(t=>t.symbol));for(let t of o){if(t.parentSymbol){let i=y(t.parentSymbol);i&&!n.has(i)&&s.push({from:t.symbol,to:i,file:t.filePath,refType:"parent"});}let r=t.data;if(r&&typeof r=="object"&&t.type==="flow"){let i=r.steps||[];for(let m of i){let c=m.symbol||m.component;if(c){let p=y(c);p&&!n.has(p)&&s.push({from:t.symbol,to:p,file:t.filePath,refType:"flow"});}}let u=r.gates||[];for(let m of u){let c=y(m);c&&!n.has(c)&&s.push({from:t.symbol,to:c,file:t.filePath,refType:"gate"});}let f=r.signals||[];for(let m of f){let c=y(m);c&&!n.has(c)&&s.push({from:t.symbol,to:c,file:t.filePath,refType:"signal"});}let b=r.components||[];for(let m of b){let c=y(m);c&&!n.has(c)&&s.push({from:t.symbol,to:c,file:t.filePath,refType:"component"});}}}let e=new Set;return s.filter(t=>{let r=`${t.from}->${t.to}@${t.file}`;return e.has(r)?false:(e.add(r),true)})}function y(o){if(!o||typeof o!="string")return null;let s=o.trim();return s?/^[#$^!~@%?&]/.test(s)?s:`#${s}`:null}function x(o){let s=new Map;for(let e of o){let t=s.get(e.symbol)||[];t.find(i=>i.file===e.filePath)||t.push({file:e.filePath,source:e.source}),s.set(e.symbol,t);}let n=[];for(let[e,t]of s)if(!(t.length<=1)){if(t.length===2){let r=t.map(i=>i.source).sort();if(r[0]==="portal"&&r[1]==="purpose")continue}n.push({symbol:e,files:t.map(r=>r.file)});}return n}function w(o){let s=[];for(let n of o)n.referencedBy.length===0&&n.references.length===0&&s.push({symbol:n.symbol,file:n.filePath,referenceCount:0});return s}function P(o,s){let n=[];for(let e of o)if(!(!e.anchors||e.anchors.length===0))for(let t of e.anchors){let r=g(t.path,s,e.filePath);l.existsSync(r)||n.push({symbol:e.symbol,anchor:t.raw});}return n}function I(o,s){let n=[];for(let e of o)if(!(!e.anchors||e.anchors.length===0))for(let t of e.anchors){let r=g(t.path,s,e.filePath);if(!l.existsSync(r))continue;let i=S(t);if(!(i<=0))try{let f=l.readFileSync(r,"utf8").split(`
`).length;i>f&&n.push({symbol:e.symbol,anchor:t.raw,fileLines:f});}catch{}}return n}function T(o,s){let n={valid:0,missing:0,outOfBounds:0,issues:[]};for(let e of o)if(!(!e.anchors||e.anchors.length===0))for(let t of e.anchors){let r=g(t.path,s,e.filePath);if(!l.existsSync(r)){n.missing++,n.issues.push({symbol:e.symbol,anchor:t.raw,status:"missing"});continue}let i=S(t);if(i>0)try{let f=l.readFileSync(r,"utf8").split(`
`).length;if(i>f){n.outOfBounds++,n.issues.push({symbol:e.symbol,anchor:t.raw,status:"out-of-bounds"});continue}}catch{n.missing++,n.issues.push({symbol:e.symbol,anchor:t.raw,status:"missing"});continue}n.valid++,n.issues.push({symbol:e.symbol,anchor:t.raw,status:"ok"});}return n}var O=new Set(["node_modules","dist",".git",".paradigm","coverage","build","__pycache__","target",".next",".nuxt"]),v=new Set([".ts",".js",".tsx",".jsx",".py",".rs",".go",".swift"]);function B(o,s){let n=[],e=[];for(let r of o){let i=a.isAbsolute(r)?r:a.join(s,r);if(!l.existsSync(i))continue;let u;try{u=l.readFileSync(i,"utf8");}catch{continue}let f=u.split(`
`).length,m=(u.match(/^  [A-Za-z][A-Za-z0-9_-]*:/gm)||[]).length;if(f>500){let c=f>1e3?"strongly recommend":"consider",p;m>=10?p=`${c} splitting: ${m} top-level symbols across ${f} lines. Split by component type or subdirectory.`:p=`${c} splitting: ${f} lines. Reduce descriptions or extract sub-components.`,n.push({file:a.relative(s,i),lines:f,symbolCount:m,suggestion:p});}try{let c=l.statSync(i),p=a.dirname(i),h=k(p,s);if(h){let R=l.statSync(h.path),d=Math.floor((R.mtime.getTime()-c.mtime.getTime())/(1e3*60*60*24));d>=7&&e.push({file:a.relative(s,i),lastModified:c.mtime.toISOString(),daysSinceUpdate:d,newestSourceFile:a.relative(s,h.path)});}}catch{}}let t=Math.max(0,100-n.length*10-e.length*5);return {oversized:n,stale:e,healthScore:t}}function g(o,s,n){if(a.isAbsolute(o))return o;if(n){let e=a.dirname(n),t=a.join(e,o);if(l.existsSync(t))return t}return a.join(s,o)}function S(o){return typeof o.lines=="number"?o.lines:Array.isArray(o.lines)?Math.max(...o.lines):0}function k(o,s){let n=null;try{let e=l.readdirSync(o,{withFileTypes:!0});for(let t of e){if(!t.isFile()||O.has(t.name))continue;let r=a.extname(t.name);if(!v.has(r))continue;let i=a.join(o,t.name);try{let u=l.statSync(i);(!n||u.mtime.getTime()>n.mtime)&&(n={path:i,mtime:u.mtime.getTime()});}catch{continue}}}catch{}return n}export{C as a,T as b,B as c};