// @bun
var{defineProperty:A,getOwnPropertyNames:N,getOwnPropertyDescriptor:W}=Object,w=Object.prototype.hasOwnProperty;function B(q){return this[q]}var x=(q)=>{var k=(O??=new WeakMap).get(q),G;if(k)return k;if(k=A({},"__esModule",{value:!0}),q&&typeof q==="object"||typeof q==="function"){for(var H of N(q))if(!w.call(k,H))A(k,H,{get:B.bind(q,H),enumerable:!(G=W(q,H))||G.enumerable})}return O.set(q,k),k},O;var I=(q)=>q;function S(q,k){this[q]=I.bind(null,k)}var D=(q,k)=>{for(var G in k)A(q,G,{get:k[G],enumerable:!0,configurable:!0,set:S.bind(k,G)})};var v=(q,k)=>()=>(q&&(k=q(q=0)),k);var f=import.meta.require;async function R(q){try{let k=await Bun.file(q).text(),G=C(k),H=F(k,q),K=L(k,G),J=P(k,G);return{file:q,metrics:G,issues:H,suggestions:K,performance:J}}catch(k){throw Error(`Failed to analyze template ${q}: ${k instanceof Error?k.message:String(k)}`)}}function C(q){let k=q.split(`
`),G=q.match(/<script\b[^>]*>([\s\S]*?)<\/script>/i),K=(G?G[1]:"").split(`
`).length,J={conditionals:(q.match(/@(if|unless|elseif|else|endif|endunless)\b/g)||[]).length,loops:(q.match(/@(foreach|for|endforeach|endfor|while|endwhile)\b/g)||[]).length,includes:(q.match(/@(include|component|extends|section|yield)\b/g)||[]).length,custom:0,total:0};J.total=J.conditionals+J.loops+J.includes+J.custom;let X=(q.match(/\{\{[\s\S]*?\}\}/g)||[]).length,Z=(q.match(/\{!![\s\S]*?!!\}/g)||[]).length,Y=(q.match(/@(?:if|unless|elseif|foreach|for|while)\s*\([^)]+\)/g)||[]).length,V=X+Z+Y,Q=(q.match(/<[A-Z][^>]*>/g)||[]).length,$=(q.match(/@(?:layout|extends)\(/g)||[]).length,j=0,z=0;for(let _ of k)if(/@(?:if|unless|foreach|for|while|section)\b/.test(_)&&!/@end/.test(_)&&!/@else/.test(_))z++,j=Math.max(j,z);else if(/@end(?:if|unless|foreach|for|while|section)\b/.test(_))z--;let E=j>=4?3:0,M=Math.min(10,Math.ceil(J.total*0.5+V*0.1+Q*0.3+(K>10?K*0.05:0)+E));return{lines:k.length,characters:q.length,directives:J,expressions:V,components:Q,layouts:$,scriptLines:K,complexity:M}}function F(q,k){let G=[],H=U(q,"if","endif");if(H>0)G.push({type:"error",category:"syntax",message:`Found ${H} unmatched @if directive(s)`,suggestion:"Ensure every @if has a corresponding @endif"});let K=U(q,"foreach","endforeach");if(K>0)G.push({type:"error",category:"syntax",message:`Found ${K} unmatched @foreach directive(s)`,suggestion:"Ensure every @foreach has a corresponding @endforeach"});let J=q.match(/@foreach[\s\S]*?@foreach[\s\S]*?@endforeach[\s\S]*?@endforeach/g);if(J&&J.length>0)G.push({type:"warning",category:"performance",message:"Nested loops detected which may impact performance",suggestion:"Consider preprocessing data or using more efficient data structures"});let X=(q.match(/\{!![\s\S]*?!!\}/g)||[]).length;if(X>0)G.push({type:"warning",category:"security",message:`Found ${X} raw output expression(s) {!! !!}`,suggestion:"Ensure raw outputs are properly sanitized to prevent XSS"});let Z=q.match(/<img[^>]*>/g);if(Z){let Q=Z.filter(($)=>!$.includes("alt="));if(Q.length>0)G.push({type:"warning",category:"accessibility",message:`Found ${Q.length} image(s) without alt attributes`,suggestion:"Add alt attributes to all images for screen reader accessibility"})}let Y=q.split(`
`).filter((Q)=>Q.length>120);if(Y.length>0)G.push({type:"info",category:"maintainability",message:`Found ${Y.length} line(s) longer than 120 characters`,suggestion:"Consider breaking long lines for better readability"});let V=(q.match(/style\s*=\s*["'][^"']*["']/g)||[]).length;if(V>3)G.push({type:"info",category:"maintainability",message:`Found ${V} inline style attributes`,suggestion:"Consider moving styles to CSS classes for better maintainability"});return G}function L(q,k){let G=[];if(k.complexity>7)G.push({type:"refactor",message:"Template complexity is high. Consider breaking into smaller components.",impact:"high",effort:"medium"});else if(k.complexity>5&&k.directives.total>8)G.push({type:"optimization",message:"Template has moderate complexity. Consider component extraction for reusability.",impact:"medium",effort:"low"});if(k.lines>100&&k.components===0)G.push({type:"refactor",message:"Large template with no components. Consider extracting reusable parts into components.",impact:"medium",effort:"medium"});if(k.expressions>20)G.push({type:"optimization",message:"Many template expressions detected. Consider preprocessing some data in the script section.",impact:"medium",effort:"low"});let H=q.match(/@foreach[\s\S]*?@foreach[\s\S]*?@endforeach[\s\S]*?@endforeach/g);if(H&&H.length>0)G.push({type:"optimization",message:"Nested loops detected. Consider preprocessing data or optimizing data structures.",impact:"high",effort:"medium"});if(k.directives.total===0&&k.expressions<5)G.push({type:"optimization",message:"Template appears static. Consider enabling aggressive caching.",impact:"high",effort:"low"});if(k.scriptLines>50)G.push({type:"refactor",message:"Large script section. Consider moving complex logic to external modules.",impact:"medium",effort:"medium"});return G}function P(q,k){let G=1;if(G+=k.directives.conditionals*0.1,G+=k.directives.loops*0.5,G+=k.directives.includes*2,G+=k.expressions*0.05,G+=k.components*1,k.scriptLines>10)G+=k.scriptLines*0.1;let H;if(k.expressions===0&&k.directives.total===0)H="high";else if(k.expressions<5&&k.directives.total<3)H="medium";else H="low";let K=[];if(G>10)K.push("Consider optimizing template complexity");if(H==="low"&&k.directives.total>5)K.push("Reduce dynamic content for better caching");if(k.components===0&&k.lines>40)K.push("Extract components to improve reusability and performance");return{estimatedRenderTime:Math.round(G*100)/100,complexityScore:k.complexity,cacheability:H,recommendations:K}}function U(q,k,G){let H=q.replace(/<!--[\s\S]*?-->/g,""),K=(H.match(new RegExp(`@${k}\\b`,"g"))||[]).length,J=(H.match(new RegExp(`@${G}\\b`,"g"))||[]).length;return Math.abs(K-J)}async function h(q=["**/*.stx"],k){let G=[],H=[];for(let J of q){let X=await Array.fromAsync(new Bun.Glob(J).scan({cwd:k||process.cwd(),onlyFiles:!0,absolute:!0}));H.push(...X.filter((Z)=>Z.endsWith(".stx")))}for(let J of H)try{let X=await R(J);G.push(X)}catch(X){console.warn(`Failed to analyze ${J}:`,X)}let K=T(G);return{results:G,summary:K}}function T(q){let k=q.length,G=q.reduce((V,Q)=>V+Q.metrics.lines,0),H=k>0?q.reduce((V,Q)=>V+Q.metrics.complexity,0)/k:0,K=q.reduce((V,Q)=>V+Q.issues.length,0),J={};q.forEach((V)=>{V.issues.forEach((Q)=>{J[Q.category]=(J[Q.category]||0)+1})});let X=k>0?q.reduce((V,Q)=>V+Q.performance.estimatedRenderTime,0)/k:0,Z=Math.max(1,Math.min(10,Math.round(10-X/2-H/2))),Y=[];if(H>6)Y.push("Project has high average complexity. Consider refactoring complex templates.");if(K>k*2)Y.push("High number of issues detected. Run detailed analysis on individual files.");if(J.security>0)Y.push("Security issues found. Review raw output usage and input sanitization.");return{totalFiles:k,totalLines:G,avgComplexity:Math.round(H*100)/100,totalIssues:K,issuesByCategory:J,performanceScore:Z,recommendations:Y}}export{R as analyzeTemplate,h as analyzeProject};
