import{d as F,j as d,a as f,t as l}from"./preload-helper-DadE4qT2.chunk.mjs";import{l as r,p as g,d as u,s as p,S as m}from"./index-Dgdobvu3.chunk.mjs";import{g as S}from"./PhotoSearch-3PnBhHOR.chunk.mjs";import{A as w}from"./AbortControllerMixin-Bb5X6Yae.chunk.mjs";const R=F({name:"FetchFilesMixin",mixins:[w],data(){return{errorFetchingFiles:null,loadingFiles:!1,doneFetchingFiles:!1,fetchSemaphore:new m(1),fetchedFileIds:[]}},watch:{"$route.path":function(){this.resetFetchFilesState()}},methods:{async fetchFiles(h={},o,n=!1){if(this.doneFetchingFiles&&!n||this.loadingFiles)return[];const a=this.abortController.signal,c=await this.fetchSemaphore.acquire();try{this.errorFetchingFiles=null,this.loadingFiles=!0;const e=200;let i=await S({firstResult:this.fetchedFileIds.length,nbResults:e,...h,signal:a});i.length!==e&&(this.doneFetchingFiles=!0),o!==void 0&&(i=i.filter(o));const t=i.map(s=>s.fileid).filter(s=>!this.fetchedFileIds.includes(s));return this.fetchedFileIds.push(...t),this.$store.dispatch("appendFiles",i),r.debug(`[FetchFilesMixin] Fetched ${t.length} new files: `,{fileIds:t}),t}catch(e){if(e.response?.status===404){const i=g.state.userConfig.photosSourceFolders;for(const t of i)if(e.response?.data?.match(`File with name /${t} could not be located`)!==null){r.debug(`The ${t} folder does not exist, creating it.`);try{return await u.createDirectory(d(f,t)),this.resetFetchFilesState(),[]}catch(s){this.errorFetchingFiles=404,r.error("Fail to create source directory",{error:s})}}}else{if(e instanceof DOMException&&e.code===e.ABORT_ERR)return[];this.errorFetchingFiles=e}p(l("photos","Error fetching files")),r.error(l("photos","Error fetching files"),{error:e})}finally{this.loadingFiles=!1,this.fetchSemaphore.release(c)}return[]},resetFetchFilesState(){this.abortPendingRequest(),this.doneFetchingFiles=!1,this.errorFetchingFiles=null,this.loadingFiles=!1,this.fetchedFileIds=[]}}});export{R as F};
//# sourceMappingURL=FetchFilesMixin-BGmLOce1.chunk.mjs.map
