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