import{F as e,a as s,b as t,c as o,E as n}from"../types/file-upload.enums.esm.js";const r=(r,a)=>{a.setIsDisabled((()=>r.state===e.DISABLED)),a.onStateChange((e=>{r.state=e})),a.onFileDrop((a=>{if(r.state===e.DISABLED)return;r.errorMessage="";const i=function(e){const s={B:1,KB:1024,MB:1048576,GB:1024**3},t=parseFloat(e),o=e.replace(/[0-9.\s]/g,"").toUpperCase(),n=s[o]||1;return t*n}(r.size),l=r.uploadedFiles;if(!r.allowMultiple&&l.length+a.length>1)return r.errorMessage=s.MULTIPLE_NOT_ALLOWED,void r.requestUpdate();let u=!1,c=!1;const d=[],p=[];for(const e of a)e.size>i?(u=!0,p.push(e)):r.allowDuplicates||!l.some((s=>s.name===e.name&&s.size===e.size&&s.type===e.type))?p.includes(e)||d.push(e):(c=!0,p.push(e));if(u){r.errorMessage=s.SIZE_LIMIT_EXCEEDED;const e=p.filter((e=>e.size>i));r.dispatchEvent(new CustomEvent(t.NILE_ERROR,{detail:{files:e,type:o.SIZE_LIMIT_EXCEEDED,errorType:n.VALIDATION,message:s.SIZE_LIMIT_EXCEEDED},bubbles:!0,composed:!0})),r.dispatchEvent(new CustomEvent(t.NILE_SIZE_EXCEED_FILES,{detail:{files:p}})),r.requestUpdate()}c&&0===d.length&&(r.errorMessage=s.DUPLICATES_NOT_ALLOWED,r.requestUpdate());let m=[];if(r.allowedTypes.length>0)for(const e of d)r.allowedTypes.includes(e.type)?m.push(e):r.errorMessage=s.INVALID_FORMAT;0===m.length&&(m=d),r.uploadedFiles=[...l,...m],r.requestUpdate()})),a.setErrorMessage((e=>{r.errorMessage=e}))},a=e=>{document.addEventListener("dragover",e.preventDragOver,{passive:!1}),document.addEventListener("drop",e.preventDrop,{passive:!1})},i=(s,t,o)=>{s.addEventListener("mouseenter",(t=>{return o=s.setState,void(s.state===e.DEFAULT&&o(e.HOVER));var o})),s.addEventListener("mouseleave",(t=>{return o=s.setState,void(s.state===e.HOVER&&o(e.DEFAULT));var o})),s.addEventListener("dragenter",(e=>t.dragEnter(e))),s.addEventListener("dragleave",(e=>t.dragLeave(e))),s.addEventListener("dragover",(e=>t.dragOver(e))),s.addEventListener("drop",(e=>t.drop(e)));const n=s.browseFiles.bind(s);new ResizeObserver((e=>{for(const t of e)t.contentRect.width<400?s.horizontalDiv.addEventListener("click",n):s.horizontalDiv.removeEventListener("click",n)})).observe(s.horizontalDiv),u(s,o),d(s.uploadedFiles,s)};function l(e){const{uploadedFiles:r,fileUploadUrl:a,uploadRequests:i}=e;if(a&&r?.length)for(const l of r){if(i.has(l))continue;if(e.doNotUpload.includes(l))continue;if(l.errorMessage)continue;const r=new FormData;r.append("file",l);const u=new XMLHttpRequest;i.set(l,u),e.emit(t.NILE_UPLOADING),u.upload.onprogress=s=>{const o=Math.floor(s.loaded/s.total*100);e.dispatchEvent(new CustomEvent(t.NILE_UPLOAD_PROGRESS,{detail:{file:l,progress:o},bubbles:!0,composed:!0}))},u.onload=()=>{u.status>=200&&u.status<300?e.dispatchEvent(new CustomEvent(t.NILE_UPLOAD_SUCCESS,{detail:{file:l,response:u.response},bubbles:!0,composed:!0})):(e.errorMessage=s.SERVER_SIDE_ERROR,e.dispatchEvent(new CustomEvent(t.SERVER_SIDE_ERROR,{detail:{file:l,status:u.status,response:u.response}})),e.dispatchEvent(new CustomEvent(t.NILE_ERROR,{detail:{type:o.SERVER_SIDE_ERROR,file:l,status:u?.status||0,response:u?.response,message:s.SERVER_SIDE_ERROR,errorType:n.SERVER},bubbles:!0,composed:!0})),e.requestUpdate())},u.onerror=()=>{i.delete(l),e.doNotUpload.push(l),e.errorMessage=s.NETWORK_ERROR,e.dispatchEvent(new CustomEvent(t.NILE_ERROR,{detail:{type:o.NETWORK_ERROR,file:l,status:u.status,response:u.response,errorType:n.NETWORK,message:s.NETWORK_ERROR},bubbles:!0,composed:!0})),e.dispatchEvent(new CustomEvent(t.NILE_NETWORK_ERROR,{detail:{file:l,status:u.status,response:u.response},bubbles:!0,composed:!0}))},u.open("POST",a),u.send(r)}}const u=(e,r)=>{document.addEventListener(t.NILE_CANCEL_UPLOAD,(a=>{const i=a.detail.file,l=r.get(i);l&&l.abort(),r.delete(i),e.uploadedFiles=e.uploadedFiles.filter((e=>e!==i)),e.dispatchEvent(new CustomEvent(t.NILE_ERROR,{detail:{type:o.UPLOAD_CANCELLED,file:i,message:s.UPLOAD_CANCELLED,errorType:n.UPLOAD_CANCELLED},bubbles:!0,composed:!0})),e.dispatchEvent(new CustomEvent(t.NILE_UPLOAD_CANCELLED,{detail:{file:i}}))}))};const c=e=>{e.preventDefault?.(),e.stopPropagation()},d=(e,s)=>{document.addEventListener("nile-remove",(t=>{const{value:o}=t.detail;e=e.filter((e=>e!==o)),s.uploadedFiles=e,s.errorMessage="",s.requestUpdate()}))},p=e=>{("vertical"===e.variant&&e.errorMessage.length>34||"horizontal"===e.variant&&e.errorMessage.length>96)&&(e.isStringTruncated=!0,e.requestUpdate())};export{i as a,a as b,c as p,r as s,p as t,l as u};
