import{get as e,update as t,remove as o}from"@clevercloud/client/esm/api/v2/oauth-consumer.js";import{camelCase as n,snakeCase as s}from"./lib/change-case.js";import{notifySuccess as r,notifyError as a}from"./lib/notifications.js";import{sendToApi as i}from"./lib/send-to-api.js";import{defineSmartComponent as c}from"./lib/smart/define-smart-component.js";import{i18n as m}from"./translation.js";import"./cc-smart-container.js";import{CcOauthConsumerWasUpdatedEvent as u,CcOauthConsumerWasDeletedEvent as p}from"./components/cc-oauth-consumer-form/cc-oauth-consumer-form.events.js";import"./cc-oauth-consumer-form.js";const h={almighty:!1,accessOrganisations:!1,accessOrganisationsBills:!1,accessOrganisationsConsumptionStatistics:!1,accessOrganisationsCreditCount:!1,accessPersonalInformation:!1,manageOrganisations:!1,manageOrganisationsApplications:!1,manageOrganisationsMembers:!1,manageOrganisationsServices:!1,managePersonalInformation:!1,manageSshKeys:!1};c({selector:"cc-oauth-consumer-form[smart-mode=update]",params:{apiConfig:{type:Object},ownerId:{type:String},key:{type:String}},onContextUpdate({context:e,updateComponent:t,onEvent:o,component:s}){const{apiConfig:i,ownerId:c,key:l}=e,g=new d(i,c,l);t("state",{type:"loading"}),g.getOauthConsumer().then((e=>{const o=Object.fromEntries(Object.entries(e.rights).map((([e,t])=>[n(e),t]))),s={...h,...o},r={name:e.name,url:e.url,baseUrl:e.baseUrl,description:e.description,picture:e.picture,rights:s};t("state",{type:"idle-update",values:r})})).catch((e=>{console.error(e),t("state",{type:"error"})})),o("cc-oauth-consumer-change",(e=>{const o=e.name;t("state",(e=>{e.type="updating"})),g.updateOauthConsumer(e).then((()=>{r(m("cc-oauth-consumer-form.update.success",{oauthConsumerName:o})),s.dispatchEvent(new u)})).catch((e=>{console.error(e),a(m("cc-oauth-consumer-form.update.error",{oauthConsumerName:o}))})).finally((()=>{t("state",(e=>{e.type="idle-update"}))}))})),o("cc-oauth-consumer-delete",(()=>{t("state",(e=>{e.type="deleting"})),g.deleteOauthConsumer().then((()=>{r(m("cc-oauth-consumer-form.delete.success")),s.dispatchEvent(new p)})).catch((e=>{console.error(e),a(m("cc-oauth-consumer-form.delete.error")),t("state",(e=>{e.type="idle-update"}))}))}))}});class d{constructor(e,t,o){this._apiConfig=e,this._ownerId=t,this._key=o}getOauthConsumer(){return e({id:this._ownerId,key:this._key}).then(i({apiConfig:this._apiConfig}))}updateOauthConsumer(e){const o=Object.fromEntries(Object.entries(e.rights).map((([e,t])=>[s(e),t]))),n={name:e.name,url:e.url,baseUrl:e.baseUrl,description:e.description,picture:e.picture,rights:o};return t({id:this._ownerId,key:this._key},n).then(i({apiConfig:this._apiConfig}))}deleteOauthConsumer(){return o({id:this._ownerId,key:this._key}).then(i({apiConfig:this._apiConfig}))}}
//# sourceMappingURL=cc-oauth-consumer-form.smart-update.js.map
