import{resetGrafanaOrganisation as n,deleteGrafanaOrganisation as a,createGrafanaOrganisation as t,getGrafanaOrganisation as o}from"@clevercloud/client/esm/api/v4/saas.js";import{notifySuccess as e,notifyError as i}from"./lib/notifications.js";import{sendToApi as r}from"./lib/send-to-api.js";import{defineSmartComponent as s}from"./lib/smart/define-smart-component.js";import{i18n as c}from"./translation.js";import"./cc-smart-container.js";import"./cc-grafana-info.js";s({selector:"cc-grafana-info",params:{apiConfig:{type:Object},ownerId:{type:String},grafanaBaseLink:{type:String}},onContextUpdate({context:s,updateComponent:f,onEvent:g,signal:l}){const{apiConfig:d,ownerId:p,grafanaBaseLink:u}=s;function m(){(function({apiConfig:n,signal:a,ownerId:t,grafanaBaseLink:e}){return o({id:t}).then(r({apiConfig:n,signal:a})).then((n=>{const a=new URL("/d/home/clever-cloud-metrics-home",e);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:d,signal:l,ownerId:p,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-reset",(()=>{f("state",(n=>{n.info.action="resetting"})),function({apiConfig:a,ownerId:t}){return n({id:t}).then(r({apiConfig:a}))}({apiConfig:d,ownerId:p}).then((()=>e(c("cc-grafana-info.reset.success")))).catch((n=>{console.error(n),i(c("cc-grafana-info.reset.error"))})).finally((()=>{f("state",(n=>{n.info.action=null}))}))})),g("cc-grafana-toggle",(({isEnabled:n})=>{f("state",(a=>{a.info.action=n?"enabling":"disabling"}));const o=n?function({apiConfig:n,ownerId:a}){return t({id:a}).then(r({apiConfig:n}))}({apiConfig:d,ownerId:p}).then((()=>{m(),e(c("cc-grafana-info.enable.success"))})).catch((n=>{console.error(n),i(c("cc-grafana-info.enable.error"))})):function({apiConfig:n,ownerId:t}){return a({id:t}).then(r({apiConfig:n}))}({apiConfig:d,ownerId:p}).then((()=>{f("state",(n=>{n.info={status:"disabled"}})),e(c("cc-grafana-info.disable.success"))})).catch((n=>{console.error(n),i(c("cc-grafana-info.disable.error"))}));o.finally((()=>{f("state",(n=>{n.info.action=null}))}))})),m()}});
//# sourceMappingURL=cc-grafana-info.smart.js.map
