import{envConfig as p}from"../../config/env-config.js";import{gray as w}from"./helpers/colors.js";const t="\x1B[",u=`${t}6n`,o=`${t}s`,c=`${t}u`,f=`${t}J`,h=n=>`${t}1;${n}r`,a=`${t}r`,l=`${t}?25h`,R=`${t}?25l`,I=()=>`${w("\u2500".repeat(process.stderr.columns||80))}

`,g=3;class E{#s=new Map;#i=process.stderr.rows;#o=0;#c=!1;#n=!1;#r;#p=!1;constructor(s=!1){this.#n=s}isInteractive(){return!!(this.#n&&process.stderr&&process.stderr.isTTY&&process.stdin.isTTY&&p.TERM!=="dumb"&&!("CI"in process.env)&&p.isDevelopMode)}isFooterChanged(s,r){return this.#s.get(s)!==r}updateFooter(s,r){const e=!this.#s.has(s);this.#s.set(s,r),this.isInteractive()&&(this.#a(),e?this.#h():this.#t())}get#e(){if(this.#s.size===0)return 0;let s=g;for(const r of this.#s.values())s+=(r.match(/\n/g)||[]).length;return s}#a(){!this.isInteractive()||this.#p||(this.#p=!0,this.#u(),process.stderr.write(u))}#t(){if(!this.isInteractive()||this.#s.size===0||!this.#r)return;this.#d();const s=this.#e,r=process.stderr.rows;if(!(s>r)){process.stderr.write(o),process.stderr.cursorTo(0,r-s-1),process.stderr.write(f),process.stderr.cursorTo(0,r-s+1),process.stderr.write(I());for(const e of this.#s.values())process.stderr.write(e);process.stderr.write(c)}}#h(s=0){if(!this.isInteractive())return;const r=process.stderr.rows;let e=this.#e;e>r&&(e=0);const i=e-this.#o;this.#o=e,i>0&&(process.stderr.write(o),process.stderr.write(`
`.repeat(i)),process.stderr.write(c)),process.stderr.write(o),s<0&&process.stderr.write(`
`.repeat(e)),process.stderr.write(h(r-e)),process.stderr.write(c),this.#t()}#d(){this.#c||!this.#r||(this.#c=!0,this.#w(),process.stderr.write(R),process.on("exit",this.#f),process.stderr.on("resize",this.#l))}#w(){if(!this.#r)return;const s=process.stderr.rows,{row:r}=this.#r,e=this.#e,i=s-e;r<i||(process.stderr.write(o),process.stderr.write(a),process.stderr.cursorTo(0,s-1),process.stderr.write(`
`.repeat(Math.max(0,r-e))),process.stderr.write(h(i)),process.stderr.write(c),process.stderr.cursorTo(0,0))}#u(){if(!this.isInteractive())return;process.stdin.setRawMode(!0),process.stdin.resume(),process.stdin.setEncoding("utf8");const s=/\[(\d+);(\d+)R/;process.stdin.once("data",r=>{if(s.test(r)){const e=s.exec(r);if(e){const i=parseInt(e[1],10),d=parseInt(e[2],10);this.#r={row:i,col:d}}}process.stdin.setRawMode(!1),process.stdin.pause(),this.#s.size>0&&this.#t()})}#f=()=>{this.isInteractive()&&(process.stderr.write(a),process.stderr.write(`${t}${process.stderr.rows};1H`),process.stderr.write(l))};#l=()=>{const s=process.stderr.rows-this.#i;this.#i=process.stderr.rows,this.#h(s)}}export{E as TerminalManager};
