import{n as e}from"./rolldown-runtime-DkW27tQK.js";import{er as t,wn as n}from"./control-ui-foundation-CeFMP1Ly.js";import{fl as r,ml as i}from"./control-ui-core-pH6nakZ-.js";import{G as a,J as o,W as s,Z as c,at as l,rt as u}from"./lit-runtime-BEtFSUCp.js";import{$t as d,Dn as f,Jt as p,Tn as m,Yt as h,en as g,m as _,p as v}from"./control-ui-core-DGVkFhVB.js";import{Gt as y,Ht as b,qt as x}from"./control-ui-core-Br5DB0Zj.js";import{In as S,Ln as C}from"./control-ui-boot-wpHCbPL9.js";import{i as w,n as T,t as E}from"./approval-result-validators-Bwny6Cwa.js";import"./approval-Dpr9VMH2.js";function D(e){return e instanceof d?(n(e.details)?e.details.reason:void 0)===`APPROVAL_NOT_FOUND`||e.gatewayCode===`APPROVAL_NOT_FOUND`||e.gatewayCode===`INVALID_REQUEST`:!1}function O(e){return new Intl.DateTimeFormat(y.getLocale(),{dateStyle:`medium`,timeStyle:`short`}).format(new Date(e))}function k(e){switch(e){case`allow-once`:return x(`execApproval.allowOnce`);case`allow-always`:return x(`execApproval.alwaysAllow`);case`deny`:return x(`execApproval.deny`)}return e}function A(e,t){return e.applied?t===`deny`?e.approval.status===`denied`:e.approval.status===`allowed`&&e.approval.decision===t:!0}function j(e,t){return t?o`
${e}
${t}
`:a}function M(e,t){let n=t?.trim();return n?o`${n}`:a}function N(e){return e.kind===`exec`?o` ${e.warningText?o`
${e.warningText}
`:a} ${e.commandPreview?o`
${x(`approvalPage.summaryLabel`)}
${e.commandPreview}
`:a}
${x(`approvalPage.commandLabel`)}
${e.commandText}
${j(x(`execApproval.labels.host`),e.host)} ${j(x(`approvalPage.nodeLabel`),e.nodeId)}
`:o`
${x(`approvalPage.requestLabel`)}
${e.description}
${e.kind===`plugin`&&e.detail?o`
${e.detail}
`:a} `}function P(e,t){if(t===`elsewhere`&&(e.status===`allowed`||e.status===`denied`))return x(`approvalPage.resolvedElsewhere`);if(t===`here`&&e.status===`allowed`)return x(`approvalPage.approvedHere`);if(t===`here`&&e.status===`denied`)return x(`approvalPage.deniedHere`);let n=e.status;switch(n){case`allowed`:return x(`approvalPage.approved`);case`denied`:return x(`approvalPage.denied`);case`expired`:return x(`approvalPage.expired`);case`cancelled`:return x(`approvalPage.cancelled`);case`pending`:return x(`approvalPage.pending`)}return n}function F(e,t){if(t===`elsewhere`&&(e.status===`allowed`||e.status===`denied`))return x(`approvalPage.resolvedElsewhereDescription`);let n=e.status;switch(n){case`allowed`:return e.decision===`allow-always`?x(`approvalPage.allowedAlwaysDescription`):x(`approvalPage.allowedOnceDescription`);case`denied`:return x(`approvalPage.deniedDescription`);case`expired`:return x(`approvalPage.expiredDescription`);case`cancelled`:return x(`approvalPage.cancelledDescription`);case`pending`:return x(`approvalPage.pendingDescription`)}return n}var I,L,R,z;function B(){return(B=e((()=>{C(),s(),c(),E(),g(),_(),p(),f(),b(),i(),I=2e3,L=250,R=`operator.approvals`,z=class extends r{constructor(...e){super(...e),this.approvalId=``,this.approval=null,this.connected=!1,this.approvalsAccess=!0,this.approvalGrantAccess=!1,this.loading=!0,this.resolving=!1,this.resolvingDecision=null,this.requestError=null,this.resolutionOrigin=`observed`,this.client=null,this.operationGeneration=0,this.handleVisibilityChange=()=>{if(document.visibilityState!==`visible`){this.clearPollTimer();return}this.approval?.status===`pending`&&this.hasGatewayConnection&&this.hasApprovalAccess&&!this.resolving&&this.loadApproval({background:!0})}}connectedCallback(){this.replaceChildren(),super.connectedCallback(),document.addEventListener(`visibilitychange`,this.handleVisibilityChange),this.previousDocumentTitle=document.title,this.bindApprovalId(!0),this.stopGateway=this.context.gateway.subscribe(e=>this.applyGatewaySnapshot(e)),this.applyGatewaySnapshot(this.context.gateway.snapshot)}disconnectedCallback(){document.removeEventListener(`visibilitychange`,this.handleVisibilityChange),this.stopGateway?.(),this.stopGateway=void 0,this.invalidateOperations(),this.clearPollTimer(),this.client=null,this.connected=!1,this.previousDocumentTitle!==void 0&&(!this.activeDocumentTitle||document.title===this.activeDocumentTitle)&&(document.title=this.previousDocumentTitle),this.previousDocumentTitle=void 0,this.activeDocumentTitle=void 0,super.disconnectedCallback()}updated(e){e.has(`approvalId`)&&this.bindApprovalId(),this.updateDocumentTitle()}bindApprovalId(e=!1){!e&&this.boundApprovalId===this.approvalId||(this.boundApprovalId=this.approvalId,this.invalidateOperations(),this.clearPollTimer(),this.approval=null,this.loading=!!this.approvalId,this.resolving=!1,this.resolvingDecision=null,this.requestError=this.approvalId?null:`unavailable`,this.resolutionOrigin=`observed`,this.approvalId&&this.connected&&this.client&&this.hasApprovalAccess&&this.loadApproval())}applyGatewaySnapshot(e){let t=e.client!==this.client,n=e.phase===`connected`!==this.connected,r=e.phase===`connected`&&!this.connected,i=h(e),a=i.canReviewApprovals,o=a!==this.approvalsAccess,s=i.canGrantApprovals!==this.approvalGrantAccess;if(this.client=e.client,this.connected=e.phase===`connected`,this.approvalsAccess=a,this.approvalGrantAccess=i.canGrantApprovals,(t||n||o||s)&&(this.invalidateOperations(),this.clearPollTimer(),this.resolving=!1,this.resolvingDecision=null),this.approvalsAccess||(this.approval=null),e.phase!==`connected`||!e.client){this.approvalId&&(this.loading=!1,this.requestError=!this.approval||this.approval.status===`pending`?`connection`:null);return}if(!this.approvalsAccess){this.approval=null,this.loading=!1,this.requestError=null;return}if(!this.approvalId){this.loading=!1,this.requestError=`unavailable`;return}if(t||r||o||!this.approval){this.loadApproval();return}this.schedulePoll()}invalidateOperations(){this.operationGeneration+=1}isCurrentOperation(e){return this.hasGatewayConnection&&this.hasApprovalAccess&&this.client===e.client&&this.approvalId===e.id&&this.operationGeneration===e.generation}get hasGatewayConnection(){return this.connected&&!!this.client}get hasApprovalAccess(){return this.approvalsAccess&&h(this.context.gateway.snapshot).canReviewApprovals}get hasApprovalGrantAccess(){return this.approvalGrantAccess&&h(this.context.gateway.snapshot).canGrantApprovals}async loadApproval(e={}){let t=this.client,n=this.approvalId;if(!t||!this.connected||!n||!this.hasApprovalAccess)return;let r=++this.operationGeneration,i=this.approval?.status,a=!1;this.clearPollTimer(),e.background||(this.loading=!0);try{let e=await t.request(`approval.get`,{id:n});if(!this.isCurrentOperation({client:t,generation:r,id:n}))return;if(!T(e)||e.approval.id!==n){this.approval=null,this.requestError=`unavailable`;return}this.requestError=null,this.approval=e.approval,e.approval.status===`pending`?this.resolutionOrigin=`observed`:i===`pending`&&this.resolutionOrigin===`observed`&&(this.resolutionOrigin=`elsewhere`,a=!0)}catch(e){if(!this.isCurrentOperation({client:t,generation:r,id:n}))return;D(e)?(this.approval=null,this.requestError=`unavailable`):this.requestError=`connection`}finally{this.isCurrentOperation({client:t,generation:r,id:n})&&(this.loading=!1,this.schedulePoll())}a&&this.isCurrentOperation({client:t,generation:r,id:n})&&await this.focusTerminalState()}async resolveApproval(e){let t=this.approval,n=this.client,r=this.approvalId;if(!n||!this.connected||!this.hasApprovalGrantAccess||!r||t?.status!==`pending`||!Array.prototype.includes.call(t.presentation.allowedDecisions,e)||this.resolving)return;let i=t.presentation.kind,a=++this.operationGeneration,o=()=>this.isCurrentOperation({client:n,generation:a,id:r})&&this.hasApprovalGrantAccess,s=!1,c=!1;this.clearPollTimer(),this.resolving=!0,this.resolvingDecision=e,this.requestError=null;try{let t=await n.request(`approval.resolve`,{id:r,kind:i,decision:e});if(!o())return;!w(t)||t.approval.id!==r||t.approval.presentation.kind!==i||!A(t,e)?(this.requestError=`connection`,c=!0):(this.approval=t.approval,this.resolutionOrigin=t.applied?`here`:`elsewhere`,s=!0)}catch(e){if(!o())return;this.requestError=D(e)?`unavailable`:`connection`}finally{o()&&(this.resolving=!1,this.resolvingDecision=null,this.schedulePoll())}if(c&&o()){await this.loadApproval({background:!0});return}s&&o()&&await this.focusTerminalState()}async focusTerminalState(){if(await this.updateComplete,this.approval?.status===`pending`)return;let e=this.querySelector(`#approval-page-title`);e?.focus({preventScroll:!0}),typeof e?.scrollIntoView==`function`&&e.scrollIntoView({behavior:`auto`,block:`center`,inline:`nearest`})}clearPollTimer(){this.pollTimer!==void 0&&(globalThis.clearTimeout(this.pollTimer),this.pollTimer=void 0)}schedulePoll(){this.clearPollTimer();let e=this.approval;if(!this.hasGatewayConnection||!this.hasApprovalAccess||this.resolving||this.requestError===`unavailable`||e?.status!==`pending`||document.visibilityState!==`visible`)return;let t=e.expiresAtMs-Date.now(),n=Math.max(L,Math.min(I,t+L));this.pollTimer=globalThis.setTimeout(()=>{this.pollTimer=void 0,this.loadApproval({background:!0})},n)}renderHeader(){return o`
${x(`approvalPage.eyebrow`)}
${x(`approvalPage.brandName`)}
`}renderLoading(){return o`

${x(`approvalPage.loadingTitle`)}

${x(`approvalPage.loadingDescription`)}

`}renderUnavailable(){return o` `}renderMissingScope(){return o` `}renderConnectionState(){return o` `}renderConnectionError(){return o` `}renderApproval(e){let t=e.status===`pending`,n=e.presentation,r=this.hasApprovalGrantAccess,i=t?n.kind===`plugin`?n.title:x(`approvalPage.execTitle`):P(e,this.resolutionOrigin),s=t?x(r?`approvalPage.pendingDescription`:`execApproval.reviewOnly`):F(e,this.resolutionOrigin);return o`
${t?x(`approvalPage.pending`):P(e,this.resolutionOrigin)}

${i}

${n.kind===`plugin`?o`${M(`plugin`,n.pluginId)} ${M(`tool`,n.toolName)}`:a} ${M(`agent`,n.agentId)}

${s}

${N(n)}
${x(t?`approvalPage.expiresLabel`:`approvalPage.resolvedLabel`)}
${this.requestError===`connection`?this.renderConnectionError():a} ${t?o`
${n.allowedDecisions.map(e=>o` `)}
`:o`
${x(`approvalPage.safeToClose`)}
`} `}render(){let e=this.connected&&!this.approvalsAccess,t=this.requestError===`unavailable`,n=this.requestError===`connection`&&!this.approval,r=e?`missing-scope`:t?`unavailable`:n?`connection-error`:this.approval?.status??`loading`,i=this.approval?.presentation,a=i?.kind===`plugin`?i.severity?.trim().toLowerCase():null,s=i?.kind===`exec`||a===`warning`||a===`warn`?`warning`:a===`danger`||a===`critical`||a===`error`?`danger`:`info`;return o`
${this.renderHeader()}
${e?this.renderMissingScope():this.loading&&!this.approval?this.renderLoading():n?this.renderConnectionState():t||!this.approval?this.renderUnavailable():this.renderApproval(this.approval)}
${x(`approvalPage.openControlUi`)}
`}updateDocumentTitle(){let e=`${this.connected&&!this.approvalsAccess?x(`common.disabled`):this.requestError===`unavailable`?x(`approvalPage.unavailableTitle`):this.requestError===`connection`&&!this.approval?x(`approvalPage.connectionErrorTitle`):this.approval?this.approval.status===`pending`?this.approval.presentation.kind===`plugin`?this.approval.presentation.title:x(`approvalPage.execTitle`):P(this.approval,this.resolutionOrigin):x(`approvalPage.loadingTitle`)} — ${x(`approvalPage.brandName`)}`;document.title=e,this.activeDocumentTitle=e}},t([S({context:v,subscribe:!1})],z.prototype,`context`,void 0),t([l({attribute:`approval-id`})],z.prototype,`approvalId`,void 0),t([u()],z.prototype,`approval`,void 0),t([u()],z.prototype,`connected`,void 0),t([u()],z.prototype,`approvalsAccess`,void 0),t([u()],z.prototype,`approvalGrantAccess`,void 0),t([u()],z.prototype,`loading`,void 0),t([u()],z.prototype,`resolving`,void 0),t([u()],z.prototype,`resolvingDecision`,void 0),t([u()],z.prototype,`requestError`,void 0),t([u()],z.prototype,`resolutionOrigin`,void 0)})))()}function V(){return(V=e((()=>{B(),customElements.get(`openclaw-approval-page`)||customElements.define(`openclaw-approval-page`,z)})))()}V(); //# sourceMappingURL=approval-page-registration-90H9Ag-v.js.map