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