import{aC as p,l as i,aD as c}from"./index.CL7yl-Wb.js";import"./vue.D1BjjNmv.js";function l(t,r,s){let e;s.response?e=`${s.response.error||s.response}`:s.responseText?e=`${s.responseText}`:e=`fail to post ${t} ${s.status}`;const o=new Error(e);return o.status=s.status,o.method="post",o.url=t,o}function d(t){const r=t.responseText||t.response;if(!r)return r;try{return JSON.parse(r)}catch{return r}}function f(t,r,s){if(typeof XMLHttpRequest>"u")return;const e=new XMLHttpRequest,o=t.action;e.timeout=t.timeout,e.upload&&(e.upload.onprogress=function(n){n.total>0&&(n.percent=n.loaded/n.total*100),t.onProgress(n)});const u=new FormData;t.data&&Object.keys(t.data).forEach(n=>{u.append(n,t.data[n])}),u.append(t.name,t.file,t.file.name),e.onerror=function(n){s(n)},e.onload=function(){if(e.status<200||e.status>=300)return t.onError(l(o,t,e));r(d(e))},e.open("post",o,!0),t.withCredentials&&"withCredentials"in e&&(e.withCredentials=!0);const a=t.headers||{};for(const n in a)a.hasOwnProperty(n)&&a[n]!==null&&e.setRequestHeader(n,a[n]);return e.send(u),e}function m(t){return new Promise((r,s)=>{f(t,async e=>{r(e)},e=>{s(e)})})}async function w(t){const{file:r,fileName:s,onProgress:e}=t,o=t.options,u=await c(r,s,o);o.data==null&&(o.data={}),o.data.key=u;const a={file:r,onProgress:e,timeout:6e4,...o};delete a.uploadRequest;let n=await(o.uploadRequest??m)(a);return o.successHandle&&(n=await o.successHandle(n,a)),n&&typeof n=="object"&&n.key==null&&(n.key=u),n}async function g(t){const{getConfig:r}=p(),s=r("form");return t.options=i.merge({},i.cloneDeep(s),t.options),await w(t)}export{g as upload};
