import{l as r,p as F,d,s as f,S as g}from"./index-VDTWCae_.chunk.mjs";import{d as u,j as p,h as m,t as h}from"./dav-D-0Zsvk_.chunk.mjs";import{g as S}from"./PhotoSearch-B-dfikGn.chunk.mjs";import{A as w}from"./AbortControllerMixin-C4cpCLeS.chunk.mjs";const R=u({name:"FetchFilesMixin",mixins:[w],data(){return{errorFetchingFiles:null,loadingFiles:!1,doneFetchingFiles:!1,fetchSemaphore:new g(1),fetchedFileIds:[]}},watch:{"$route.path":function(){this.resetFetchFilesState()}},methods:{async fetchFiles(l={},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,...l,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=F.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 d.createDirectory(p(m,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}f(h("photos","Error fetching files")),r.error(h("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-GV1YkZ7s.chunk.mjs.map
