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