import{i as u,b as a,A as p,a as _,r as c,t as f}from"./vendor-lit-B2r-dKc0.js";import{p as h}from"./app-review-C3CmcSYC.js";import"./app-session-runtime-CeFJDILJ.js";import"./app-panel-workspace-C3XDRCgE.js";import"./app-routing-C_r2pdnC.js";import"./vendor-mini-lit-CpUQub-j.js";import"./vendor-lucide-B9Yl1Gez.js";import"./vendor-marked-_iteVaMm.js";import"./app-message-reducer-wATqnZGf.js";import"./vendor-sortable-B1o3v0Qs.js";import"./app-inbox-CaZD85aD.js";import"./app-pr-walkthrough-WipZACSB.js";var g=Object.defineProperty,b=Object.getOwnPropertyDescriptor,d=(s,o,r,i)=>{for(var t=i>1?void 0:i?b(o,r):o,e=s.length-1,l;e>=0;e--)(l=s[e])&&(t=(i?l(o,r,t):l(t))||t);return i&&t&&g(o,r,t),t};let n=class extends u{constructor(){super(...arguments),this.compactionId="",this.sessionId="",this._total=null,this._loading=!1,this._error=null,this._expanded=!1,this._rows=[],this._firstLoadedIndex=0,this._observer=null,this._countLoaded=!1}createRenderRoot(){return this}connectedCallback(){super.connectedCallback(),typeof IntersectionObserver<"u"?(this._observer=new IntersectionObserver(s=>{for(const o of s)if(o.isIntersecting){this._loadCount(),this._observer?.disconnect(),this._observer=null;break}},{rootMargin:"200px"}),queueMicrotask(()=>{this._observer&&this._observer.observe(this)}),setTimeout(()=>{this._countLoaded||this._loadCount()},500)):this._loadCount()}disconnectedCallback(){super.disconnectedCallback(),this._observer?.disconnect(),this._observer=null}async refreshCount(){this._countLoaded=!1,this._total=null,await this._loadCount()}async _loadCount(){if(!(this._countLoaded||!this.sessionId||!this.compactionId)){this._countLoaded=!0;try{const s=await h(`/api/sessions/${encodeURIComponent(this.sessionId)}/transcript/before-compaction?compactionId=${encodeURIComponent(this.compactionId)}&limit=1`);if(!s.ok){s.status!==404&&console.warn(`[pre-compaction-history] count fetch HTTP ${s.status}`),this._total=0;return}const o=await s.json();this._total=typeof o.total=="number"?o.total:0}catch(s){console.warn("[pre-compaction-history] count fetch failed:",s),this._total=0}}}async _loadFirstPage(){if(this._loading)return;this._loading=!0,this._error=null;const s=50,o=this._total??0,r=Math.max(0,o-s),i=o-r;try{const t=await h(`/api/sessions/${encodeURIComponent(this.sessionId)}/transcript/before-compaction?compactionId=${encodeURIComponent(this.compactionId)}&cursor=${r}&limit=${i}&verbose=1`);if(!t.ok){this._error=`Failed to load (HTTP ${t.status})`;return}const e=await t.json();this._rows=e.messages||[],this._firstLoadedIndex=r,typeof e.total=="number"&&(this._total=e.total)}catch(t){this._error=t instanceof Error?t.message:String(t)}finally{this._loading=!1}}async _loadOlder(){if(this._loading||this._firstLoadedIndex<=0)return;this._loading=!0,this._error=null;const o=Math.max(0,this._firstLoadedIndex-50),r=this._firstLoadedIndex-o;try{const i=await h(`/api/sessions/${encodeURIComponent(this.sessionId)}/transcript/before-compaction?compactionId=${encodeURIComponent(this.compactionId)}&cursor=${o}&limit=${r}&verbose=1`);if(!i.ok){this._error=`Failed to load (HTTP ${i.status})`;return}const t=await i.json();this._rows=[...t.messages||[],...this._rows],this._firstLoadedIndex=o}catch(i){this._error=i instanceof Error?i.message:String(i)}finally{this._loading=!1}}_onToggle(){if(this._expanded){this._expanded=!1;return}this._expanded=!0,this._rows.length===0&&!this._loading&&this._loadFirstPage()}render(){if(this._total===null)return a`<div data-testid="pre-compaction-history" data-state="loading"></div>`;if(this._total===0)return a`<div data-testid="pre-compaction-history" data-state="empty"></div>`;const s=this._expanded?"expanded":"collapsed",o=this._expanded?"▾":"▸",r=this._firstLoadedIndex,i=r>0,t=this._rows.map(e=>e.message).filter(e=>!!e).map((e,l)=>{const m=typeof e.content=="string"?[{type:"text",text:e.content}]:e.content;return{...e,content:m,id:typeof e.id=="string"&&e.id.length>0?`orphan:${this.compactionId}:${e.id}`:`orphan:${this.compactionId}:${l}`}});return a`
			<div
				data-testid="pre-compaction-history"
				data-state=${s}
				data-test-row-count=${this._rows.length}
				data-test-total=${this._total}
				style="margin-bottom: 0.5rem;"
			>
				<button
					type="button"
					@click=${this._onToggle}
					data-testid="pre-compaction-toggle"
					class="inline-flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground"
					style="background: none; border: none; padding: 0.25rem 0; cursor: pointer;"
				>
					<span aria-hidden="true">${o}</span>
					${this._expanded?a`Hide ${this._total} message${this._total===1?"":"s"} before compaction`:a`Show ${this._total} message${this._total===1?"":"s"} before compaction`}
				</button>
				${this._expanded?a`
						<div
							data-testid="pre-compaction-rows"
							style="border-left: 2px solid var(--border); padding-left: 0.75rem; margin-top: 0.5rem; opacity: 0.7;"
						>
							${this._error?a`<div class="text-xs text-destructive">${this._error}</div>`:p}
							${i&&!this._loading?a`<button
									type="button"
									@click=${this._loadOlder}
									data-testid="pre-compaction-load-more"
									class="text-xs text-muted-foreground hover:text-foreground"
									style="background: none; border: none; padding: 0.25rem 0; cursor: pointer; margin-bottom: 0.5rem;"
								>\u25b2 Load ${Math.min(50,r)} older</button>`:p}
							${this._loading?a`<div class="text-xs text-muted-foreground">Loading\u2026</div>`:p}
							<message-list
								.messages=${t}
								.isStreaming=${!1}
								.hasStreamMessage=${!1}
							></message-list>
						</div>
					`:p}
			</div>
		`}};d([_({type:String,attribute:"compaction-id"})],n.prototype,"compactionId",2);d([_({type:String,attribute:"session-id"})],n.prototype,"sessionId",2);d([c()],n.prototype,"_total",2);d([c()],n.prototype,"_loading",2);d([c()],n.prototype,"_error",2);d([c()],n.prototype,"_expanded",2);d([c()],n.prototype,"_rows",2);d([c()],n.prototype,"_firstLoadedIndex",2);n=d([f("bobbit-pre-compaction-history")],n);export{n as PreCompactionHistory};
