import{t as e}from"./FileSystemHandle.DIBYSYyr.js";import{t}from"./util.DyniJHYf.js";import{t as n}from"./config.C6PEK-vI.js";var{WritableStream:r}=n,i=class e extends r{#e;constructor(t){super(t),this.#e=t,Object.setPrototypeOf(this,e.prototype),this._closed=!1}async close(){this._closed=!0;let e=this.getWriter(),t=e.close();return e.releaseLock(),t}seek(e){return this.write({type:`seek`,position:e})}truncate(e){return this.write({type:`truncate`,size:e})}write(e){if(this._closed)return Promise.reject(TypeError(`Cannot write to a CLOSED writable stream`));let t=this.getWriter(),n=t.write(e);return t.releaseLock(),n}};Object.defineProperty(i.prototype,Symbol.toStringTag,{value:`FileSystemWritableFileStream`,writable:!1,enumerable:!1,configurable:!0}),Object.defineProperties(i.prototype,{close:{enumerable:!0},seek:{enumerable:!0},truncate:{enumerable:!0},write:{enumerable:!0}}),globalThis.FileSystemFileHandle&&!globalThis.FileSystemFileHandle.prototype.createWritable&&!globalThis.FileSystemWritableFileStream&&(globalThis.FileSystemWritableFileStream=i);var{INVALID:a,SYNTAX:o,GONE:s}=t,c=Symbol(`adapter`),l=class extends e{[c];constructor(e){super(e),this[c]=e}async createWritable(e={}){return new i(await this[c].createWritable(e))}async getFile(){return this[c].getFile()}};if(Object.defineProperty(l.prototype,Symbol.toStringTag,{value:`FileSystemFileHandle`,writable:!1,enumerable:!1,configurable:!0}),Object.defineProperties(l.prototype,{createWritable:{enumerable:!0},getFile:{enumerable:!0}}),globalThis.FileSystemFileHandle&&!globalThis.FileSystemFileHandle.prototype.createWritable){let e=new WeakMap,t,n=()=>{let e,t;onmessage=async n=>{let r=n.ports[0],i=n.data;switch(i.type){case`open`:let n=i.name,r=await navigator.storage.getDirectory();for(let e of i.path)r=await r.getDirectoryHandle(e);e=await r.getFileHandle(n),t=await e.createSyncAccessHandle();break;case`write`:t.write(i.data,{at:i.position}),t.flush();break;case`truncate`:t.truncate(i.size);break;case`abort`:case`close`:t.close();break}r.postMessage(0)}};globalThis.FileSystemFileHandle.prototype.createWritable=async function(r){if(!t){let e=`(${n.toString()})()`,r=new Blob([e],{type:`text/javascript`});t=URL.createObjectURL(r)}let c=new Worker(t,{type:`module`}),l=0,u=new TextEncoder,d=await this.getFile().then(e=>e.size),f=e=>new Promise((t,n)=>{let r=new MessageChannel;r.port1.onmessage=e=>{e.data instanceof Error?n(e.data):t(e.data),r.port1.close(),r.port2.close(),r.port1.onmessage=null},c.postMessage(e,[r.port2])}),p=await navigator.storage.getDirectory(),m=await e.get(this),h=await p.resolve(m);if(h===null)throw new DOMException(...s);return await f({type:`open`,path:h,name:this.name}),r?.keepExistingData===!1&&(await f({type:`truncate`,size:0}),d=0),new i({start:e=>{},async write(e){if(e=e?.constructor===Object?{...e}:{type:`write`,data:e,position:l},e.type===`write`){if(!(`data`in e))throw await f({type:`close`}),new DOMException(...o(`write requires a data argument`));if(e.position??=l,typeof e.data==`string`)e.data=u.encode(e.data);else if(e.data instanceof ArrayBuffer)e.data=new Uint8Array(e.data);else if(!(e.data instanceof Uint8Array)&&ArrayBuffer.isView(e.data))e.data=new Uint8Array(e.data.buffer,e.data.byteOffset,e.data.byteLength);else if(!(e.data instanceof Uint8Array)){let t=await new Response(e.data).arrayBuffer();e.data=new Uint8Array(t)}Number.isInteger(e.position)&&e.position>=0&&(l=e.position),l+=e.data.byteLength,d+=e.data.byteLength}else if(e.type===`seek`)if(Number.isInteger(e.position)&&e.position>=0){if(d<e.position)throw new DOMException(...a);console.log(`seeking`,e),l=e.position;return}else throw await f({type:`close`}),new DOMException(...o(`seek requires a position argument`));else if(e.type===`truncate`)if(Number.isInteger(e.size)&&e.size>=0)d=e.size,l>d&&(l=d);else throw await f({type:`close`}),new DOMException(...o(`truncate requires a size argument`));await f(e)},async close(){await f({type:`close`}),c.terminate()},async abort(e){await f({type:`abort`,reason:e}),c.terminate()}})};let r=FileSystemDirectoryHandle.prototype.getFileHandle;FileSystemDirectoryHandle.prototype.getFileHandle=async function(...t){let n=await r.call(this,...t);return e.set(n,this),n}}export{l as FileSystemFileHandle,l as default,i as t};