import{get as n,getAllTags as d,update as a,replaceAddonTags as e,remove as t}from"@clevercloud/client/esm/api/v2/addon.js";import{i18n as o}from"./i18n.js";import{notifySuccess as i,notifyError as s}from"./lib/notifications.js";import{sendToApi as r}from"./lib/send-to-api.js";import{defineSmartComponent as c}from"./lib/smart/define-smart-component.js";import"./cc-smart-container.js";import{CcAddonNameWasChangedEvent as p,CcAddonWasDeletedEvent as g}from"./components/cc-addon-admin/cc-addon-admin.events.js";import"./cc-addon-admin.js";c({selector:"cc-addon-admin",params:{apiConfig:{type:Object},ownerId:{type:String},addonId:{type:String}},onContextUpdate({component:n,context:d,onEvent:a,updateComponent:e,signal:t}){const{apiConfig:r,ownerId:c,addonId:l}=d,h=new m({apiConfig:r,signal:t});e("state",{type:"loading"}),h.fetchAddonAndTags({ownerId:c,addonId:l}).then((({addon:n,tags:d})=>{e("state",{type:"loaded",id:n.id,name:n.name,tags:d})})).catch((n=>{console.error(n),e("state",{type:"error"})})),a("cc-addon-name-change",(({name:d})=>{e("state",(n=>({...n,name:d,type:"updating-name"}))),h.onUpdateName({ownerId:c,addonId:l,name:d}).then((()=>{i(o("cc-addon-admin.update-name.success")),n.dispatchEvent(new p({id:l,name:d})),e("state",(n=>({...n,type:"loaded"})))})).catch((n=>{console.error(n),s(o("cc-addon-admin.update-name.error")),e("state",(n=>({...n,type:"loaded"})))}))})),a("cc-addon-tags-change",(({tags:n})=>{e("state",(d=>({...d,tags:n,type:"updating-tags"}))),h.onUpdateTags({ownerId:c,addonId:l,tags:n}).then((()=>{i(o("cc-addon-admin.update-tags.success")),e("state",(n=>({...n,type:"loaded"})))})).catch((n=>{console.error(n),e("state",(n=>({...n,type:"loaded"}))),s(o("cc-addon-admin.update-tags.error"))}))})),a("cc-addon-delete",(({id:d,name:a})=>{e("state",(n=>({...n,type:"deleting"}))),h.onDeleteAddon({ownerId:c,addonId:l}).then((()=>{e("state",(n=>({...n,type:"loaded"}))),i(o("cc-addon-admin.delete.success",{name:a})),n.dispatchEvent(new g({id:d,name:a}))})).catch((n=>{console.error(n),s(o("cc-addon-admin.delete.error",{name:a})),e("state",(n=>({...n,type:"loaded"})))}))}))}});class m{constructor({apiConfig:n,signal:d}){this._apiConfig=n,this._signal=d}fetchAddon({ownerId:d,addonId:a}){return n({id:d,addonId:a}).then(r({apiConfig:this._apiConfig,signal:this._signal}))}fetchTags({ownerId:n,addonId:a}){return d({id:n,addonId:a}).then(r({apiConfig:this._apiConfig,signal:this._signal}))}async fetchAddonAndTags({ownerId:n,addonId:d}){return Promise.all([this.fetchAddon({ownerId:n,addonId:d}),this.fetchTags({ownerId:n,addonId:d})]).then((([n,d])=>({addon:n,tags:d})))}onUpdateName({ownerId:n,addonId:d,name:e}){return a({id:n,addonId:d},{name:e}).then(r({apiConfig:this._apiConfig,signal:this._signal}))}onUpdateTags({ownerId:n,addonId:d,tags:a}){return e({id:n,addonId:d},a).then(r({apiConfig:this._apiConfig,signal:this._signal}))}async onDeleteAddon({ownerId:n,addonId:d}){return t({id:n,addonId:d}).then(r({apiConfig:this._apiConfig,signal:this._signal}))}}
//# sourceMappingURL=cc-addon-admin.smart.js.map
