import{i as e}from"./useAnalytics-D7ict_xi.js";import{t}from"./axios-DSyjHw-i.js";import{t as n}from"./sessionSettingStore-ZHUB-lwo.js";import{t as r}from"./backendApi-BRM6WgjB.js";var i=`database/cloud`,a=class{static async getStatus(){return r.get(`${i}/status`)}static async getVersions(){return r.get(`${i}/versions`)}static async backup(e){return r.post(`${i}/backup`,e?{name:e}:{})}static async restore(e){return r.post(`${i}/restore/${encodeURIComponent(e)}`)}static async deleteVersion(e){return r.delete(`${i}/versions/${encodeURIComponent(e)}`)}static async savePreferences(e){return r.post(`${i}/preferences`,e)}},o=e(`cloudDatabase`,{state:()=>({status:null,versions:[],totalStorageUsed:0,maxStorageAllowed:0,maxVersions:0,isLoading:!1,isBackingUp:!1,restoringId:null,deletingId:null,isSavingPreferences:!1,lastError:null}),getters:{hasAccess(){return n().hasFeature(`BasicTier`)},hasVersions(){return this.versions.length>0},storageRatio(){let e=this.status?.maxBytes??this.maxStorageAllowed;if(!e)return 0;let t=this.status?.cloudBytesUsed??this.totalStorageUsed;return Math.min(t/e,1)},versionRatio(){let e=this.status?.maxVersions??this.maxVersions;if(!e)return 0;let t=this.status?.cloudVersionCount??this.versions.length;return Math.min(t/e,1)}},actions:{async refresh(){if(this.hasAccess){this.isLoading=!0,this.lastError=null;try{let[e,t]=await Promise.all([a.getStatus(),a.getVersions()]);this.status=e,this.versions=[...t.versions].sort((e,t)=>s(t.createdAt)-s(e.createdAt)),this.totalStorageUsed=t.totalStorageUsed,this.maxStorageAllowed=t.maxStorageAllowed,this.maxVersions=t.maxVersions}catch(e){this.lastError=c(e,`Failed to load cloud backups`),console.error(`Failed to load cloud database status:`,e)}finally{this.isLoading=!1}}},async ensureLoaded(){!this.hasAccess||this.isLoading||this.status||await this.refresh()},applyStatusPush(e){this.isBackingUp=e.isBackingUp,this.status?(this.status.hasLocalChanges=e.hasLocalChanges,this.status.lastBackupAt=e.lastBackupAt):this.status={hasFeature:this.hasAccess,syncOnLaunch:!1,autoBackup:!1,lastBackupAt:e.lastBackupAt,hasLocalChanges:e.hasLocalChanges,cloudVersionCount:0,cloudBytesUsed:0,maxBytes:0,maxVersions:0,cloudReachable:!0}},async refreshStatus(){if(this.hasAccess)try{this.status=await a.getStatus(),this.lastError=null}catch(e){console.error(`Failed to refresh cloud database status:`,e)}},async backupNow(e){this.isBackingUp=!0,this.lastError=null;try{let t=await a.backup(e);return t.success?await this.refresh():this.lastError=t.error,t}catch(e){return this.lastError=c(e,`Backup failed`),console.error(`Cloud backup failed:`,e),null}finally{this.isBackingUp=!1}},async restoreVersion(e){this.restoringId=e,this.lastError=null;try{let t=await a.restore(e);return t.success||(this.lastError=t.error),t}catch(e){return this.lastError=c(e,`Restore failed`),console.error(`Cloud restore failed:`,e),null}finally{this.restoringId=null}},async deleteVersion(e){this.deletingId=e,this.lastError=null;try{let t=await a.deleteVersion(e);return t.success?(this.versions=this.versions.filter(t=>t.id!==e),await this.refreshStatus()):this.lastError=t.error,t}catch(e){return this.lastError=c(e,`Delete failed`),console.error(`Cloud version delete failed:`,e),null}finally{this.deletingId=null}},async savePreferences(e){this.isSavingPreferences=!0,this.lastError=null;let t=this.status?{syncOnLaunch:this.status.syncOnLaunch,autoBackup:this.status.autoBackup}:null;this.status&&(this.status.syncOnLaunch=e.syncOnLaunch,this.status.autoBackup=e.autoBackup);try{let n=await a.savePreferences(e);return n.success||(this.lastError=n.error,this.status&&t&&(this.status.syncOnLaunch=t.syncOnLaunch,this.status.autoBackup=t.autoBackup)),n}catch(e){return this.lastError=c(e,`Could not save preferences`),console.error(`Saving cloud database preferences failed:`,e),this.status&&t&&(this.status.syncOnLaunch=t.syncOnLaunch,this.status.autoBackup=t.autoBackup),null}finally{this.isSavingPreferences=!1}}}});function s(e){if(!e)return-1/0;let t=new Date(e).getTime();return Number.isNaN(t)?-1/0:t}function c(e,n){if(t.isAxiosError(e)){let t=e.response?.data;if(typeof t==`string`&&t.length>0)return t;if(t&&typeof t==`object`&&`error`in t){let e=t.error;if(typeof e==`string`&&e.length>0)return e}}return e instanceof Error&&e.message.length>0?e.message:n}export{o as t};