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