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