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