import{defineComponent as B,ref as E,onMounted as K,onBeforeUnmount as $,createVNode as x}from"vue";import I from"lodash-es/partition";import N from"../_util/pickAttrs.mjs";import C from"./attr-accept.mjs";import{uploadProps as V}from"./interface.mjs";import z from"./request.mjs";import G from"./traverseFileTree.mjs";import v from"./uid.mjs";const L=B({compatConfig:{MODE:3},name:"AjaxUploader",inheritAttrs:!1,props:V(),setup(r,{slots:M,attrs:F,expose:T}){const w=E(v()),c={},D=E();let b=!1;const q=async(e,t)=>{const{beforeUpload:n}=r;let o=e;if(n){try{o=await n(e,t)}catch{o=!1}if(o===!1)return{origin:e,parsedFile:null,action:null,data:null}}const{action:s}=r;let a;typeof s=="function"?a=await s(e):a=s;const{data:i}=r;let u;typeof i=="function"?u=await i(e):u=i;const l=(typeof o=="object"||typeof o=="string")&&o?o:e;let f;l instanceof File?f=l:f=new File([l],e.name,{type:e.type});const d=f;return d.uid=e.uid,{origin:e,data:u,parsedFile:d,action:a}},O=({data:e,origin:t,action:n,parsedFile:o})=>{if(!b)return;const{onStart:s,customRequest:a,name:i,headers:u,withCredentials:l,method:f}=r,{uid:d}=t,g=a||z,k={action:n,filename:i,data:e,file:o,headers:u,withCredentials:l,method:f||"post",onProgress:m=>{const{onProgress:y}=r;y?.(m,o)},onSuccess:(m,y)=>{const{onSuccess:p}=r;p?.(m,o,y),delete c[d]},onError:(m,y)=>{const{onError:p}=r;p?.(m,y,o),delete c[d]}};s(t),c[d]=g(k)},S=()=>{w.value=v()},P=e=>{if(e){const t=e.uid?e.uid:e;c[t]&&c[t].abort&&c[t].abort(),delete c[t]}else Object.keys(c).forEach(t=>{c[t]&&c[t].abort&&c[t].abort(),delete c[t]})};K(()=>{b=!0}),$(()=>{b=!1,P()});const h=e=>{const t=[...e],n=t.map(o=>(o.uid=v(),q(o,t)));Promise.all(n).then(o=>{const{onBatchStart:s}=r;s?.(o.map(({origin:a,parsedFile:i})=>({file:a,parsedFile:i}))),o.filter(a=>a.parsedFile!==null).forEach(a=>{O(a)})})},U=e=>{const{accept:t,directory:n}=r,{files:o}=e.target,s=[...o].filter(a=>!n||C(a,t));h(s),S()},A=e=>{const t=D.value;if(!t)return;const{onClick:n}=r;t.click(),n&&n(e)},R=e=>{e.key==="Enter"&&A(e)},j=e=>{const{multiple:t}=r;if(e.preventDefault(),e.type!=="dragover")if(r.directory)G(Array.prototype.slice.call(e.dataTransfer.items),h,n=>C(n,r.accept));else{const n=I(Array.prototype.slice.call(e.dataTransfer.files),a=>C(a,r.accept));let o=n[0];const s=n[1];t===!1&&(o=o.slice(0,1)),h(o),s.length&&r.onReject&&r.onReject(s)}};return T({abort:P}),()=>{const{componentTag:e,prefixCls:t,disabled:n,id:o,multiple:s,accept:a,capture:i,directory:u,openFileDialogOnClick:l,onMouseenter:f,onMouseleave:d,...g}=r,k={[t]:!0,[`${t}-disabled`]:n,[F.class]:!!F.class},m=u?{directory:"directory",webkitdirectory:"webkitdirectory"}:{};return x(e,{...n?{}:{onClick:l?A:()=>{},onKeydown:l?R:()=>{},onMouseenter:f,onMouseleave:d,onDrop:j,onDragover:j,tabindex:"0"},class:k,role:"button",style:F.style},{default:()=>[x("input",{...N(g,{aria:!0,data:!0}),id:o,type:"file",ref:D,onClick:p=>p.stopPropagation(),onCancel:p=>p.stopPropagation(),key:w.value,style:{display:"none"},accept:a,...m,multiple:s,onChange:U,...i!=null?{capture:i}:{}},null),M.default?.()]})}}});export{L as default};
