import{createColors as d}from"colorette";import*as _ from"../../tools/notifiers/helpers/colors.js";let{gray:g,red:M,colorsAreEnabled:y}=_;const A=120,k=3;function X(e,t,i=""){const f=!t||!y;t||({gray:g,red:M}=d({useColor:!1}));const{start:c,end:o,lines:n}=e;if(c.line===0)return{frame:"",start:{line:1,character:1}};const u=c.line,s=Math.max(Math.min(o.line??1,n.length),c.line);let l=Math.max(s-u-k+1,0);l<2&&(l=0);const m=[];let b=0;for(let r=u;r<=s&&!(l>0&&r>=s-l);r++){const a=n[r-1]||"";a!==""&&(b=$(a));let h=r===u?c.character-1:b,L=r===s?o.character-1:a.length;m.push([`${r}`,p(a,h,L,M)]),f&&m.push(["",C(a,h,L)])}l>0&&(m.push(["\u2026",`${E(b)}${g(`< ${l} more lines >`)}`]),m.push([`${s}`,p(n[s-1],-1,o.character-1,M)]),f&&m.push(["",C(n[s-1],-1,o.character-1)]));const N=P([[`${u-2}`,p(n[u-1-2])],[`${u-1}`,p(n[u-1-1])],...m,[`${s+1}`,p(n[s-1+1])],[`${s+2}`,p(n[s-1+2])]],i);return{gray:g,red:M}=_,{frame:N,start:c};function p(r,a=-1,h=1/0,L=g){return f||!r?r:(a===-1&&(a=$(r)),h=Math.min(h,r.length),r.substr(0,a)+L(r.substring(a,h))+r.substr(h))}}function P(e,t=""){const i=e.filter(([o,n])=>n!==void 0),f=Math.max(...i.map(([o])=>o.length)),c=Math.min(...i.map(([o,n])=>n===""?1/0:$(n)));return i.map(([o,n])=>t+g(w(f,o)+" |")+(n?" "+S(n.substring(c)):"")).join(`
`)}function w(e,t){return E(e-t.length)+t}function $(e){for(let t=0;t<e.length;t++)if(e[t]!==" ")return t;return e.length}function E(e){return" ".repeat(e)}function S(e,t=A){const i=e.length-t;if(i>0){const f=g(`...<${i} chars>`);return e.substring(0,t-f.length)+f}else return e}function C(e,t=-1,i=1/0){return t===-1&&(t=$(e)),i=Math.min(i,e.length),E(t)+"^".repeat(Math.max(i-t,1))}export{X as getCodeframe};
