import{getAllEnvVars as e,updateAllEnvVars as t,getAllDeployments as r,redeploy as a}from"@clevercloud/client/esm/api/v2/application.js";import{toNameValueObject as n}from"@clevercloud/client/esm/utils/env-vars.js";import{notifySuccess as o,notifyError as p}from"./lib/notifications.js";import{sendToApi as i}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{CcEnvVarsWasUpdatedEvent as d}from"./components/cc-env-var-form/cc-env-var-form.events.js";import"./cc-env-var-form.js";s({selector:'cc-env-var-form[context="env-var-app"]',params:{apiConfig:{type:Object},ownerId:{type:String},appId:{type:String},logsUrlPattern:{type:String}},onContextUpdate({context:e,onEvent:t,updateComponent:r,component:a,signal:i}){const{apiConfig:s,ownerId:m,appId:v,logsUrlPattern:f}=e,h=new l({apiConfig:s,ownerId:m,appId:v,signal:i});r("state",{type:"loading"}),r("resourceId",v),r("restartApp",!1),h.fetchEnvVars().then((e=>{r("state",{type:"loaded",validationMode:"simple",variables:e})})).catch((e=>{console.error(e),r("state",{type:"error"})})),t("cc-env-var-form-submit",(e=>{r("state",(e=>{e.type="saving"}));const t=n(e);h.updateVariables(t).then((()=>{r("state",(t=>{t.variables=e})),o(c("cc-env-var-form.update.success")),h.hasDeployments().then((e=>{e&&r("restartApp",!0)})),a.dispatchEvent(new d(e))})).catch((()=>p(c("cc-env-var-form.update.error")))).finally((()=>{r("state",(e=>{e.type="loaded"}))}))})),t("cc-application-restart",(()=>{h.redeployApp().then((()=>{r("restartApp",!1),o(c("cc-env-var-form.redeploy.success.text",{logsUrl:f.replace(":id",v)}),c("cc-env-var-form.redeploy.success.heading"))})).catch((e=>{console.error(e),4014===e.id?p(c("cc-env-var-form.redeploy.error.app-stopped")):p(c("cc-env-var-form.redeploy.error"))}))}))}});class l{constructor({apiConfig:e,ownerId:t,appId:r,signal:a}){this._apiConfig=e,this._ownerId=t,this._appId=r,this._signal=a}fetchEnvVars(){return e({id:this._ownerId,appId:this._appId}).then(i({apiConfig:this._apiConfig,signal:this._signal}))}updateVariables(e){return t({id:this._ownerId,appId:this._appId},e).then(i({apiConfig:this._apiConfig}))}async hasDeployments(){return(await r({id:this._ownerId,appId:this._appId}).then(i({apiConfig:this._apiConfig}))).length>0}redeployApp(){return a({id:this._ownerId,appId:this._appId}).then(i({apiConfig:this._apiConfig}))}}
//# sourceMappingURL=cc-env-var-form.smart-env-var-app.js.map
