import{ag as W,r as m,N as Y,m as r,h as j,e as h,aO as V,at as ee,v as ae}from"./vendor-3e8OGtgt.js";import{q as P,aD as te,aE as se,an as ne,aF as N,aG as w,aH as le,aI as g,aC as A,aJ as ie,aK as oe,aL as re,aM as f,aN as ue,aO as ce,aP as de,aQ as fe,aR as ve,aS as me}from"./index-D8gWJXbr.js";async function he(c){const{data:u}=await P.mutate({mutation:te,variables:{command:{id:c}}});return u?.deleteFile}async function Fe(c){const{data:u}=await P.query({query:se,variables:{scope:c}});return u.fileUploadOptions}const z=ee(Fe),pe=3;function Ue(c,u){const{translate:C}=ne("file_error"),{t:I,n:b}=W(),E=m(0),v=m([]),X=e=>ve({method:"POST",onUploadProgress:a=>K(a,e)}),U=m(new Map),O={maxFileCount:oe,maxFileSize:ie,allowedExtensions:[]},d=m(O);async function q(){const e=await z(h(c));e&&(d.value={...O,...e})}const l=m([]);u&&Y(u,e=>{e&&l.value.length===0&&(l.value=[...h(e)]),e.forEach(a=>{if(g(a)){const t=l.value.find(s=>s.name===a.name);t&&(t.status="attached",t.url=a.url)}})},{immediate:!0});const y=r(()=>l.value.filter(N)),R=r(()=>y.value.length>0),S=r(()=>l.value.filter(w)),D=r(()=>l.value.filter(le)),T=r(()=>D.value.length>0),_=r(()=>l.value.filter(g)),L=r(()=>l.value.filter(A)),M=r(()=>l.value.filter(e=>!g(e))),Z=r(()=>M.value.length>0||j(u)&&_.value.length!==h(u).length),$=r(()=>[..._.value,...L.value]),B=r(()=>l.value.every(e=>g(e)||A(e)));function k(e){return l.value.length+e.length>d.value.maxFileCount?!1:(l.value.push(...e),!0)}function G(){R.value&&y.value.forEach(e=>{l.value.some(t=>t.name===e.name&&t!==e)&&f(e,F("ALREADY_EXISTS")),d.value.maxFileSize<e.size&&f(e,F("INVALID_SIZE",d.value.maxFileSize));const a=/(\.[^.]+)?$/.exec(e.name)?.[1]?.toLowerCase();d.value.allowedExtensions.length&&a&&!d.value.allowedExtensions.map(t=>t.toLowerCase()).includes(a)&&f(e,F("INVALID_EXTENSION"))})}function H(e,a){e&&e.forEach(t=>{const s=a.find(n=>n.name===t.name);s&&(t.succeeded?me(s,t.id,t.url):f(s,F(t.errorCode,t.errorParameter,t.errorMessage)))})}async function x(){const e=pe-E.value;if(e<=0)return await Promise.all(v.value),x();const a=[...y.value],t=Math.max(1,Math.ceil(a.length/e));for(let n=0;n<e&&a.length>0;n++){const o=a.splice(0,t),i=J(o);v.value.push(i),i.finally(()=>{const p=v.value.indexOf(i);p>-1&&v.value.splice(p,1)})}if(await Promise.all(v.value),l.value.filter(n=>N(n)||w(n)).length>0)return x()}async function J(e){if(e.length===0)return;const a=ae();try{E.value++,U.value.set(a,e),e.forEach(i=>{de(i)});const t=new FormData,s=S.value.filter(i=>e.some(p=>p.name===i.name));s.forEach(i=>t.append("file",i.file));const{data:n,execute:o}=X(a);await o(`/api/files/${h(c)}`,{data:t}),H(n.value,s)}catch{e.forEach(t=>{const s=S.value.find(n=>n.name===t.name);s&&f(s,I("file_error.UPLOAD_FAILED"))})}finally{E.value--,U.value.delete(a)}}function K(e,a){if(!e.total)return;const t=U.value.get(a);if(!t||t.length===0)return;const s=S.value.filter(o=>t.some(i=>i.name===o.name));if(s.length===0)return;const n=s.reduce((o,i)=>o+i.size,0);s.forEach(o=>{const i=o.size/n;o.progress=Math.min(100,Math.round(e.loaded/(e.total||1)*100*i))})}async function Q(e){await re(e,async a=>{A(a)&&!await he(a.id)?f(a,I("file_error.CANNOT_DELETE")):ue(a)}),l.value=l.value.filter(a=>!ce(a))}function F(e,a,t){let s;if(e==="INVALID_SIZE"){const n=fe(a);s=[b(n.value,{notation:"compact",style:"unit",unit:n.unit,unitDisplay:"narrow"})]}else s=[a];return C({errorCode:e,errorMessage:t,errorParameters:s.map((n,o)=>({key:o.toString(),value:n}))})}return V(()=>{z.delete(h(c))}),{files:l,failedFiles:D,hasFailedFiles:T,attachedFiles:_,uploadedFiles:L,modifiedFiles:M,anyFilesModified:Z,attachedAndUploadedFiles:$,allFilesAttachedOrUploaded:B,options:d,addFiles:k,validateFiles:G,uploadFiles:x,removeFiles:Q,fetchOptions:q}}export{Ue as u};
//# sourceMappingURL=useFiles-tvXIeDC6.js.map
