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 r,notifyError as a}from"./lib/notifications.js";import{sendToApi as c}from"./lib/send-to-api.js";import{defineSmartComponent as i}from"./lib/smart/define-smart-component.js";import{i18n as p}from"./translation.js";import"./cc-smart-container.js";import"./cc-tcp-redirection-form.js";const s=["default","cleverapps"];i({selector:"cc-tcp-redirection-form",params:{apiConfig:{type:Object},ownerId:{type:String},appId:{type:String}},onContextUpdate({context:i,onEvent:d,updateComponent:m,signal:l}){const{apiConfig:f,ownerId:g,appId:u}=i;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:r}){return t({id:n,appId:o},{namespace:r}).then(c({apiConfig:e}))}({apiConfig:f,ownerId:g,appId:u,namespace:e}).then((({port:t})=>{r(p("cc-tcp-redirection-form.create.success",{namespace:e})),I(e,(e=>{e.type="loaded",e.sourcePort=t}))})).catch((t=>{console.error(t),a(p("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:r,namespace:a}){return n({id:t,appId:o,sourcePort:r?.toString(),namespace:a}).then(c({apiConfig:e}))}({apiConfig:f,ownerId:g,appId:u,sourcePort:t,namespace:e}).then((()=>{r(p("cc-tcp-redirection-form.delete.success",{namespace:e})),I(e,(e=>{e.type="loaded",e.sourcePort=null}))})).catch((t=>{console.error(t),a(p("cc-tcp-redirection-form.delete.error",{namespace:e})),I(e,(e=>{e.type="loaded"}))}))})),m("state",{type:"loading"}),m("applicationId",u),async function({apiConfig:t,signal:n,ownerId:r,appId:a}){return Promise.all([o({id:r}).then(c({apiConfig:t,signal:n})),e({id:r,appId:a}).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:g,appId:u,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
