import{get as e}from"@clevercloud/client/esm/api/v2/addon.js";import{getGrafanaOrganisation as a}from"@clevercloud/client/esm/api/v4/saas.js";import{ONE_SECOND as i}from"@clevercloud/client/esm/with-cache.js";import{getDevHubUrl as r}from"./lib/dev-hub-url.js";import{sendToApi as t}from"./lib/send-to-api.js";class n{constructor({apiConfig:e,ownerId:a,addonId:i,providerId:r,grafanaLink:t,signal:n}){this._apiConfig=e,this._ownerId=a,this._addonId=i,this.providerId=r,this._grafanaLink=t,this._realId=null,this._signal=n}_getAddon(){return e({id:this._ownerId,addonId:this._addonId}).then(t({apiConfig:this._apiConfig,signal:this._signal,cacheDelay:i}))}_getOperator(e,a){return(r={providerId:e,realId:a},Promise.resolve({method:"get",url:`/v4/addon-providers/addon-${r.providerId}/addons/${r.realId}`,headers:{Accept:"application/json"}})).then(t({apiConfig:this._apiConfig,signal:this._signal,cacheDelay:i}));var r}getOperatorVersionInfo(){return(e={providerId:this.providerId,realId:this._realId},Promise.resolve({method:"get",url:`/v4/addon-providers/addon-${e.providerId}/addons/${e.realId}/version/check`,headers:{Accept:"application/json"}})).then(t({apiConfig:this._apiConfig,signal:this._signal}));var e}_getGrafanaAppLink({appId:e,signal:i}){return a({id:this._ownerId}).then(t({apiConfig:this._apiConfig,signal:i})).then((a=>{const i=new URL("/d/runtime/application-runtime",this._grafanaLink.base);return i.searchParams.set("orgId",a.id),i.searchParams.set("var-SELECT_APP",e),i.toString()})).catch((e=>404===e.response?.status?this._grafanaLink.console:e))}updateOperatorVersion(e){return o({providerId:this.providerId,realId:this._realId},{targetVersion:e}).then(t({apiConfig:this._apiConfig}))}async getAddonInfo(){const e=await this._getAddon();this._realId=e.realId,this.providerId=e.provider.id;const[a,i]=await Promise.all([this._getOperator(this.providerId,this._realId),this.getOperatorVersionInfo()]);return{addonInfo:e,operator:a,operatorVersionInfo:i,grafanaAppLink:null!=this._grafanaLink?await this._getGrafanaAppLink({appId:a.resources.entrypoint,signal:this._signal}):null}}}function o(e,a){return Promise.resolve({method:"post",url:`/v4/addon-providers/addon-${e.providerId}/addons/${e.realId}/version/update`,headers:{Accept:"application/json","Content-Type":"application/json"},body:a})}function s(e){return e.needUpdate?{stateType:"update-available",installed:e.installed,available:e.available.filter((a=>a!==e.installed)),latest:e.latest,changelogLink:`${r("/changelog")}`}:{stateType:"up-to-date",installed:e.installed,latest:e.latest}}export{n as CcAddonInfoClient,s as formatVersionState,o as updateOperatorVersion};
//# sourceMappingURL=cc-addon-info.client.js.map
