/* RainLoop Webmail (c) RainLoop Team | MIT */ (e=>{const t=20,n=e=>void 0!==e,r=(e,r,i,o)=>{if(e&&e.length){let a=i=n(i)?parseInt(i||0,10):t,s=null,d=0{e&&(!d||0<=--i?(s=l(e))&&r(s):d&&!u&&0>i&&o&&(u=!0,o(a)))})}},i=(e,t)=>Object.entries(t).forEach(([t,n])=>e.addEventListener(t,n)),l=e=>{let t=n(e.fileName)?e.fileName:n(e.name)?e.name:null,r=n(e.fileSize)?e.fileSize:n(e.size)?e.size:null,i=n(e.type)?e.type:null;return"/"===t.charAt(0)&&(t=t.substr(1)),i||0!==r?{FileName:t,Size:r,Type:i,Folder:"",File:e}:null},o=e=>{try{return e.dataTransfer.types.includes("Files")}catch(e){return!1}};class a extends Array{constructor(e){super(),this.limit=parseInt(e||0,10)}push(e,...t){this.limit>this.length&&(super.push([e,t]),this.call())}call(){if(!this.running){let e;for(this.running=!0;e=this.shift();)e[0](...e[1]);this.running=!1}}}class s{constructor(n){const l=this;l.oEvents={onSelect:null,onStart:null,onComplete:null,onProgress:null,onDragEnter:null,onDragLeave:null,onBodyDragEnter:null,onBodyDragLeave:null,onLimitReached:null},n=Object.assign({action:"",name:"juaFile",hidden:{},disableMultiple:!1,queueSize:10,clickElement:null,dragAndDropElement:null,dragAndDropBodyElement:null,disableDocumentDropPrevent:!1,multipleSizeLimit:t},n||{}),l.oXhrs={},l.oUids={},l.oOptions=n,l.oQueue=new a(n.queueSize);let s=n.clickElement;if(s&&(s.style.position="relative",s.style.overflow="hidden","inline"===s.style.display&&(s.style.display="inline-block"),l.generateNewInput(s)),s=n.dragAndDropElement){let t=n.dragAndDropBodyElement||e;n.disableDocumentDropPrevent||e.addEventListener("dragover",e=>{if(o(e))try{e.dataTransfer.dropEffect="none",e.preventDefault()}catch(e){console.error(e)}}),t&&i(t,{dragover:()=>l.docTimer.clear(),dragenter:e=>{o(e)&&(l.docTimer.clear(),e.preventDefault(),l.runEvent("onBodyDragEnter",[e]))},dragleave:e=>e.dataTransfer&&l.docTimer.start(()=>l.runEvent("onBodyDragLeave",[e])),drop:e=>{if(e.dataTransfer){let t=o(e);return t&&e.preventDefault(),l.runEvent("onBodyDragLeave",[e]),!t}return!1}}),i(s,{dragenter:e=>{o(e)&&(l.docTimer.clear(),e.preventDefault(),l.runEvent("onDragEnter",[s,e]))},dragover:e=>{if(o(e))try{let t=e.dataTransfer.effectAllowed;l.docTimer.clear(),e.dataTransfer.dropEffect="move"===t||"linkMove"===t?"move":"copy",e.stopPropagation(),e.preventDefault()}catch(e){console.error(e)}},dragleave:t=>{if(t.dataTransfer){let n=e.elementFromPoint(t.clientX,t.clientY);n&&s.contains(n)||(l.docTimer.clear(),l.runEvent("onDragLeave",[s,t]))}},drop:e=>{o(e)&&(e.preventDefault(),r(e.files||e.dataTransfer.files,e=>{e&&(l.addNewFile(e),l.docTimer.clear())},n.multipleSizeLimit,l.getEvent("onLimitReached"))),l.runEvent("onDragLeave",[e])}})}}on(e,t){return this.oEvents[e]=t,this}runEvent(e,t){this.oEvents[e]&&this.oEvents[e].apply(null,t||[])}getEvent(e){return this.oEvents[e]||null}addNewFile(e){this.addFile("jua-uid-"+s.randomId(16)+"-"+Date.now().toString(),e)}addFile(e,t){const n=this.getEvent("onSelect");!t||n&&!1===n(e,t)?this.cancel(e):(this.oUids[e]=!0,this.oQueue.push((...e)=>this.uploadTask(...e),e,t))}uploadTask(e,t){if(!1===this.oUids[e]||!t||!t.File)return!1;try{const r=this,i=new XMLHttpRequest,l=new FormData,o=this.oOptions.action,a=this.oOptions.hidden,s=this.getEvent("onStart"),d=this.getEvent("onProgress");return i.open("POST",o,!0),d&&i.upload&&(i.upload.onprogress=(t=>{t&&t.lengthComputable&&n(t.loaded)&&n(t.total)&&d(e,t.loaded,t.total)})),i.onreadystatechange=(()=>{if(4===i.readyState){delete r.oXhrs[e];let t=!1,n=null;if(200===i.status)try{n=JSON.parse(i.responseText),t=!0}catch(e){console.error(e)}this.getEvent("onComplete")(e,t,t?n:null)}}),s&&s(e),l.append(this.oOptions.name,t.File),Object.entries(a).forEach(([e,n])=>l.append(e,("function"==typeof n?n(t):n).toString())),i.send(l),this.oXhrs[e]=i,!0}catch(e){console.error(e)}return!1}generateNewInput(t){if(t){const n=this,i=e.createElement("input"),l=()=>i.click();i.type="file",i.tabIndex=-1,i.style.display="none",i.multiple=!n.oOptions.disableMultiple,t.addEventListener("click",l),i.addEventListener("input",()=>{const e=e=>{n.addNewFile(e),setTimeout(()=>{i.remove(),t.removeEventListener("click",l),n.generateNewInput(t)},10)};i.files&&i.files.length?r(i.files,e,n.oOptions.multipleSizeLimit,n.getEvent("onLimitReached")):e({FileName:i.value.split("\\").pop().split("/").pop(),Size:null,Type:null,Folder:"",File:null})})}}cancel(e){if(this.oUids[e]=!1,this.oXhrs[e]){try{this.oXhrs[e].abort&&this.oXhrs[e].abort()}catch(e){console.error(e)}delete this.oXhrs[e]}}}s.randomId=(e=>{let t=new Uint8Array((e||32)/2);return crypto.getRandomValues(t),t.map(e=>e.toString(16).padStart(2,"0")).join("")}),s.prototype.docTimer={start:function(e){this.clear(),this.timer=setTimeout(e,200)},clear:function(){this.timer&&clearTimeout(this.timer),this.timer=0}},this.Jua=s})(document);