import{n as e}from"./rolldown-runtime-DkW27tQK.js";import{er as t}from"./control-ui-foundation-CeFMP1Ly.js";import{fl as n,fs as r,ml as i,ms as a}from"./control-ui-core-pH6nakZ-.js";import{G as o,J as s,W as c,Z as l,at as u,rt as d}from"./lit-runtime-BEtFSUCp.js";import{Jt as f,Wt as p,qt as m}from"./control-ui-core-DGVkFhVB.js";import{Ht as h,Lt as g,Rt as _,qt as v}from"./control-ui-core-Br5DB0Zj.js";import{mt as y,pt as b}from"./control-ui-boot-wpHCbPL9.js";import{a as x,i as S,r as C}from"./gateway-runtime-DUNO4Mi1.js";import{Do as w,Mo as T,jo as E}from"./control-ui-boot-Cdpgu1Gm.js";import{t as D}from"./board-view-DEfofU9J.js";var O;function k(){return(k=e((()=>{y(),c(),l(),f(),_(),h(),T(),a(),C(),i(),D(),O=class extends n{constructor(...e){super(...e),this.sessionKey=null,this.onDocumentClose=null,this.documentState=`loading`,this.activeTabId=``,this.errorText=``,this.provider=null,this.providerLease=null,this.binding=null,this.unsubscribeSnapshot=null,this.unsubscribeLoadError=null,this.unsubscribeEvents=null,this.bindingGeneration=0}disconnectedCallback(){this.releaseProvider(),super.disconnectedCallback()}updated(e){(e.has(`sessionKey`)||e.has(`gatewaySnapshot`))&&this.synchronizeProvider()}providerCapabilities(e){let t=m(e.hello?.auth??null);return{canMutate:t,canGrant:p(e.hello?.auth??null),canPinWidgets:t&&S(e,b.BOARD_WIDGET_PUT_CANVAS_DOC)===!0,canPinMcpApps:t&&x(e,`board.widget.appView`)===!0&&x(e,`board.widget.put`)===!0}}synchronizeProvider(){let e=this.sessionKey?.trim()??``;if(!e){this.releaseProvider(),this.documentState=`missing-session`;return}let t=this.gatewaySnapshot,n=t?.client;if(!t||!n){this.releaseProvider(),this.documentState=`loading`;return}if(x(t,`board.get`)===!1){this.releaseProvider(),this.documentState=`unavailable`;return}let r=this.providerCapabilities(t),i=JSON.stringify(r);if(this.binding?.client===n&&this.binding.sessionKey===e&&this.binding.capabilityKey===i){this.providerLease?.update(n,t.phase===`connected`,r);return}this.releaseProvider(),this.documentState=`loading`;let a=this.bindingGeneration;this.bindProvider({client:n,sessionKey:e,capabilityKey:i},r,a)}async bindProvider(e,t,n){try{let r=await e.client.request(`sessions.describe`,{key:e.sessionKey});if(n!==this.bindingGeneration)return;if(!r.session){this.documentState=`not-found`;return}let i=this.gatewaySnapshot;if(!i||i.client!==e.client)return;let a=w(e.sessionKey,e.client,i.phase===`connected`,t.canPinWidgets,t.canPinMcpApps,t.canMutate,t.canGrant),o=a.provider;this.provider=o,this.providerLease=a,this.binding=e,this.unsubscribeSnapshot=o.snapshot$.subscribe(()=>this.reconcileProvider(o)),this.unsubscribeLoadError=o.loadError$.subscribe(()=>this.reconcileProvider(o)),this.unsubscribeEvents=o.events.subscribe(e=>{let t=e.command;t.kind===`focus_tab`&&o.snapshot$.value.tabs.some(e=>e.tabId===t.tabId)&&(this.activeTabId=t.tabId)}),this.reconcileProvider(o)}catch(e){n===this.bindingGeneration&&(this.errorText=r(e),this.documentState=`error`)}}selectAvailableTab(){let e=this.snapshot;e&&(e.tabs.some(e=>e.tabId===this.activeTabId)||(this.activeTabId=e.tabs[0]?.tabId??e.widgets[0]?.tabId??``))}reconcileProvider(e){if(this.provider!==e)return;if(E(e)){this.snapshot=e.snapshot$.value,this.selectAvailableTab(),this.documentState=`ready`;return}let t=e.loadError$.value;t?(this.errorText=t,this.documentState=`error`):(this.errorText=``,this.documentState=`loading`)}releaseProvider(){this.bindingGeneration+=1,this.unsubscribeSnapshot?.(),this.unsubscribeLoadError?.(),this.unsubscribeEvents?.(),this.unsubscribeSnapshot=null,this.unsubscribeLoadError=null,this.unsubscribeEvents=null,this.providerLease?.release(),this.provider=null,this.providerLease=null,this.binding=null,this.snapshot=void 0,this.activeTabId=``,this.errorText=``}renderState(){if(this.documentState===`loading`)return s`