// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
"use strict";(()=>{var T=["invalid-frame","invalid-payload","payload-too-large","unsupported-version","schema-mismatch","permission-denied","unknown-method","receiver-unavailable","not-ready","overloaded","cancelled","deadline-exceeded","navigated","closed","transport","handler"],i=class extends Error{constructor(s,r=s,c="Check the connection and the generated contract.",o){super(r);this.code=s;this.help=c;this.applicationCode=o;this.name="IpcError"}code;help;applicationCode};function N(e){return T.includes(e)?e:"transport"}function k(e,a="transport"){if(e instanceof i)return e;if(e&&typeof e=="object"){let s=Object.getOwnPropertyDescriptor(e,"name")?.value,r=Object.getOwnPropertyDescriptor(e,"code")?.value;if(s==="IpcError"&&T.includes(r))return new i(r)}return new i(a)}var h=Object.freeze({maxFrameBytes:1048576,maxNativeControlBytes:4096,maxPendingCallsPerDirection:64,maxOutstandingNotificationsPerDirection:64,maxQueuedFramesPerDirection:128,maxQueuedBytesPerDirection:8388608,maxAdmittedInputBytesPerFrame:8388608,maxRetainedBytesPerFrame:16777216,maxWorkerTasksPerFrameIncludingRetiredDocuments:128,maxCallbacksPerEvent:16,maxCallbacksPerDocument:128,maxCallbackTasksPerDocument:128,maxCollectionEntriesPerMessage:16384,maxSchemaDepth:16,maxReadinessWaiters:16,defaultTimeoutMs:3e4,maxTimeoutMs:3e5,notificationAcceptTimeoutMs:3e4,handshakeTimeoutMs:5e3,maxErrorTextBytesTotal:2048,reservedControlFramesPerDirection:128});function R(e){for(let a of Object.keys(h))if(!Number.isSafeInteger(e[a])||e[a]<=0)throw new i("invalid-frame",`Invalid IPC limit: ${a}`);if(e.maxFrameBytes>16*1048576||e.maxNativeControlBytes>65536||e.maxPendingCallsPerDirection>4096||e.maxCallbacksPerDocument>1024||e.maxQueuedBytesPerDirection>64*1048576||e.maxFrameBytes>e.maxQueuedBytesPerDirection||e.defaultTimeoutMs>e.maxTimeoutMs||e.maxTimeoutMs>3e5||e.maxOutstandingNotificationsPerDirection>4096||e.maxCallbacksPerEvent>e.maxCallbacksPerDocument||e.maxSchemaDepth>16||e.maxReadinessWaiters>16||e.notificationAcceptTimeoutMs>e.maxTimeoutMs||e.maxWorkerTasksPerFrameIncludingRetiredDocuments>4096||e.maxCallbackTasksPerDocument>4096||e.maxQueuedFramesPerDirection>8192||e.reservedControlFramesPerDirection>8192||e.maxCollectionEntriesPerMessage>1048576||e.maxErrorTextBytesTotal>2048||e.handshakeTimeoutMs>5e3||e.maxFrameBytes<e.maxErrorTextBytesTotal+128)throw new i("invalid-frame","Inconsistent IPC limits.");if(e.maxAdmittedInputBytesPerFrame>64*1048576||e.maxRetainedBytesPerFrame>128*1048576||e.maxAdmittedInputBytesPerFrame<e.maxFrameBytes||e.maxRetainedBytesPerFrame<e.maxAdmittedInputBytesPerFrame)throw new i("invalid-frame","Inconsistent IPC byte budgets.")}function F(e){return{wireVersion:e.wireVersion,contractName:e.contractName,contractMajor:e.contractMajor,schemaHash:e.schemaHash}}function I(e,a){if(typeof e!="string"||e.length!==a)return!1;for(let s=0;s<e.length;s++){let r=e.charCodeAt(s);if(!(r>=48&&r<=57)&&!(r>=97&&r<=102))return!1}return!0}function C(e,a=!1){if(typeof e!="string"||e.length===0||e.length>20)return!1;try{let s=BigInt(e);return s>=(a?0n:1n)&&s<=0xffffffffffffffffn&&String(s)===e}catch{return!1}}function x(e){if(!e||typeof e!="object"||Array.isArray(e))throw new i("invalid-frame");let a=JSON.stringify(e);if(a.length>h.maxNativeControlBytes||new TextEncoder().encode(a).byteLength>h.maxNativeControlBytes)throw new i("invalid-frame");return e}function L(e,a=0n){let s="";for(let t of crypto.getRandomValues(new Uint8Array(16)))s+=t.toString(16).padStart(2,"0");let r,c,o,f=!1,d=!1,l,B=a,u,E="1",v=e.subscribe(j);function D(t){try{Promise.resolve(e.postMessage({kind:"disconnect",generation:t.generation,token:t.token})).catch(()=>{})}catch{}}function P(){if(!l)return;let t=l;l=void 0,D(t)}function M(t,n){return t.kind==="helloResult"&&t.callId===E&&t.navigation===n.navigation&&t.documentNonce===n.documentNonce&&t.challenge===n.challenge}function A(t,n){try{let m=x(t);M(m,n)&&!m.error&&C(m.generation)&&I(m.token,32)&&D({generation:m.generation,token:m.token})}catch{}}function b(t){if(!u)return;let n=u;u=void 0,c=void 0,r=void 0,clearTimeout(n.timer),d=!0,v.close(),o=void 0,P(),n.reject(t)}function j(t){if(!d)try{let n=x(t);if(n.kind==="ready"||n.kind==="closed"){if(!C(n.generation))throw new i("invalid-frame");if(BigInt(n.generation)<=a)return;if(n.kind==="ready")o?.({kind:"ready",generation:n.generation});else{let w=N(n.code);o?.({kind:"closed",generation:n.generation,code:w}),(!l||n.generation===l.generation)&&b(new i(w))}return}if(!u||!r||!M(n,r))return;if(n.error){let w=x(n.error);b(new i(N(w.code)));return}if(!C(n.generation))throw new i("invalid-frame");if(!I(n.token,32))throw new i("invalid-frame");if(l={generation:n.generation,token:n.token},BigInt(n.generation)<=a)throw new i("navigated");B=BigInt(n.generation);let m=x(n.limits);R(m);let y=u;u=void 0,clearTimeout(y.timer),y.resolve({generation:n.generation,token:n.token,limits:Object.freeze({...m})})}catch(n){b(k(n,"invalid-frame"))}}return{bootstrap:Object.freeze({documentNonce:s,activate(t){if(d||!t||t.documentNonce!==s||!C(t.navigation,!0)||!I(t.challenge,32))return!1;if(r)return r.navigation===t.navigation&&r.challenge===t.challenge;r=Object.freeze({navigation:t.navigation,documentNonce:s,challenge:t.challenge});let n=c;return c=void 0,n?.(),!0},hello(t){if(f||d)return Promise.reject(new i("not-ready"));f=!0;try{if(t=F(t),t.wireVersion!==3||!t.contractName||t.contractName.length>1024||!Number.isSafeInteger(t.contractMajor)||t.contractMajor<=0||t.contractMajor>4294967295||!I(t.schemaHash,64))throw new i("invalid-payload")}catch(n){return d=!0,v.close(),o=void 0,Promise.reject(k(n,"invalid-payload"))}return new Promise((n,m)=>{u={resolve:n,reject:m,timer:setTimeout(()=>{b(new i("deadline-exceeded","Native IPC admission timed out.")),d=!0,v.close(),o=void 0},h.handshakeTimeoutMs)};let y=()=>{if(!r)return;let w=r;Promise.resolve().then(()=>{if(!u||d)return;let p={...t,...w,kind:"hello",callId:E};return x(p),e.postMessage(p)}).then(p=>{p!==void 0&&(d?A(p,w):j(p))}).catch(p=>b(k(p)))};r?y():c=y})},subscribeControl(t){if(d)throw new i("closed");if(o)throw new i("invalid-frame","A transport already owns this document.");return o=t,{close(){o===t&&(o=void 0)}}},disconnect(t,n){if(!d){if(l){if(t!==l.generation||n!==l.token)return}else if(t!=="0"||n!=="")return;d=!0,c=void 0,r=void 0,b(new i("closed")),v.close(),o=void 0,P()}}}),get generation(){return B},retire(){let t=u,n=o,m=l?.generation??"0";d=!0,u=void 0,o=void 0,r=void 0,c=void 0,t&&clearTimeout(t.timer),v.close(),P(),t?.reject(new i("navigated")),n?.({kind:"closed",generation:m,code:"navigated"})}}}function O(e,a,s){if("__webuiDesktopIpcV2"in e)throw new i("invalid-frame","Conflicting IPC bootstrap.");if(!Object.isExtensible(e))throw new i("invalid-frame","IPC bootstrap target is not extensible.");let r=L(a),c=Object.freeze({get documentNonce(){return r.bootstrap.documentNonce},activate:o=>r.bootstrap.activate(o),hello:o=>r.bootstrap.hello(o),subscribeControl:o=>r.bootstrap.subscribeControl(o),disconnect:(o,f)=>r.bootstrap.disconnect(o,f)});return Object.defineProperty(e,"__webuiDesktopIpcV2",{value:c,writable:!1,configurable:!1}),s?.(o=>{let f=r;try{f.retire()}finally{o&&(r=L(a,f.generation))}}),c}var g=window;if(g===g.top){let e=g.webkit?.messageHandlers?.webuiDesktopIpc,a=g.chrome?.webview;if(e||a){let s,r={postMessage:c=>e?e.postMessage(c):a.postMessage(c),subscribe(c){s=c;let o=d=>c(d.data),f=!0;return a&&a.addEventListener("message",o),{close(){f&&(f=!1,s===c&&(s=void 0),a&&a.removeEventListener("message",o))}}}};if("__webuiDesktopIpcReceiveV2"in g)throw new i("invalid-frame","Conflicting native IPC control receiver.");Object.defineProperty(g,"__webuiDesktopIpcReceiveV2",{value:Object.freeze(c=>s?.(c)),writable:!1,configurable:!1}),O(g,r,c=>{g.addEventListener("pagehide",o=>{o.isTrusted&&c(o.persisted)})})}}})();