import{formatValuePretty,truncate}from"./tool-format.js";import{elisionText}from"./rail.js";import{clipVisible,visibleLength,wrapVisibleLine}from"#cli/ui/terminal-text.js";function renderTool(e,t,n,r){return(e.depth??0)===0?renderToolRows(e,t-1,n,r).map(e=>` ${e}`):renderToolRows(e,t,n,r)}function renderToolRows(e,n,i,a){let{icon:o,accent:s}=toolGlyph(e.status??`running`,i,a),c=e.status===`done`&&e.doneTitle!==void 0?e.doneTitle:e.title??`tool`,l=n-3,u=clipVisible(c,l),d=` ${o} ${boldLeadingWord(u,i)}`,f=l-u.length-2,p=e.subtitle??``;p.length>0&&f>=6&&(d+=`  ${i.colors.gray(truncate(p,f))}`);let m=[d],h=[];return e.toolGroupItems===void 0?!e.expanded&&showToolDetail(e)&&h.push(...detailLineRows(e.detailLines??[],n,i)):h.push(...toolGroupItemRows(e.toolGroupItems,n,i)),h.length>0&&m.push(...h,` ${i.colors.dim(i.glyph.corner)}`),e.expanded?m.push(...renderToolExpanded(e,n,i)):e.status===`done`&&e.result&&e.result.length>0?m.push(resultLine(i.glyph.arrow,e.result,n,i,s)):e.status===`error`&&e.result?m.push(errorDetailLine(e.result,n,i)):e.status===`denied`&&m.push(resultLine(i.glyph.arrow,`denied`,n,i,i.colors.yellow)),m}function boldLeadingWord(e,t){let n=e.indexOf(` `);return n===-1?t.colors.bold(e):`${t.colors.bold(e.slice(0,n))}${t.colors.dim(e.slice(n))}`}function showToolDetail(e){if(e.detailLines===void 0||e.detailLines.length===0)return!1;let t=e.status??`running`;return t===`running`||t===`approval`?!0:e.keepDetailWhenDone===!0}const maxVisibleToolGroupItems=5;function railRow(e,t){return` ${t.colors.dim(t.glyph.rule)} ${e}`}function elisionRow(e,t){return railRow(elisionText(e,t),t)}function toolGroupItemRows(e,t,n){let r=e.slice(0,5),i=e.length-r.length,a=itemRows(r,t,n);return i>0&&a.push(elisionRow(i,n)),a}function detailLineRows(e,t,n){let i=n.colors,a=e.slice(0,10),o=e.length-a.length,s=a.some(e=>e.kind===`added`||e.kind===`removed`),c=Math.max(1,t-(s?5:4)),l=a.map(e=>{if(e.kind===`gap`)return railRow(i.dim(n.glyph.ellipsis),n);let t=clipVisible(e.text,c);switch(e.kind){case`added`:return` ${i.dim(n.glyph.rule)}${i.green(`+`)} ${i.green(t)}`;case`removed`:return` ${i.dim(n.glyph.rule)}${i.red(`-`)} ${i.red(t)}`;default:return s?` ${i.dim(n.glyph.rule)}  ${i.gray(t)}`:railRow(i.gray(t),n)}});return o>0&&l.push(elisionRow(o,n)),l}function itemRows(e,n,r){let a=Math.max(1,n-4);if(!e.some(e=>e.result!==void 0&&e.result.length>0))return e.map(e=>railRow(r.colors.gray(truncate(e.text,a)),r));let o=Math.min(Math.max(...e.map(e=>visibleLength(e.text))),Math.max(8,a-9)),s=Math.max(8,a-o-1);return e.map(e=>{let n=truncate(e.text,o);if(e.result===void 0||e.result.length===0)return railRow(r.colors.gray(n),r);let a=n+` `.repeat(Math.max(0,o-visibleLength(n)));return railRow(`${r.colors.gray(a)} ${r.colors.red(truncate(e.result,s))}`,r)})}function renderToolExpanded(t,n,r){let i=[],push=(t,o,s)=>{if(o!==void 0){i.push(`  ${r.colors.dim(t)}`);for(let t of formatValuePretty(o).split(`
`))for(let e of wrapVisibleLine(t,Math.max(1,n-4)))i.push(`    ${s(e)}`)}};return push(`input`,t.toolInput,r.colors.gray),t.status===`error`&&t.result?push(`error`,t.result,r.colors.red):push(`output`,t.toolOutput,r.colors.gray),i}function resultLine(e,n,r,i,a){let o=r-4;return`  ${i.colors.dim(e)} ${a(truncate(n,o))}`}function errorDetailLine(e,n,r){let i=n-5;return`   ${r.colors.dim(r.glyph.corner)} ${r.colors.red(truncate(e,i))}`}function toolGlyph(e,t,n){switch(e){case`done`:return{icon:t.colors.gray(t.glyph.square),accent:t.colors.gray};case`error`:return{icon:t.colors.red(t.glyph.error),accent:t.colors.red};case`denied`:return{icon:t.colors.yellow(t.glyph.warning),accent:t.colors.yellow};case`approval`:return{icon:t.colors.yellow(t.glyph.question),accent:t.colors.yellow};default:return{icon:t.colors.yellow(n.activityPulse),accent:t.colors.gray}}}export{maxVisibleToolGroupItems,renderTool};