(function(){let a=null,s=null,n=null,c=null,l="",i=0,u=!1;function f(e){return e instanceof Error?`${e.name||"Error"}: ${e.message||"Unknown worker error."}`:String(e||"Unknown worker error.")}function r(e,t){self.postMessage(e,t||[])}async function y(){if(a||!n)return i;const e=await n.getFile();return Number.isFinite(e.size)?e.size:i}async function g(e){c=await(await navigator.storage.getDirectory()).getDirectoryHandle("ofdl-temp",{create:!0}),l=e.tempBasename,n=await c.getFileHandle(l,{create:!0}),u=!1,i=0;const t=n;if(typeof t.createSyncAccessHandle=="function"){a=await t.createSyncAccessHandle(),s=null;return}s=await n.createWritable(),a=null}async function h(e){const t=e.data instanceof Uint8Array?e.data:new Uint8Array(e.data);if(a){const o=a.write(t,{at:e.position});i=Math.max(i,e.position+o);return}if(!s)throw new Error("Browser storage writable is not open.");await s.write({type:"write",position:e.position,data:t}),i=Math.max(i,e.position+t.byteLength)}async function k(e){const t=Math.max(0,Math.floor(e.length));if(t===0)return new Uint8Array(0);if(a){const d=new Uint8Array(t),b=a.read(d,{at:e.offset});return d.subarray(0,Math.max(0,b))}if(!n)throw new Error("Browser storage file handle is not available.");const o=(await n.getFile()).slice(e.offset,e.offset+t);return new Uint8Array(await o.arrayBuffer())}async function w(){if(!u){if(u=!0,a){a.flush(),a.close(),a=null;return}s&&(await s.close(),s=null)}}async function p(){try{u||await w()}catch(e){}if(c&&l)try{await c.removeEntry(l)}catch(e){}n=null,c=null,l="",i=0}async function m(e){const t=e.data;try{switch(t.type){case"open":await g(t),r({id:t.id,ok:!0});return;case"write":await h(t),r({id:t.id,ok:!0});return;case"getSize":r({id:t.id,ok:!0,payload:await y()});return;case"read":{const o=await k(t);r({id:t.id,ok:!0,payload:o},[o.buffer]);return}case"close":await w(),r({id:t.id,ok:!0});return;case"remove":await p(),r({id:t.id,ok:!0});return;default:throw new Error("Unknown browser storage worker request.")}}catch(o){r({id:t.id,ok:!1,error:f(o)})}}self.onmessage=e=>{m(e)},self.addEventListener("error",e=>{r({id:-1,ok:!1,error:f(e.error||e.message)})}),self.addEventListener("unhandledrejection",e=>{r({id:-1,ok:!1,error:f(e.reason)})})})();
