import{c as u,e as p}from"./es6--XPcqeHG-B7r8Rb2B.js";import"./index-D3uELfdo.js";const{File:r,Blob:y,DOMException:s}=u,{INVALID:_,GONE:n,MISMATCH:w,MOD_ERR:z,SYNTAX:l,SECURITY:I,DISALLOWED:m}=p;class b{constructor(e,i){this.fileHandle=e,this.file=i,this.size=i.size,this.position=0}write(e){let i=this.file;if(typeof e=="object"){if(e.type==="write"){if(Number.isInteger(e.position)&&e.position>=0&&(this.position=e.position,this.size<e.position&&(this.file=new r([this.file,new ArrayBuffer(e.position-this.size)],this.file.name,this.file))),!("data"in e))throw new s(...l("write requires a data argument"));e=e.data}else if(e.type==="seek")if(Number.isInteger(e.position)&&e.position>=0){if(this.size<e.position)throw new s(..._);this.position=e.position;return}else throw new s(...l("seek requires a position argument"));else if(e.type==="truncate")if(Number.isInteger(e.size)&&e.size>=0){i=e.size<this.size?new r([i.slice(0,e.size)],i.name,i):new r([i,new Uint8Array(e.size-this.size)],i.name),this.size=i.size,this.position>i.size&&(this.position=i.size),this.file=i;return}else throw new s(...l("truncate requires a size argument"))}e=new y([e]);let t=this.file;const o=t.slice(0,this.position),d=t.slice(this.position+e.size);let h=this.position-o.size;h<0&&(h=0),t=new r([o,new Uint8Array(h),e,d],t.name),this.size=t.size,this.position+=e.size,this.file=t}close(){if(this.fileHandle._deleted)throw new s(...n);this.fileHandle._file=this.file,this.file=this.position=this.size=null,this.fileHandle.onclose&&this.fileHandle.onclose(this.fileHandle)}}class a{constructor(e="",i=new r([],e),t=!0){this._file=i,this.name=e,this.kind="file",this._deleted=!1,this.writable=t,this.readable=!0}async getFile(){if(this._deleted)throw new s(...n);return this._file}async createWritable(e){if(!this.writable)throw new s(...m);if(this._deleted)throw new s(...n);const i=e.keepExistingData?await this.getFile():new r([],this.name);return new b(this,i)}async isSameEntry(e){return this===e}async _destroy(){this._deleted=!0,this._file=null}}class f{constructor(e,i=!0){this.name=e,this.kind="directory",this._deleted=!1,this._entries={},this.writable=i,this.readable=!0}async*entries(){if(this._deleted)throw new s(...n);yield*Object.entries(this._entries)}async isSameEntry(e){return this===e}async getDirectoryHandle(e,i){if(this._deleted)throw new s(...n);const t=this._entries[e];if(t){if(t instanceof a)throw new s(...w);return t}else{if(i.create)return this._entries[e]=new f(e);throw new s(...n)}}async getFileHandle(e,i){const t=this._entries[e],o=t instanceof a;if(t&&o)return t;if(t&&!o)throw new s(...w);if(!t&&!i.create)throw new s(...n);if(!t&&i.create)return this._entries[e]=new a(e)}async removeEntry(e,i){const t=this._entries[e];if(!t)throw new s(...n);await t._destroy(i.recursive),delete this._entries[e]}async _destroy(e){for(let i of Object.values(this._entries)){if(!e)throw new s(...z);await i._destroy(e)}this._entries={},this._deleted=!0}}const g=new f(""),A=()=>g;export{a as FileHandle,f as FolderHandle,b as Sink,A as default};
