import{addDomain as n,removeDomain as e,unmarkFavouriteDomain as a,markFavouriteDomain as i,getFavouriteDomain as o,getAllDomains as t}from"@clevercloud/client/esm/api/v2/application.js";import{getDefaultLoadBalancersDnsInfo as d}from"@clevercloud/client/esm/api/v4/load-balancers.js";import{isTestDomain as r,parseDomain as m,getHostWithWildcard as s}from"./lib/domain.js";import{notifySuccess as c,notify as p,notifyError as l}from"./lib/notifications.js";import{sendToApi as f}from"./lib/send-to-api.js";import{defineSmartComponent as g}from"./lib/smart/define-smart-component.js";import{i18n as u}from"./translation.js";import"./cc-smart-container.js";import{CcDomainPrimaryChangeEvent as h}from"./components/cc-domain-management/cc-domain-management.events.js";import{CcDomainManagement as I}from"./cc-domain-management.js";function y(n,e,a,i){return o({id:n,appId:e}).then(f({apiConfig:a,signal:i})).catch((n=>{if(404===n.response.status)return null}))}function C(n,e,a){return t({id:n,appId:e}).then(f({apiConfig:a})).then((n=>n.map((({fqdn:n})=>{const{hostname:e,pathname:a,isWildcard:i}=m(n);return{id:n,hostname:e,pathPrefix:a,isWildcard:i}}))))}g({selector:"cc-domain-management",params:{apiConfig:{type:Object},appId:{type:String},ownerId:{type:String}},onContextUpdate({component:o,context:t,onEvent:m,updateComponent:g,signal:w}){const{apiConfig:S,appId:b,ownerId:j}=t;function v(n,e){g("domainListState",(a=>{const i=a.domains.find((e=>e.id===n));null!=i&&e(i)}))}g("applicationId",b),function({apiConfig:n,ownerId:e,appId:a,signal:i}){return Promise.all([y(e,a,n,i),C(e,a,n)]).then((([n,e])=>e.map((e=>({...e,type:"idle",isPrimary:n?.fqdn===e.id})))))}({apiConfig:S,ownerId:j,appId:b,signal:w}).then((n=>{g("domainListState",{type:"loaded",domains:n})})).catch((n=>{console.error(n),g("domainListState",{type:"error"})})),function({apiConfig:n,ownerId:e,appId:a,signal:i}){return d({appId:a,ownerId:e}).then(f({apiConfig:n,signal:i})).then((n=>{const e=n[0];return{cnameRecord:e?.dns?.cname,aRecords:e?.dns?.a}}))}({apiConfig:S,ownerId:j,appId:b,signal:w}).then((({cnameRecord:n,aRecords:e})=>{g("dnsInfoState",{type:"loaded",cnameRecord:n,aRecords:e})})).catch((n=>{console.error(n),g("dnsInfoState",{type:"error"})})),m("cc-domain-add",(({hostname:e,pathPrefix:a,isWildcard:i})=>{const o=s(e+a,i);g("domainFormState",(n=>{n.type="adding"})),function({apiConfig:e,ownerId:a,appId:i,hostname:o,pathPrefix:t,isWildcard:d}){const r=s(o,d)+encodeURIComponent(t);return n({id:a,appId:i,domain:r}).then(f({apiConfig:e}))}({apiConfig:S,ownerId:j,appId:b,hostname:e,pathPrefix:a,isWildcard:i}).then((()=>{r(e)?c(u("cc-domain-management.form.submit.success",{domain:o})):p({intent:"info",message:u("cc-domain-management.form.submit.success-config",{domain:o}),options:{timeout:0,closeable:!0}}),g("domainFormState",I.INIT_DOMAIN_FORM_STATE),g("domainListState",(n=>{const o={id:e+a,hostname:e,type:"idle",pathPrefix:a,isWildcard:i,isPrimary:!1};n.domains.push(o)}))})).catch((n=>{console.error(n);const e=function(n){if("Invalid domain"===n.message||n.message.includes("is invalid"))return"invalid-format";if(n.message.includes("is already taken")||n.message.includes("You are not allowed to use"))return"already-used";return null}(n);g("domainFormState",(n=>{n.type="idle"})),"invalid-format"!==e?"already-used"!==e?l(u("cc-domain-management.form.submit.error",{domain:o})):l(u("cc-domain-management.form.submit.error-duplicate.text",{domain:o}),u("cc-domain-management.form.submit.error-duplicate.heading")):g("domainFormState",(n=>{n.hostname.error={code:e}}))}))})),m("cc-domain-delete",(({id:n,hostname:a,pathPrefix:i,isWildcard:o})=>{const t=s(a+i,o);v(n,(n=>{n.type="deleting"})),function({apiConfig:n,ownerId:a,appId:i,id:o}){return e({id:a,appId:i,domain:encodeURIComponent(o)}).then(f({apiConfig:n}))}({apiConfig:S,ownerId:j,appId:b,id:n}).then((()=>{g("domainListState",(e=>{e.domains=e.domains.filter((e=>e.id!==n))})),c(u("cc-domain-management.list.delete.success",{domain:t}))})).catch((e=>{console.error(e),l(u("cc-domain-management.list.delete.error",{domain:t})),v(n,(n=>{n.type="idle"}))}))})),m("cc-domain-mark-as-primary",(({id:n,hostname:e,pathPrefix:t,isWildcard:d})=>{const r=s(e+t,d);v(n,(n=>{n.type="marking-primary"})),function({apiConfig:n,ownerId:e,appId:o,id:t}){return a({id:e,appId:o}).then(f({apiConfig:n})).then((()=>i({id:e,appId:o},{fqdn:t}).then(f({apiConfig:n}))))}({apiConfig:S,ownerId:j,appId:b,id:n}).then((()=>{g("domainListState",(e=>{e.domains=e.domains.map((e=>({...e,type:"idle",isPrimary:e.id===n})))})),o.dispatchEvent(new h(n)),c(u("cc-domain-management.list.primary.success",{domain:r}))})).catch((e=>{console.error(e),v(n,(n=>{n.type="idle"})),3004!==e.id?l(u("cc-domain-management.list.primary.error",{domain:r})):l(u("cc-domain-management.list.error-not-found.text",{domain:r}),u("cc-domain-management.list.error-not-found.heading"))}))}))}});
//# sourceMappingURL=cc-domain-management.smart.js.map
