import{n as e}from"./rolldown-runtime-DAXXjFlN.js";function t(e,t,n){window.chrome.webview.postMessage(JSON.stringify(n===void 0?{t:e,v:t}:{t:e,v:t,v2:n}))}function n(e,t,n){return new Promise((r,i)=>{let a=`${f}-${++p}`,o=setTimeout(()=>{u.delete(a),i(Error(`cast request timed out`))},n);u.set(a,{resolve:r,reject:i,timer:o});try{window.chrome.webview.postMessage(JSON.stringify({t:`cast.req`,rid:a,op:e,p:t}))}catch(e){clearTimeout(o),u.delete(a),i(e instanceof Error?e:Error(String(e)))}})}function r(e){return d.add(e),()=>{d.delete(e)}}function i(){h||!window.chrome?.webview||(h=!0,window.chrome.webview.addEventListener(`message`,e=>{let t=e?.data;if(t!=null){if(typeof t==`object`&&typeof t.t==`string`){a(t);return}if(typeof t==`string`&&t.startsWith(`{`))try{let e=JSON.parse(t);e?.channel&&o(e.channel,e.data)}catch{}}}),s&&window.chrome.webview.postMessage(`cider:ipc-hello`))}function a(e){let t=window.CiderApp;switch(e.t){case`cmd.nba`:e.v&&c.has(e.v)&&t?.nativeBridgeActions?.[e.v]?.();break;case`cmd.rpc`:e.v&&l.has(e.v)&&t?.RPC?.[e.v]?.();break;case`evt.window`:e.v===`minimized`?window.dispatchEvent(new CustomEvent(`cider:window-minimized`,{detail:{minimized:!!e.d}})):e.v===`resize-active`&&window.dispatchEvent(new CustomEvent(`cider:resize-active`,{detail:{active:!!e.d}}));break;case`cast.res`:{let t=e.rid,n=t?u.get(t):void 0;if(!t||!n)break;if(clearTimeout(n.timer),u.delete(t),e.ok)n.resolve(e.d);else{let t=Error(e.e?.message??`cast request failed`);e.e?.code&&(t.code=e.e.code),n.reject(t)}break}case`cast.evt`:for(let t of d)try{t(e.d)}catch(e){console.error(`[@ciderapp/frontend][messageBus] cast event listener threw`,e)}break}}function o(e,t){if(e===`update-window-maximized`){let e=window.CiderApp;e?.appState?.window&&(e.appState.window.maximized=t===`true`)}}var s,c,l,u,d,f,p,m,h,g,_,v=e((()=>{s=typeof window<`u`&&!!window.chrome?.webview&&(window.__CIDER_IPC__?.version??0)>=2,c=new Set([`togglePlayPause`,`next`,`previous`,`stop`,`toggleShuffle`]),l=new Set([`toggleRepeat`]),u=new Map,d=new Set,f=globalThis.crypto?.randomUUID?.()??`${Date.now().toString(36)}${Math.random().toString(36).slice(2,10)}`,p=0,m=()=>s,h=!1,g=null,_={setPlaybackTime(e){s?t(`ps.time`,e):window.chrome.webview.hostObjects.playbackState.SetPlaybackTime(e)},setPlaybackState(e){s?t(`ps.state`,e):window.chrome.webview.hostObjects.playbackState.SetPlaybackState(e)},setVolume(e){s?t(`ps.volume`,e):window.chrome.webview.hostObjects.playbackState.SetVolume(e)},setShuffleMode(e){s?t(`ps.shuffle`,String(e)):window.chrome.webview.hostObjects.playbackState.SetShuffleMode(e)},setRepeatMode(e){s?t(`ps.repeat`,String(e)):window.chrome.webview.hostObjects.playbackState.SetRepeatMode(e)},setItemStatus(e,n){s?t(`ps.itemStatus`,e,n):window.chrome.webview.hostObjects.playbackState.SetItemStatus(e,n)},setNowPlaying(e){e!==g&&(g=e,s?t(`ps.nowPlaying`,e):window.chrome.webview.hostObjects.playbackState.SetNowPlaying(e))}}}));export{r as a,i,m as n,_ as o,v as r,n as t};