var t,e,s,i,n,r,h,a=function(t,e,s){if(!e.has(t))throw new TypeError("attempted to set private field on non-instance");return e.set(t,s),s},o=function(t,e){if(!e.has(t))throw new TypeError("attempted to get private field on non-instance");return e.get(t)};class c{constructor(c){t.set(this,new Map),e.set(this,new Map),s.set(this,new Map),i.set(this,void 0),n.set(this,void 0),r.set(this,void 0),h.set(this,void 0),this.cancel=async t=>(o(this,s).get(t)&&(o(this,s).delete(t),this.executeObservers([t])),o(this,i).call(this,t)),this.delete=async e=>{const i=o(this,s).get(e),r=o(this,t).get(e);return i&&o(this,s).delete(e),r&&o(this,t).delete(e),(i||r)&&this.executeObservers([e]),o(this,n).call(this,e)},this.rename=async(e,i)=>{const n=o(this,s).get(e),h=o(this,t).get(e);return n&&o(this,s).set(e,Object.assign(Object.assign({},n),{file:Object.assign(Object.assign({},n.file),{name:i})})),h&&o(this,t).set(e,Object.assign(Object.assign({},h),{name:i})),(n||h)&&this.executeObservers([e]),o(this,r).call(this,e,i)},this.upload=async t=>{const{id:e,name:i,type:n}=t,r=await t.hash(),a=URL.createObjectURL(t.blob),c={hash:r,id:e,name:i,path:"",type:n,url:a};o(this,s).set(t.id,{file:c,progress:0}),this.executeObservers([e]);const u=await o(this,h).call(this,t),p=Object.assign(Object.assign({},c),u);return a!==p.url&&URL.revokeObjectURL(c.url),o(this,s).set(t.id,{file:p,progress:1}),this.executeObservers([e]),p},a(this,i,c.cancel),a(this,n,c.delete),a(this,r,c.rename),a(this,h,c.upload)}static inMemory(t={}){return u(Object.assign({delay:{}},t))}get(e){return o(this,t).get(e)}getIds(){const e=[];return o(this,t).forEach((t,s)=>e.push(s)),o(this,s).forEach((t,s)=>e.push(s)),Array.from(new Set(e))}setFiles(e){const i=new Map;for(const t of e)o(this,s).delete(t.id),i.set(t.id,t);const n=this.getIds().filter(e=>{const s=o(this,t).get(e),n=i.get(e);return!this.isEqual(s,n)});a(this,t,i),n.length&&this.executeObservers(n)}setProgress(t,e){const i=o(this,s).get(t);i&&(o(this,s).set(t,Object.assign(Object.assign({},i),{progress:e})),this.executeObservers([t]))}subscribe(t,s){return o(this,e).set(t,s),t(this.getDataObject(s)),()=>this.unsubscribe(t)}unsubscribe(t){o(this,e).delete(t)}executeObservers(t){for(const[s,i]of o(this,e).entries())i&&!t.some(t=>i.indexOf(t)>=0)||s(this.getDataObject(i))}getDataObject(e){return this.getIds().reduce((i,n)=>{var r;if(e&&e.indexOf(n)<0)return i;const h=o(this,t).get(n),a=o(this,s).get(n);return i[n]={file:h||(null==a?void 0:a.file),progress:null!==(r=null==a?void 0:a.progress)&&void 0!==r?r:1},i},{})}isEqual(t,e){if(null==t||null==e)return t===e;if("number"==typeof t||"number"==typeof e)return t===e;for(const s in t)if(t[s]!==e[s])return!1;return!0}}t=new WeakMap,e=new WeakMap,s=new WeakMap,i=new WeakMap,n=new WeakMap,r=new WeakMap,h=new WeakMap;const u=t=>{const e=new Map,s=new Map,i=()=>{n.setFiles(Array.from(s.values()))},n=new c({cancel:async t=>{const s=e.get(t);s&&(window.clearTimeout(s),e.delete(t))},delete:async t=>{s.delete(t),i()},rename:async(t,e)=>{const n=s.get(t);n&&(s.set(n.id,Object.assign(Object.assign({},n),{name:e})),i())},upload:n=>{const{min:r=2e3,max:h=5e3}=t.delay;return new Promise(t=>{const a=Math.random()*(h-r+1)+r,o=window.setTimeout(async()=>{const r={hash:await n.hash(),id:n.id,name:n.name,path:"<in-memory>",type:n.type,url:URL.createObjectURL(n.blob)};e.delete(n.id),s.set(n.id,r),t(r),i()},a);e.set(n.id,o)})}});return n};export{c as F};