import{V as j,r as z,aj as k,q as n,T as F,i as R,cg as Y,u as I}from"./vendor-BGtMsOoo.js";import{d as B,a as G,u as H,D as J,b as K}from"./constants-55usBUX1.js";import{v as _,an as Q,ao as W,ap as ee,aq as ae,ar as m,am as h,ad as se,as as d,at as te,au as ie,av as le,aw as oe,ax as ne}from"./index-CRze-aDK.js";async function re(u){const{data:o}=await _.mutate({mutation:B,variables:{command:{id:u}}});return o==null?void 0:o.deleteFile}async function ue(u){const{data:o}=await _.query({query:G,variables:{scope:u}});return o.fileUploadOptions}function Fe(u,o){const{translate:A}=Q("file_error"),{t:D,n:U}=j(),{data:w,execute:L}=H({method:"POST",onUploadProgress:Z}),E={maxFileCount:K,maxFileSize:J,allowedExtensions:[]},r=z(E);async function N(){const e=await ue(I(u));e&&(r.value={...E,...e})}const i=z([]);o&&k(o,i);const p=n(()=>i.value.filter(W)),g=F(()=>p.value.length>0),c=n(()=>i.value.filter(ee)),O=n(()=>c.value.reduce((e,s)=>e+s.size,0)),S=n(()=>i.value.filter(ae)),q=F(()=>S.value.length>0),v=n(()=>i.value.filter(m)),x=n(()=>i.value.filter(h)),y=n(()=>i.value.filter(e=>!m(e))),C=F(()=>y.value.length>0||R(o)&&v.value.length!==Y(o).length),M=n(()=>[...v.value,...x.value]),T=F(()=>i.value.every(e=>m(e)||h(e)));function X(e){return i.value.length+e.length>r.value.maxFileCount?!1:(i.value.push(...e),!0)}function b(){g.value&&p.value.forEach(e=>{var a,t;i.value.some(l=>l.name===e.name&&l!==e)&&d(e,f("ALREADY_EXISTS")),r.value.maxFileSize<e.size&&d(e,f("INVALID_SIZE",r.value.maxFileSize));const s=(t=(a=/(\.[^.]+)?$/.exec(e.name))==null?void 0:a[1])==null?void 0:t.toLowerCase();r.value.allowedExtensions.length&&s&&!r.value.allowedExtensions.map(l=>l.toLowerCase()).includes(s)&&d(e,f("INVALID_EXTENSION"))})}async function P(){var s;if(!g.value)return;p.value.forEach(le);const e=new FormData;c.value.forEach(a=>e.append("file",a.file)),await L(`/api/files/${I(u)}`,{data:e}),(s=w.value)==null||s.forEach(a=>{const t=c.value.find(l=>l.name===a.name);t&&(a.succeeded?oe(t,a.id,a.url):d(t,f(a.errorCode,a.errorParameter,a.errorMessage)))})}function Z(e){let a=e.total-O.value;c.value.forEach(t=>{a+t.size<=e.loaded?t.progress=100:a>=e.loaded-t.size&&(t.progress=Math.round(Math.max(e.loaded-a,0)/t.size*100)),a+=t.size})}async function $(e){await se(e,async s=>{h(s)&&!await re(s.id)?d(s,D("file_error.CANNOT_DELETE")):te(s)}),i.value=i.value.filter(s=>!ie(s))}function f(e,s,a){let t;if(e==="INVALID_SIZE"){const l=ne(s);t=[U(l.value,{notation:"compact",style:"unit",unit:l.unit,unitDisplay:"narrow"})]}else t=[s];return A({errorCode:e,errorMessage:a,errorParameters:t.map((l,V)=>({key:V.toString(),value:l}))})}return{files:i,failedFiles:S,hasFailedFiles:q,attachedFiles:v,uploadedFiles:x,modifiedFiles:y,anyFilesModified:C,attachedAndUploadedFiles:M,allFilesAttachedOrUploaded:T,options:r,addFiles:X,validateFiles:b,uploadFiles:P,removeFiles:$,fetchOptions:N}}export{Fe as u};
//# sourceMappingURL=useFiles-DW9DMZJb.js.map
