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