import{b as o,n as C,e as E}from"./vendor-lit-B2r-dKc0.js";import{a as H}from"./vendor-mini-lit-CpUQub-j.js";import{aC as P,aD as y}from"./app-session-runtime-CeFJDILJ.js";import{aO as w,t as U}from"./vendor-lucide-B9Yl1Gez.js";import"./vendor-marked-_iteVaMm.js";import"./app-review-C3CmcSYC.js";import"./app-panel-workspace-C3XDRCgE.js";import"./app-routing-C_r2pdnC.js";import"./app-message-reducer-wATqnZGf.js";import"./vendor-sortable-B1o3v0Qs.js";import"./app-inbox-CaZD85aD.js";import"./app-pr-walkthrough-WipZACSB.js";function k(e){if(!e)return"";try{const t=new Date(e);return Number.isNaN(t.getTime())?"":t.toLocaleTimeString(void 0,{hour:"2-digit",minute:"2-digit",second:"2-digit"})}catch{return""}}function N(e){switch(e){case"user":return"bg-blue-500/15 text-blue-700 dark:text-blue-400";case"assistant":return"bg-emerald-500/15 text-emerald-700 dark:text-emerald-400";default:return"bg-muted text-muted-foreground"}}function _(e){return o`
		<div class="border-l-2 border-border pl-2 py-1">
			<div class="flex items-center gap-2 text-xs">
				<span class="font-mono text-muted-foreground">#${e.index}</span>
				<span class="px-1.5 py-0.5 rounded ${N(e.role)}">${e.role}</span>
				${e.ts?o`<span class="text-muted-foreground">${k(e.ts)}</span>`:""}
			</div>
			${e.text?o`<div class="mt-1 text-sm whitespace-pre-wrap break-words">${e.text}</div>`:""}
			${e.toolUses?.length?o`<div class="mt-1 text-xs text-muted-foreground">
					${e.toolUses.map(t=>o`<div class="font-mono">→ ${t.name}(${t.inputPreview})</div>`)}
				</div>`:""}
			${e.toolResults?.length?o`<div class="mt-1 text-xs text-muted-foreground">
					${e.toolResults.map(t=>o`<div class="font-mono">← ${t.name??"result"}: ${t.preview}</div>`)}
				</div>`:""}
		</div>
	`}async function z(e,t,s,p){const l=new URLSearchParams({offset:String(t),limit:String(s)}),r=await fetch(`/api/sessions/${encodeURIComponent(e)}/transcript?${l.toString()}`,{credentials:"include"});if(!r.ok){const n=await r.json().catch(()=>({}));throw new Error(`${r.status}: ${n&&n.error||"transcript fetch failed"}`)}return r.json()}function M(e){const s=document.createElement("div");s.style.cssText="position:fixed;inset:0;background:rgba(0,0,0,0.5);z-index:9998;display:flex;align-items:center;justify-content:center;padding:1rem;",s.tabIndex=-1;const p=document.createElement("div");p.className="bg-background text-foreground border border-border rounded-lg shadow-2xl",p.style.cssText="width:min(900px,100%);max-height:90vh;display:flex;flex-direction:column;overflow:hidden;";const l=document.createElement("div");l.className="flex items-center justify-between gap-2 px-4 py-2 border-b border-border",l.innerHTML=`<div class="text-sm font-medium">Session transcript <span class="text-muted-foreground font-mono text-xs ml-2">${e.slice(0,12)}</span></div>`;const r=document.createElement("button");r.className="text-muted-foreground hover:text-foreground text-sm px-2 py-1",r.textContent="✕",r.setAttribute("aria-label","Close"),r.onclick=()=>s.remove(),l.appendChild(r);const n=document.createElement("div");n.className="flex-1 overflow-y-auto p-4 space-y-2",n.style.cssText="scrollbar-width:thin;";const c=document.createElement("div");c.className="text-xs text-muted-foreground",c.textContent="Loading…",n.appendChild(c),p.appendChild(l),p.appendChild(n),s.appendChild(p),document.body.appendChild(s),s.addEventListener("click",i=>{i.target===s&&s.remove()}),s.addEventListener("keydown",i=>{i.key==="Escape"&&s.remove()}),setTimeout(()=>s.focus(),0);let d=0,u=1/0,x=!1;async function g(){if(!(x||d>=u)){x=!0;try{const i=await z(e,d,50,!1);u=i.total??0,c.remove();const m=i.messages??[];for(const a of m){const $=document.createElement("div");$.className="border-l-2 border-border pl-3 py-1";const v=k(a.ts),f=a.role||"?",T=N(f),R=`<div style="display:flex;gap:0.5rem;align-items:center;font-size:0.75rem;">
					<span style="font-family:monospace;color:var(--muted-foreground);">#${a.index}</span>
					<span class="${T}" style="padding:0.1rem 0.4rem;border-radius:0.25rem;">${f}</span>
					${v?`<span style="color:var(--muted-foreground);">${v}</span>`:""}
				</div>`,S=a.text?`<div style="margin-top:0.25rem;font-size:0.875rem;white-space:pre-wrap;word-break:break-word;">${b(a.text)}</div>`:"",L=a.toolUses?.length?`<div style="margin-top:0.25rem;font-size:0.75rem;color:var(--muted-foreground);font-family:monospace;">${a.toolUses.map(h=>`→ ${b(h.name)}(${b(h.inputPreview)})`).join("<br/>")}</div>`:"",j=a.toolResults?.length?`<div style="margin-top:0.25rem;font-size:0.75rem;color:var(--muted-foreground);font-family:monospace;">${a.toolResults.map(h=>`← ${b(h.name??"result")}: ${b(h.preview)}`).join("<br/>")}</div>`:"";$.innerHTML=R+S+L+j,n.appendChild($)}if(d+=m.length||50,d>=u){const a=document.createElement("div");a.className="text-xs text-muted-foreground text-center py-2",a.textContent=`End of transcript (${u} messages)`,n.appendChild(a)}}catch(i){const m=document.createElement("div");m.className="text-xs text-destructive",m.textContent=`Failed to load: ${i?.message??String(i)}`,n.appendChild(m),d=u}finally{x=!1}}}n.addEventListener("scroll",()=>{n.scrollTop+n.clientHeight>=n.scrollHeight-100&&g()}),g()}function b(e){return e.replace(/[&<>"']/g,t=>({"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"})[t])}class W{render(t,s,p){const l=P(s,p),r=E(),n=E(),c=s?.details,d=c?.session_id??t?.session_id??"",u=d?d.slice(0,12):"?";if(!s){const v=t?.session_id?t.session_id.slice(0,12):"?",f=t?.pattern?`pattern="${t.pattern}" offset=${t.offset??0} limit=${t.limit??20}`:`offset=${t?.offset??0} limit=${t?.limit??20}`;return{content:o`
					<div>
						${y(l,w,o`Reading session <span class="font-mono text-xs">${v}</span> — <span class="text-xs text-muted-foreground">${f}</span>`,r,n,!1)}
					</div>
				`,isCustom:!1}}if(s.isError){const v=s.content?.filter(f=>f.type==="text").map(f=>f.text).join(`
`)||"";return{content:o`
					<div>
						${y(l,w,o`read_session <span class="font-mono text-xs">${u}</span> — <span class="text-destructive text-xs">error</span>`,r,n,!0)}
						<div ${C(r)} class="max-h-[2000px] mt-3 overflow-hidden transition-all duration-300">
							<div class="text-xs font-mono text-destructive whitespace-pre-wrap">${v}</div>
						</div>
					</div>
				`,isCustom:!1}}const x=c?.messages??[],g=c?.total??0,i=c?.matchCount,m=c?.returned??x.length,a=i!==void 0?o`<span class="text-xs text-muted-foreground">${m}/${i} matches of ${g}</span>`:o`<span class="text-xs text-muted-foreground">${m} of ${g}</span>`,$=()=>{d&&M(d)};return{content:o`
				<div>
					${y(l,w,o`read_session <span class="font-mono text-xs">${u}</span> — ${a}`,r,n,!0)}
					<div ${C(r)} class="max-h-[2000px] mt-3 overflow-hidden transition-all duration-300">
						<div class="space-y-1">
							${x.length===0?o`<div class="text-xs text-muted-foreground italic">No messages in window.</div>`:x.map(_)}
						</div>
						${d?o`
								<button
									@click=${$}
									class="mt-3 inline-flex items-center gap-1 text-xs px-2 py-1 rounded border border-border hover:bg-muted transition-colors"
									data-testid="read-session-open-full"
								>
									Open full transcript
									<span class="inline-block">${H(U,"xs")}</span>
								</button>
							`:""}
					</div>
				</div>
			`,isCustom:!1}}}export{W as ReadSessionRenderer};
