import{o as e}from"./chunk-BPLyUllO.js";import{ya as t}from"./_virtual_wxt-plugins-xyJ09udC.js";import{a as n,l as r,o as i,s as a}from"./chunk-VCGWCGR7-BsHMpZ7V.js";var o=e(t(),1),s=(e=``)=>{let t=a(),s,c=[],l=i(),u=new Set,d=new Set,f=(e,t)=>{switch(e.status){case`undeliverable`:c.some(t=>t.message.messageID===e.message.messageID)||(c=[...c,{message:e.message,resolvedDestination:e.resolvedDestination}]);return;case`deliverable`:c=c.reduce((r,i)=>i.resolvedDestination===e.deliverableTo?(n.toBackground(t,{type:`deliver`,message:i.message}),r):[...r,i],[]);return;case`delivered`:e.receipt.message.messageType===`message`&&l.add(e.receipt);return;case`incoming`:e.message.messageType===`reply`&&l.remove(e.message.messageID),u.forEach(n=>n(e.message,t));return;case`terminated`:{let t=l.entries().filter(t=>e.fingerprint===t.to);l.remove(t),t.forEach(({message:e})=>d.forEach(t=>t(e)))}}},p=()=>{s=o.default.runtime.connect({name:r({endpointName:e,fingerprint:t})}),s.onMessage.addListener(f),s.onDisconnect.addListener(p),n.toBackground(s,{type:`sync`,pendingResponses:l.entries(),pendingDeliveries:[...new Set(c.map(({resolvedDestination:e})=>e))]})};return p(),{onFailure(e){d.add(e)},onMessage(e){u.add(e)},postMessage(e){n.toBackground(s,{type:`deliver`,message:e})}}};export{s as t};