import{$ as K,r as m,ax as Q,E as d,_ as g,h as V,e as h,bw as ee,ae,v as te}from"./vendor-CsGITfgk.js";import{r as P,ar as se,as as le,at as ne,au as M,av as N,aw as ie,ax as E,aq as I,ay as oe,az as re,aA as ue,aB as ce,aC as f,aD as de,aE as fe,aF as ve,aG as me,aH as he}from"./index-CsCqMgta.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([]),q=e=>ve({method:"POST",onUploadProgress:a=>j(a,e)}),y=m(new Map),w={maxFileCount:re,maxFileSize:oe,allowedExtensions:[]},c=m(w);async function T(){const e=await C(h(u));e&&(c.value={...w,...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)),$=g(()=>U.value.length>0),_=d(()=>n.value.filter(N)),L=d(()=>n.value.filter(ie)),B=g(()=>L.value.length>0),A=d(()=>n.value.filter(E)),O=d(()=>n.value.filter(I)),z=d(()=>n.value.filter(e=>!E(e))),R=g(()=>z.value.length>0||V(r)&&A.value.length!==h(r).length),Z=d(()=>[...A.value,...O.value]),k=g(()=>n.value.every(e=>E(e)||I(e)));function G(e){return n.value.length+e.length>c.value.maxFileCount?!1:(n.value.push(...e),!0)}function H(){$.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 W(e,a){e&&e.forEach(t=>{const s=a.find(l=>l.name===t.name);s&&(t.succeeded?me(s,t.id,t.url):f(s,F(t.errorCode,t.errorParameter,t.errorMessage)))})}async function S(){const e=ge-x.value;if(e<=0)return await Promise.all(v.value),S();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=Y(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 S()}async function Y(e){if(e.length===0)return;const a=te();try{x.value++,y.value.set(a,e),e.forEach(i=>{de(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:l,execute:o}=q(a);await o(`/api/files/${h(u)}`,{data:t}),W(l.value,s)}catch{e.forEach(t=>{const s=_.value.find(l=>l.name===t.name);s&&f(s,D("file_error.UPLOAD_FAILED"))})}finally{x.value--,y.value.delete(a)}}function j(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 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=>{I(a)&&!await Fe(a.id)?f(a,D("file_error.CANNOT_DELETE")):he(a)}),n.value=n.value.filter(a=>!ce(a))}function F(e,a,t){let s;if(e==="INVALID_SIZE"){const l=fe(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:L,hasFailedFiles:B,attachedFiles:A,uploadedFiles:O,modifiedFiles:z,anyFilesModified:R,attachedAndUploadedFiles:Z,allFilesAttachedOrUploaded:k,options:c,addFiles:G,validateFiles:H,uploadFiles:S,removeFiles:J,fetchOptions:T}}export{ye as u};
//# sourceMappingURL=useFiles-EFPcvAdh.js.map
