async function d(i,n){try{const s=[{type:"files",query:"MATCH (n:File) RETURN COUNT(n) AS count"},{type:"functions",query:"MATCH (n:Function) RETURN COUNT(n) AS count"},{type:"classes",query:"MATCH (n:Class) RETURN COUNT(n) AS count"},{type:"interfaces",query:"MATCH (n:Interface) RETURN COUNT(n) AS count"},{type:"methods",query:"MATCH (n:Method) RETURN COUNT(n) AS count"}],e={};for(const{type:t,query:r}of s)try{const l=(await i(r))[0];e[t]=Array.isArray(l)?l[0]??0:(l==null?void 0:l.count)??0}catch{e[t]=0}return{projectName:n,fileCount:e.files,functionCount:e.functions,classCount:e.classes,interfaceCount:e.interfaces,methodCount:e.methods}}catch(s){return console.error("Failed to get codebase stats:",s),{projectName:n,fileCount:0,functionCount:0,classCount:0,interfaceCount:0,methodCount:0}}}async function m(i,n=8){try{const s=`
      MATCH (n)-[r:CodeRelation]-(m)
      WHERE n.name IS NOT NULL
      WITH n, COUNT(r) AS connections
      ORDER BY connections DESC
      LIMIT ${n}
      RETURN n.name AS name, LABEL(n) AS type, n.filePath AS filePath, connections
    `;return(await i(s)).map(t=>Array.isArray(t)?{name:t[0],type:t[1],filePath:t[2],connections:t[3]}:{name:t.name,type:t.type,filePath:t.filePath,connections:t.connections}).filter(t=>t.name&&t.type)}catch(s){return console.error("Failed to get hotspots:",s),[]}}async function A(i,n=10,s="indented"){try{const r=(await i("MATCH (f:File) RETURN f.filePath AS path ORDER BY path")).map(o=>Array.isArray(o)?o[0]:o.path).filter(Boolean);if(r.length===0)return"";if(s==="ascii"){const o=$(r,n);return C(o,"",!0)}return T(r,n)}catch(e){return console.error("Failed to get folder tree:",e),""}}function T(i,n){const s={children:new Map,fileCount:0};for(const r of i){const l=r.replace(/\\/g,"/").split("/").filter(Boolean);let u=s;for(let c=0;c<l.length;c++){const a=l[c],f=c===l.length-1;if(u.children.has(a)||u.children.set(a,{isFile:f,children:new Map,fileCount:0}),u=u.children.get(a),f){let h=s;for(let p=0;p<c;p++)h=h.children.get(l[p]),h.fileCount++}}}const e=[];function t(r,o,l){const u=[...r.children.entries()];u.sort(([c,a],[f,h])=>a.isFile!==h.isFile?a.isFile?1:-1:!a.isFile&&!h.isFile?h.fileCount-a.fileCount:c.localeCompare(f));for(const[c,a]of u)if(a.isFile)e.push(`${o}${c}`);else{a.children.size;const f=a.fileCount;l>=n?e.push(`${o}${c}/ (${f} files)`):(e.push(`${o}${c}/`),t(a,o+"  ",l+1))}}return t(s,"",0),e.join(`
`)}function $(i,n){const s=new Map;for(const e of i){const r=e.replace(/\\/g,"/").split("/").filter(Boolean);let o=s;const l=Math.min(r.length,n+1);for(let u=0;u<l;u++){const c=r[u],a=u===r.length-1;o.has(c)||o.set(c,a?null:new Map);const f=o.get(c);if(f instanceof Map)o=f;else break}}return s}function C(i,n,s=!0){const e=[],t=Array.from(i.entries());return t.sort(([r,o],[l,u])=>{const c=o instanceof Map,a=u instanceof Map;return c!==a?a?1:-1:r.localeCompare(l)}),t.forEach(([r,o],l)=>{const u=l===t.length-1,c=u?"└── ":"├── ",a=n+(u?"    ":"│   ");if(o instanceof Map&&o.size>0){const f=y(o),h=f>3?` (${f} items)`:"";e.push(`${n}${c}${r}/${h}`),e.push(C(o,a,u))}else o instanceof Map?e.push(`${n}${c}${r}/`):e.push(`${n}${c}${r}`)}),e.filter(Boolean).join(`
`)}function y(i){let n=0;for(const[,s]of i)s instanceof Map?n+=1+y(s):n+=1;return n}async function E(i,n,s="indented"){const[e,t,r]=await Promise.all([d(i,n),m(i),A(i,10,s)]);return{stats:e,hotspots:t,folderTree:r}}function R(i){const{stats:n,hotspots:s,folderTree:e}=i,t=[];t.push(`### 📊 CODEBASE: ${n.projectName}`);const r=[`Files: ${n.fileCount}`,`Functions: ${n.functionCount}`,n.classCount>0?`Classes: ${n.classCount}`:null,n.interfaceCount>0?`Interfaces: ${n.interfaceCount}`:null].filter(Boolean);return t.push(r.join(" | ")),t.push(""),s.length>0&&(t.push("**Hotspots** (most connected):"),s.slice(0,5).forEach(o=>{t.push(`- \`${o.name}\` (${o.type}) — ${o.connections} edges`)}),t.push("")),e&&(t.push("### 📁 STRUCTURE"),t.push("```"),t.push(n.projectName+"/"),t.push(e),t.push("```")),t.join(`
`)}function M(i,n){const s=R(n);return`${i}

---

## 📦 CURRENT CODEBASE
${s}`}export{E as buildCodebaseContext,M as buildDynamicSystemPrompt,$ as buildTreeFromPaths,R as formatContextForPrompt,C as formatTreeAsAscii,d as getCodebaseStats,A as getFolderTree,m as getHotspots};
