(()=>{let e=null;const r=new Map;chrome.runtime.onMessage.addListener(t=>{if(t&&"cancel-remux"===t.type){if(e){try{e.terminate()}catch{}e=null}for(const[,e]of r)e?.sendResponse?.({ok:!1,error:"cancelled"});return r.clear(),!1}}),chrome.runtime.onMessage.addListener((t,s,o)=>{if(!t||"remux-start"!==t.type&&"webm-start"!==t.type)return;const{requestId:n,inputFileName:u,outputFileName:a}=t;if(!n||!u||!a)return o({ok:!1,error:"invalid-remux-start-payload"}),!1;const d="webm-start"===t.type?"webm":"remux";try{const t=e||(e=new Worker(chrome.runtime.getURL("remuxworker.bundle.js")),e.onmessage=e=>{const t=e.data;if(!t||!t.requestId)return;const s=r.get(t.requestId);if("progress"!==t.type)return"done"===t.type?(t.requestId,t.outputFileName,r.delete(t.requestId),void s?.sendResponse?.({ok:!0,outputFileName:t.outputFileName})):"error"===t.type?(t.requestId,t.error,r.delete(t.requestId),void s?.sendResponse?.({ok:!1,error:t.error})):void 0;chrome.runtime.sendMessage({type:"remux-progress",requestId:t.requestId,progress:t.progress}).catch(()=>{})},e.onerror=t=>{const s=String(t?.message||t||"worker-error");for(const[e,t]of r)t?.sendResponse?.({ok:!1,error:s});r.clear();try{e.terminate()}catch{}e=null},e);r.set(n,{sendResponse:o}),t.postMessage({type:d,requestId:n,inputFileName:u,outputFileName:a})}catch(e){return r.delete(n),o({ok:!1,error:String(e?.message||e||"remux-start-failed")}),!1}return!0})})();