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