import{ai as W,r as m,N as Y,m as r,h as j,e as h,aS as V,D as ee,v as ae}from"./vendor-BmRNukEL.js";import{s as P,aG as te,aH as se,aq as ie,aI as N,aJ as w,aK as le,aL as g,aF as A,aM as ne,aN as oe,aO as re,aP as f,aQ as ue,aR as ce,aS as de,aT as fe,aU as ve,aV as me}from"./index-CfcNWeCf.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}=ie("file_error"),{t:I,n:b}=W(),E=m(0),v=m([]),T=e=>ve({method:"POST",onUploadProgress:a=>K(a,e)}),U=m(new Map),D={maxFileCount:oe,maxFileSize:ne,allowedExtensions:[]},d=m(D);async function X(){const e=await z(h(c));e&&(d.value={...D,...e})}const l=m([]);u&&Y(u,e=>{e&&l.value.length===0&&(l.value=[...h(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 S=r(()=>l.value.filter(N)),q=r(()=>S.value.length>0),y=r(()=>l.value.filter(w)),L=r(()=>l.value.filter(le)),R=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||j(u)&&_.value.length!==h(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(){q.value&&S.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?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=[...S.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=ae();try{E.value++,U.value.set(a,e),e.forEach(n=>{de(n)});const t=new FormData,s=y.value.filter(n=>e.some(p=>p.name===n.name));s.forEach(n=>t.append("file",n.file));const{data:i,execute:o}=T(a);await o(`/api/files/${h(c)}`,{data:t}),H(i.value,s)}catch{e.forEach(t=>{const s=y.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=y.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 re(e,async a=>{A(a)&&!await he(a.id)?f(a,I("file_error.CANNOT_DELETE")):ue(a)}),l.value=l.value.filter(a=>!ce(a))}function F(e,a,t){let s;if(e==="INVALID_SIZE"){const i=fe(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 V(()=>{z.delete(h(c))}),{files:l,failedFiles:L,hasFailedFiles:R,attachedFiles:_,uploadedFiles:O,modifiedFiles:M,anyFilesModified:Z,attachedAndUploadedFiles:$,allFilesAttachedOrUploaded:B,options:d,addFiles:k,validateFiles:G,uploadFiles:x,removeFiles:Q,fetchOptions:X}}export{Ue as u};
//# sourceMappingURL=useFiles-DxnQNlfw.js.map
