import{resetGrafanaOrganisation as n,deleteGrafanaOrganisation as a,createGrafanaOrganisation as i,getGrafanaOrganisation as o}from"./lib/api-helpers.js";import{defineSmartComponent as t}from"./lib/define-smart-component.js";import{notifySuccess as e,notifyError as r}from"./lib/notifications.js";import{sendToApi as s}from"./lib/send-to-api.js";import{i18n as c}from"./translation.js";import"./cc-smart-container.js";import"./cc-grafana-info.js";t({selector:"cc-grafana-info",params:{apiConfig:{type:Object},ownerId:{type:String},grafanaBaseLink:{type:String}},onContextUpdate({context:t,updateComponent:f,onEvent:g,signal:l}){const{apiConfig:p,ownerId:d,grafanaBaseLink:u}=t;function m(){(function({apiConfig:n,signal:a,ownerId:i,grafanaBaseLink:t}){return o({id:i}).then(s({apiConfig:n,signal:a})).then((n=>{const a=new URL("/d/home/clever-cloud-metrics-home",t);a.searchParams.set("orgId",n.id);return{status:"enabled",link:a.toString()}})).catch((n=>{if(404===n.response?.status&&n.toString().startsWith("Error: Grafana organization not found"))return{status:"disabled"};throw n}))})({apiConfig:p,signal:l,ownerId:d,grafanaBaseLink:u}).then((n=>{f("state",{type:"loaded",info:n})})).catch((n=>{console.error(n),f("state",{type:"error"})}))}f("state",{type:"loading"}),g("cc-grafana-info:reset",(()=>{f("state",(n=>{n.info.action="resetting"})),function({apiConfig:a,ownerId:i}){return n({id:i}).then(s({apiConfig:a}))}({apiConfig:p,ownerId:d}).then((()=>e(c("cc-grafana-info.reset.success")))).catch((n=>{console.error(n),r(c("cc-grafana-info.reset.error"))})).finally((()=>{f("state",(n=>{n.info.action=null}))}))})),g("cc-grafana-info:disable",(()=>{f("state",(n=>{n.info.action="disabling"})),function({apiConfig:n,ownerId:i}){return a({id:i}).then(s({apiConfig:n}))}({apiConfig:p,ownerId:d}).then((()=>{f("state",(n=>{n.info={status:"disabled"}})),e(c("cc-grafana-info.disable.success"))})).catch((n=>{console.error(n),r(c("cc-grafana-info.disable.error"))})).finally((()=>{f("state",(n=>{n.info.action=null}))}))})),g("cc-grafana-info:enable",(()=>{f("state",(n=>{n.info.action="enabling"})),function({apiConfig:n,ownerId:a}){return i({id:a}).then(s({apiConfig:n}))}({apiConfig:p,ownerId:d}).then((()=>{m(),e(c("cc-grafana-info.enable.success"))})).catch((n=>{console.error(n),r(c("cc-grafana-info.enable.error"))})).finally((()=>{f("state",(n=>{n.info.action=null}))}))})),m()}});
//# sourceMappingURL=cc-grafana-info.smart.js.map
