/**
 * @license
 * Copyright 2025 ReverseCraft
 * SPDX-License-Identifier: Apache-2.0
 */
export function isMinified(n){if(!n||n.length<100)return!1;const t=n.split("\n").filter(n=>n.trim().length>0);if(0===t.length)return!1;if(t.reduce((n,t)=>n+t.length,0)/t.length>500)return!0;return n.length/t.length>500&&t.length<=5}export function extractContextCode(n,t){const{lineNumber:e,columnNumber:i,contextLines:r,formatMinified:o,maxLineLength:s=500}=t;if(0===r)return{lines:[],wasFormatted:!1,warnings:[]};if(!n||0===n.length)return{lines:[],wasFormatted:!1,warnings:["Script has no source content"]};const l=t.lineOffsets||buildLineOffsets(n),u=l.length,h=Math.max(0,e-1-r),a=Math.min(u-1,e-1+r),g=[];for(let t=h;t<=a;t++){const r=t+1,o=r===e,u=l[t],h=t+1<l.length?l[t+1]-1:n.length;let a=n.slice(u,h);if(a.length>s)if(o){const n=Math.floor(s/2),t=Math.max(0,i-n),e=Math.min(a.length,i+n);a=(t>0?"... ":"")+a.substring(t,e)+(e<a.length?" ...":"")}else a=a.substring(0,s)+"... [truncated]";g.push({displayLineNumber:r,originalLineNumber:r,content:a,isCurrentLine:o,annotations:[]})}return{lines:g,wasFormatted:!1,warnings:[]}}export function formatContextCodeOutput(n,t,e,i,r){if(0===n.lines.length)return n.warnings.length>0?`⚠️ ${n.warnings.join(", ")}`:"No context code available";const o=[],s=r?` (${r})`:"";o.push(`📍 Code Context${s}:`),i&&o.push(`   Script: ${i}`),o.push(`   Original position: line ${t}, column ${e}`);n.lines.some(n=>n.content.length>500)&&o.push("   ℹ️ Code is from minified source");for(const t of n.warnings)o.push(`   ⚠️ ${t}`);o.push(""),o.push("   "+"─".repeat(50));const l=Math.max(...n.lines.map(n=>n.displayLineNumber)),u=String(l).length;for(const t of n.lines){const n=t.isCurrentLine?">>>":"   ",e=String(t.displayLineNumber).padStart(u," "),i=`[L:${t.originalLineNumber},C:0]`;let r=`${n}${e}  │ ${t.content}`;const s=Math.max(0,60-r.length);r+=" ".repeat(s)+i,t.isCurrentLine&&(r+=" ◄ current"),o.push(r)}o.push("   "+"─".repeat(50));const h=n.lines.find(n=>n.isCurrentLine);return h&&(o.push(""),o.push("   💡 Current position:"),o.push(`      • [L:${h.originalLineNumber},C:${e}]`)),o.join("\n")}export function formatPositionAnnotation(n,t){return`[L:${n},C:${t}]`}export function buildLineOffsets(n){const t=[0];if(!n)return t;for(let e=0;e<n.length;e++)"\n"===n[e]&&t.push(e+1);return t}export function lineColumnToIndex(n,t,e,i){if(!n||t<1)return 0;if(i&&i.length>0){const r=t-1;if(r>=i.length)return n.length;const o=i[r]+e;return Math.min(o,n.length)}let r=1,o=0;for(let i=0;i<n.length;i++){if(r===t){const t=o+e;return Math.min(t,n.length)}"\n"===n[i]&&(r++,o=i+1)}if(r===t){const t=o+e;return Math.min(t,n.length)}return n.length}