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