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