import{getTcpRedirs as e,addTcpRedir as n,removeTcpRedir as t}from"@clevercloud/client/esm/api/v2/application.js";import{getNamespaces as o}from"@clevercloud/client/esm/api/v2/organisation.js";import{defineSmartComponent as a}from"./lib/define-smart-component.js";import{notifySuccess as r,notifyError as c}from"./lib/notifications.js";import{sendToApi as p}from"./lib/send-to-api.js";import{i18n as i}from"./translation.js";import"./cc-smart-container.js";import"./cc-tcp-redirection-form.js";const s=["default","cleverapps"];a({selector:"cc-tcp-redirection-form",params:{apiConfig:{type:Object},ownerId:{type:String},appId:{type:String}},onContextUpdate({container:a,component:d,context:m,onEvent:l,updateComponent:f,signal:u}){const{apiConfig:g,ownerId:I,appId:y}=m;function C(e,n){f("state",(t=>{const o=t.redirections.find((n=>n.namespace===e));null!=o&&n(o)}))}l("cc-tcp-redirection:create",(({namespace:e})=>{C(e,(e=>{e.type="waiting"})),async function({apiConfig:e,ownerId:t,appId:o,namespace:a}){return n({id:t,appId:o,payment:"accepted"},{namespace:a}).then(p({apiConfig:e}))}({apiConfig:g,ownerId:I,appId:y,namespace:e}).then((({port:n})=>{r(i("cc-tcp-redirection-form.create.success",{namespace:e})),C(e,(e=>{e.type="loaded",e.sourcePort=n}))})).catch((n=>{console.error(n),c(i("cc-tcp-redirection-form.create.error",{namespace:e})),C(e,(e=>{e.type="loaded"}))}))})),l("cc-tcp-redirection:delete",(({namespace:e,sourcePort:n})=>{C(e,(e=>{e.type="waiting"})),async function({apiConfig:e,ownerId:n,appId:o,sourcePort:a,namespace:r}){return t({id:n,appId:o,sourcePort:a,namespace:r}).then(p({apiConfig:e}))}({apiConfig:g,ownerId:I,appId:y,sourcePort:n,namespace:e}).then((()=>{r(i("cc-tcp-redirection-form.delete.success",{namespace:e})),C(e,(e=>{e.type="loaded",e.sourcePort=null}))})).catch((n=>{console.error(n),c(i("cc-tcp-redirection-form.delete.error",{namespace:e})),C(e,(e=>{e.type="loaded"}))}))})),f("state",{type:"loading"}),async function({apiConfig:n,signal:t,ownerId:a,appId:r}){return Promise.all([o({id:a}).then(p({apiConfig:n,signal:t})),e({id:a,appId:r}).then(p({apiConfig:n,signal:t}))]).then((([e,n])=>e.map((({namespace:e})=>{const t=n.find((n=>n.namespace===e))?.port,o=!s.includes(e);return{namespace:e,sourcePort:t,isPrivate:o}}))))}({apiConfig:g,ownerId:I,appId:y,signal:u}).then((e=>{f("state",{type:"loaded",redirections:e.map((e=>({type:"loaded",...e})))})})).catch((e=>{console.error(e),f("state",{type:"error"})}))}});
//# sourceMappingURL=cc-tcp-redirection-form.smart.js.map
