import{ag as Y,r as m,N as j,m as d,aa as g,h as V,e as h,bx as ee,at as ae,v as te}from"./vendor-DYYUXsKb.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-BjgfEvRF.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 xe(u,r){const{translate:b}=le("file_error"),{t:D,n:X}=Y(),U=m(0),v=m([]),q=e=>me({method:"POST",onUploadProgress:a=>Q(a,e)}),x=m(new Map),L={maxFileCount:re,maxFileSize:oe,allowedExtensions:[]},c=m(L);async function R(){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 y=d(()=>l.value.filter(w)),T=g(()=>y.value.length>0),S=d(()=>l.value.filter(z)),O=d(()=>l.value.filter(ie)),Z=g(()=>O.value.length>0),_=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)&&_.value.length!==h(r).length),B=d(()=>[..._.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&&y.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=[...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=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++,x.value.set(a,e),e.forEach(i=>{fe(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}=q(a);await o(`/api/files/${h(u)}`,{data:t}),J(n.value,s)}catch{e.forEach(t=>{const s=S.value.find(n=>n.name===t.name);s&&f(s,D("file_error.UPLOAD_FAILED"))})}finally{U.value--,x.value.delete(a)}}function Q(e,a){if(!e.total)return;const t=x.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 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=[X(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:_,uploadedFiles:M,modifiedFiles:N,anyFilesModified:$,attachedAndUploadedFiles:B,allFilesAttachedOrUploaded:k,options:c,addFiles:G,validateFiles:H,uploadFiles:A,removeFiles:W,fetchOptions:R}}export{xe as u};
//# sourceMappingURL=useFiles-C2noQlO_.js.map
