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