import{formatPromptHeader,formatRailLine}from"./prompt-ui.js";const SPINNER_FRAMES=[`⠨`,`⠸`,`⢸`,`⢺`,`⢾`,`⢿`,`⢾`,`⢼`,`⢸`,`⠸`,`⠨`,`⠪`,`⠮`,`⠯`,`⢯`,`⢿`,`⠿`,`⠾`,`⠺`,`⠪`],SPINNER_CELLS=[...SPINNER_FRAMES[0]].length;function dotCount(e){let t=0;for(let n of e){let e=(n.codePointAt(0)??10240)-10240;for(;e>0;)t+=e&1,e>>=1}return t}const SPINNER_STATIC=SPINNER_FRAMES.reduce((e,t)=>dotCount(t)>dotCount(e)?t:e);function countRows(e){return e.split(`
`).length-1}function createRailLog(a){let o={kind:`idle`},s=a.output.isTTY===!0;function writeLine(e){a.output.write(formatRailLine(e,a.colors,a.output))}function clearPreview(){!s||o.kind===`idle`||o.previewRows===0||(a.output.write(`\u001B[${o.previewRows}A\u001B[J`),o.previewRows=0)}function settleStatus(e){if(o.kind!==`idle`){if(s&&(clearPreview(),e))for(let e of o.commandLines)writeLine(a.colors.dim(e));o={kind:`idle`}}}return{message(e){settleStatus(!1),writeLine(e),o={kind:`active`,commandLines:[],previewRows:0}},info(e){settleStatus(!1),writeLine(a.colors.dim(e))},success(e){settleStatus(!1),writeLine(a.colors.dim(e))},warning(e){settleStatus(!0),writeLine(a.colors.yellow(e))},error(e){settleStatus(!0),writeLine(a.colors.red(e))},commandOutput(e){if(o.kind===`idle`){writeLine(a.colors.dim(e));return}if(o.commandLines.push(e),!s){writeLine(a.colors.dim(e));return}clearPreview();let n=formatRailLine(a.colors.dim(e),a.colors,a.output);a.output.write(n),o.previewRows=countRows(n)},section(n,r){settleStatus(!1);let i=r.map(e=>formatRailLine(e,a.colors,a.output)).join(``);a.output.write(`${formatPromptHeader(`submit`,n,{colors:a.colors,leadingRail:`green`})}${i}`)},spinner(e){settleStatus(!1);let t=`${a.colors.green(`│`)}\n`,o=s&&a.output.columns?a.output.columns:80,c=Math.max(4,o-(SPINNER_CELLS+3)),l=e.length>c?`${e.slice(0,c-1)}…`:e,row=e=>`${a.colors.green(e)}  ${l}`;if(!s)return a.output.write(`${t}${row(SPINNER_STATIC)}\n`),{stop(){}};let u=0;a.output.write(`${t}${row(SPINNER_FRAMES[0])}`);let d=setInterval(()=>{u+=1;let e=SPINNER_FRAMES[u%SPINNER_FRAMES.length]??SPINNER_FRAMES[0];a.output.write(`\r\u001B[K${row(e)}`)},120);d.unref?.();let f=!1;return{stop(){f||(f=!0,clearInterval(d),a.output.write(`\r\x1B[K\x1B[1A\r\x1B[K`))}}},settle(){settleStatus(!1)}}}export{SPINNER_FRAMES,createRailLog};