import{sendToApi as e}from"./lib/send-to-api.js";import{defineSmartComponent as d}from"./lib/smart/define-smart-component.js";import"./cc-smart-container.js";import"./cc-addon-backups.js";import{get as a}from"@clevercloud/client/esm/api/v2/addon.js";import{getBackups as n}from"@clevercloud/client/esm/api/v2/backups.js";import{getAddon as o}from"@clevercloud/client/esm/api/v4/addon-providers.js";d({selector:"cc-addon-backups",params:{apiConfig:{type:Object},ownerId:{type:String},addonId:{type:String}},async onContextUpdate({context:e,updateComponent:d,signal:a}){const{apiConfig:n,ownerId:o,addonId:s}=e;d("state",{type:"loading"}),d("addonId",s);const i=new t(n,a),c=await i.fetchAddon({ownerId:o,addonId:s}),p=await i.fetchAddonDetails({addon:c,addonId:s});i.fetchBackups({addon:c,ownerId:o,addonDetails:p}).then((e=>{const a=e.sort(((e,d)=>new Date(d.createdAt).getTime()-new Date(e.createdAt).getTime()));d("state",{type:"loaded",providerId:r({addon:c,addonDetails:p}),passwordForCommand:p.password,backups:a})}))}});class t{constructor(e,d){this.apiConfig=e,this.signal=d}fetchAddon({ownerId:d,addonId:n}){return a({id:d,addonId:n}).then(e({apiConfig:this.apiConfig,signal:this.signal}))}fetchAddonDetails({addon:d,addonId:a}){return o({providerId:d.provider.id,addonId:a}).then(e({apiConfig:this.apiConfig,signal:this.signal}))}fetchBackups({addon:e,ownerId:d,addonDetails:a}){return"es-addon"===r({addon:e,addonDetails:a})?this.getElasticsearchBackups({addon:e,ownerId:d}):this.getDefaultBackups({addon:e,addonDetails:a,ownerId:d})}getElasticsearchBackups({addon:d,ownerId:a}){return n({ownerId:a,ref:d.realId}).then(e({apiConfig:this.apiConfig,signal:this.signal})).then((e=>e.map((e=>({createdAt:e.creation_date,url:e.link,expiresAt:e.delete_at,restoreCommand:e.restore_command,deleteCommand:e.delete_command})))))}getDefaultBackups({addon:d,addonDetails:a,ownerId:o}){return n({ownerId:o,ref:d.realId}).then(e({apiConfig:this.apiConfig,signal:this.signal})).then((e=>e.map((e=>({createdAt:e.creation_date,url:e.download_url,expiresAt:e.delete_at,restoreCommand:s({addon:d,addonDetails:a})})))))}}function s({addon:e,addonDetails:d}){switch(r({addon:e,addonDetails:d})){case"postgresql-addon":return`pg_restore -h ${d.host} -p ${d.port} -U ${d.user} -d ${d.database} --no-owner --no-privileges --no-comments --format=c YOUR_BACKUP_FILE`;case"mysql-addon":return`mysql -h ${d.host} -P ${d.port} -u ${d.user} -p ${d.database} < YOUR_BACKUP_FILE`;case"mongodb-addon":return`mongorestore --host=${d.host} --port=${d.port} --username=${d.user} --nsFrom="${d.database}.*" --nsTo="${d.database}.*" --authenticationDatabase="${d.database}" --archive={YOUR_BACKUP_FILE} --gzip`;default:return null}}function r({addon:e,addonDetails:d}){if("es-addon"===e.provider.id){const e=d.services.find((e=>"kibana"===e.name));return null!=e&&e.enabled?"es-addon":"es-addon-old"}return e.provider.id}
//# sourceMappingURL=cc-addon-backups.smart.js.map
