(()=>{const e="recording-";let t=null,r=null,n=0,a=0,o=!1;const c=e=>{self.postMessage(e)};let s=Promise.resolve();const i=e=>{const t=s.then(e).catch(e=>({__error:e}));return s=t,t};self.onmessage=s=>{const u=s.data;if(!u||!u.type||!u.requestId)return;const y=e=>({error:String(e?.message||e),errorName:e?.name||"Error"});"open"!==u.type?"write"!==u.type?"close"!==u.type?"abort"!==u.type||i(async()=>{try{await(async()=>{if(r){try{r.close()}catch{}r=null}if(t)try{const e=await navigator.storage.getDirectory();await e.removeEntry(t).catch(()=>{})}catch{}t=null,n=0,a=0,o=!0})(),c({type:"aborted",requestId:u.requestId,ok:!0})}catch(e){c({type:"aborted",requestId:u.requestId,ok:!1,...y(e)})}}):i(async()=>{try{const e=(()=>{if(!r)return{byteSize:n,chunkCount:a,fileName:t};const e="undefined"!=typeof performance?performance.now():Date.now();try{r.flush()}catch{}const c="undefined"!=typeof performance?performance.now():Date.now(),s="function"==typeof r.getSize?r.getSize():n,i="undefined"!=typeof performance?performance.now():Date.now();r.close();const u="undefined"!=typeof performance?performance.now():Date.now();return r=null,o=!0,{byteSize:s,chunkCount:a,fileName:t,timings:{flushMs:Math.round(c-e),getSizeMs:Math.round(i-c),closeHandleMs:Math.round(u-i)}}})();c({type:"closed",requestId:u.requestId,ok:!0,byteSize:e.byteSize,chunkCount:e.chunkCount,fileName:e.fileName,timings:e.timings})}catch(e){c({type:"closed",requestId:u.requestId,ok:!1,...y(e)})}}):i(async()=>{try{const{totalSize:e}=await(async e=>{if(o)throw new Error("opfs-writer-closed");if(!r)throw new Error("opfs-writer-not-open");const t=await e.arrayBuffer(),c=new Uint8Array(t),s=r.write(c,{at:n});return n+="number"==typeof s?s:c.byteLength,a+=1,{totalSize:n}})(u.chunk);c({type:"written",requestId:u.requestId,ok:!0,totalSize:e})}catch(e){c({type:"written",requestId:u.requestId,ok:!1,...y(e)})}}):i(async()=>{try{const{fileName:s}=await(async(c,s)=>{const i=await navigator.storage.getDirectory(),u=`${e}${c}.${"webm"===s?"webm":"mp4"}`,y=await i.getFileHandle(u,{create:!0}),l=await(async e=>{const t=[0,200,400,600,800];let r=null;for(let n=0;n<t.length;n+=1){t[n]>0&&await new Promise(e=>setTimeout(e,t[n]));try{return await e.createSyncAccessHandle()}catch(e){if(r=e,"NoModificationAllowedError"!==e?.name)break}}throw r})(y);return l.truncate(0),t=u,r=l,n=0,a=0,o=!1,await(async(t=null)=>{try{const r=await navigator.storage.getDirectory();for await(const[n]of r.entries())n.startsWith(e)&&(t&&n===t||await r.removeEntry(n).catch(()=>{}))}catch{}})(u),{fileName:u}})(u.recordingId,u.extension);c({type:"ready",requestId:u.requestId,ok:!0,fileName:s})}catch(e){c({type:"ready",requestId:u.requestId,ok:!1,...y(e)})}})}})();