const a={timeout:1e4,retries:0,retryDelay:1e3};async function c(o,n={}){const s={...a,...n};let t=null;for(let r=0;r<=s.retries;r++){r>0&&await new Promise(e=>setTimeout(e,s.retryDelay));try{const e=await i(o,s.timeout);return e&&typeof e=="object"&&"success"in e?e:{success:!0,data:e}}catch(e){if(t=e instanceof Error?e:new Error(String(e)),e instanceof Error&&e.message.includes("Extension context invalidated"))break;r<s.retries&&console.warn(`Message send failed (attempt ${r+1}):`,e)}}return{success:!1,error:t?.message||"Unknown error occurred"}}function i(o,n){return new Promise((s,t)=>{const r=setTimeout(()=>{t(new Error(`Message timeout after ${n}ms`))},n);if(!chrome.runtime||!chrome.runtime.sendMessage){clearTimeout(r),t(new Error("Chrome runtime not available"));return}try{chrome.runtime.sendMessage(o,e=>{if(clearTimeout(r),chrome.runtime.lastError){t(new Error(chrome.runtime.lastError.message));return}s(e)})}catch(e){clearTimeout(r),t(e)}})}export{c as sendMessageSafe};
