#!/usr/bin/env node
import {e}from'./chunk-JIXHEBGK.js';import {e as e$1}from'./chunk-QT2LKB3P.js';import'./chunk-XHJ27CER.js';import*as v from'fs';import*as l from'path';import o from'chalk';import D from'ora';function Q(t){let a=t.length;return Math.ceil(a/3.5)}function f(t,a){try{let s=v.readFileSync(t,"utf8"),m=Buffer.byteLength(s,"utf8"),C=Q(s),r=l.relative(a,t);return {path:t,relativePath:r,tokens:C,bytes:m}}catch{return null}}function i(t){return t<1e3?t.toString():`${(t/1e3).toFixed(1)}k`}async function L(t,a){let s=t?l.resolve(t):process.cwd(),m=D();a.json||console.log(o.blue(`
\u{1F4B0} Paradigm Cost Analysis
`)),m.start("Analyzing context files...");let C=await e(s),r=[];for(let e of C){let n=f(e,s);n&&r.push(n);}let A=await e$1(s),y=[];for(let e of A){let n=f(e,s);n&&y.push(n);}let R=l.join(s,".paradigm","scan-index.json"),h=f(R,s),I=l.join(s,".cursorrules"),q=l.join(s,".cursor","rules","paradigm.mdc"),g=f(I,s);g||(g=f(q,s)),m.stop();let x=r.reduce((e,n)=>e+n.tokens,0),F=y.reduce((e,n)=>e+n.tokens,0),b=h?.tokens||0,P=g?.tokens||0,w=x+F+b,u=w+P,T=150,k=T*7,c=u>0?Math.round((1-k/u)*100):0,d=[],M=r.filter(e=>e.tokens>500);M.length>0&&d.push(`${M.length} .purpose file(s) exceed 500 tokens. Consider splitting large features.`);let N=l.join(s,".cursor","mcp.json"),$=v.existsSync(N);$||d.push("MCP not configured. Run `paradigm mcp setup` to enable dynamic context (80%+ savings)."),h||d.push("No scan-index.json found. Run `paradigm index` for visual discovery support."),r.length<3&&d.push("Few .purpose files found. Add more context for better AI understanding.");let O={static:{purposeFiles:r,portalFiles:y,scanIndex:h,cursorrules:g,total:u},dynamic:{avgQueryTokens:T,typicalConversation:k},savings:{percentage:Math.max(0,c),description:c>0?`${c}% fewer tokens with MCP vs static context`:"MCP provides on-demand context loading"},recommendations:d};if(a.json){console.log(JSON.stringify(O,null,2));return}if(console.log(o.white("Context Token Analysis")),console.log(o.gray("\u2500".repeat(50))),console.log(o.cyan(`
Static Context (loaded every conversation):`)),console.log(`  .purpose files (${r.length}):`.padEnd(35)+o.yellow(i(x)+" tokens")),console.log(`  portal.yaml (${y.length}):`.padEnd(35)+o.yellow(i(F)+" tokens")),h&&console.log("  scan-index.json:".padEnd(35)+o.yellow(i(b)+" tokens")),g&&console.log("  .cursorrules:".padEnd(35)+o.yellow(i(P)+" tokens")),console.log(o.gray("\u2500".repeat(50))),console.log("  Static Total:".padEnd(35)+o.yellow.bold(i(u)+" tokens")),console.log(o.cyan(`
Dynamic Context (MCP on-demand):`)),console.log("  Avg query response:".padEnd(35)+o.green(i(T)+" tokens")),console.log("  Typical conversation (~7 queries):".padEnd(35)+o.green(i(k)+" tokens")),console.log(o.cyan(`
Potential Savings:`)),c>0){let e=c>70?o.green:c>40?o.yellow:o.white;console.log("  MCP vs Static:".padEnd(35)+e.bold(`${c}% reduction`));let n=u/1e3*.01,p=k/1e3*.01,j=n-p;j>.001&&console.log("  Est. savings per conversation:".padEnd(35)+o.green(`~$${j.toFixed(4)}`));}if(a.detailed&&r.length>0){console.log(o.cyan(`
Top Token Consumers:`));let e=[...r].sort((n,p)=>p.tokens-n.tokens).slice(0,5);for(let n of e){let p=Math.round(n.tokens/w*100);console.log(`  ${n.relativePath}`.padEnd(40)+o.gray(`${i(n.tokens)} (${p}%)`));}}if(d.length>0){console.log(o.cyan(`
Recommendations:`));for(let e of d)console.log(o.yellow(`  \u2022 ${e}`));}console.log(o.cyan(`
MCP Status:`)),$?console.log(o.green("  \u2713 MCP configured - using dynamic context loading")):(console.log(o.yellow("  \u25CB MCP not configured")),console.log(o.gray("    Run `paradigm mcp setup` to enable token-efficient queries"))),console.log("");}export{L as costCommand};