import{addDomain as n,removeDomain as a,unmarkFavouriteDomain as e,markFavouriteDomain as i,getFavouriteDomain as o,getAllDomains as t}from"@clevercloud/client/esm/api/v2/application.js";import{defineSmartComponent as d}from"./lib/define-smart-component.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{i18n as g}from"./translation.js";import"./cc-smart-container.js";import{CcDomainManagement as u}from"./cc-domain-management.js";function h(n,a,e,i){return o({id:n,appId:a}).then(f({apiConfig:e,signal:i})).catch((n=>{if(404===n.response.status)return null}))}function I(n,a,e){return t({id:n,appId:a}).then(f({apiConfig:e})).then((n=>n.map((({fqdn:n})=>{const{hostname:a,pathname:e,isWildcard:i}=m(n);return{id:n,hostname:a,pathPrefix:e,isWildcard:i}}))))}d({selector:"cc-domain-management",params:{apiConfig:{type:Object},appId:{type:String},ownerId:{type:String}},onContextUpdate({context:o,onEvent:t,updateComponent:d,signal:m}){const{apiConfig:y,appId:C,ownerId:w}=o;function S(n,a){d("domainListState",(e=>{const i=e.domains.find((a=>a.id===n));null!=i&&a(i)}))}(function({apiConfig:n,ownerId:a,appId:e,signal:i}){return Promise.all([h(a,e,n,i),I(a,e,n)]).then((([n,a])=>a.map((a=>({...a,type:"idle",isPrimary:n?.fqdn===a.id})))))})({apiConfig:y,ownerId:w,appId:C,signal:m}).then((n=>{d("domainListState",{type:"loaded",domains:n})})).catch((n=>{console.error(n),d("domainListState",{type:"error"})})),function({apiConfig:n,ownerId:a,appId:e,signal:i}){return function({appId:n,ownerId:a}){return Promise.resolve({method:"get",url:`/v4/load-balancers/organisations/${a}/applications/${n}/load-balancers/default`,headers:{Accept:"application/json"}})}({appId:e,ownerId:a}).then(f({apiConfig:n,signal:i})).then((n=>{const a=n[0];return{cnameRecord:a?.dns?.cname,aRecords:a?.dns?.a}}))}({apiConfig:y,ownerId:w,appId:C,signal:m}).then((({cnameRecord:n,aRecords:a})=>{d("dnsInfoState",{type:"loaded",cnameRecord:n,aRecords:a})})).catch((n=>{console.error(n),d("dnsInfoState",{type:"error"})})),t("cc-domain-management:add",(({hostname:a,pathPrefix:e,isWildcard:i})=>{const o=s(a+e,i);d("domainFormState",(n=>{n.type="adding"})),function({apiConfig:a,ownerId:e,appId:i,hostname:o,pathPrefix:t,isWildcard:d}){const r=s(o,d)+encodeURIComponent(t);return n({id:e,appId:i,domain:r}).then(f({apiConfig:a}))}({apiConfig:y,ownerId:w,appId:C,hostname:a,pathPrefix:e,isWildcard:i}).then((()=>{r(a)?c(g("cc-domain-management.form.submit.success",{domain:o})):p({intent:"info",message:g("cc-domain-management.form.submit.success-config",{domain:o}),options:{timeout:0,closeable:!0}}),d("domainFormState",u.INIT_DOMAIN_FORM_STATE),d("domainListState",(n=>{const o={id:null!=e&&"/"!==e?a+e:a,hostname:a,type:"idle",pathPrefix:e,isWildcard:i,isPrimary:!1};n.domains.push(o)}))})).catch((n=>{console.error(n);const a=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);d("domainFormState",(n=>{n.type="idle"})),"invalid-format"!==a?"already-used"!==a?l(g("cc-domain-management.form.submit.error",{domain:o})):l(g("cc-domain-management.form.submit.error-duplicate.text",{domain:o}),g("cc-domain-management.form.submit.error-duplicate.heading")):d("domainFormState",(n=>{n.hostname.error={code:a}}))}))})),t("cc-domain-management:delete",(({id:n,hostname:e,pathPrefix:i,isWildcard:o})=>{const t=s(e+i,o);S(n,(n=>{n.type="deleting"})),function({apiConfig:n,ownerId:e,appId:i,id:o}){return a({id:e,appId:i,domain:encodeURIComponent(o)}).then(f({apiConfig:n}))}({apiConfig:y,ownerId:w,appId:C,id:n}).then((()=>{d("domainListState",(a=>{a.domains=a.domains.filter((a=>a.id!==n))})),c(g("cc-domain-management.list.delete.success",{domain:t}))})).catch((a=>{console.error(a),l(g("cc-domain-management.list.delete.error",{domain:t})),S(n,(n=>{n.type="idle"}))}))})),t("cc-domain-management:mark-as-primary",(({id:n,hostname:a,pathPrefix:o,isWildcard:t})=>{const r=s(a+o,t);S(n,(n=>{n.type="marking-primary"})),function({apiConfig:n,ownerId:a,appId:o,id:t}){return e({id:a,appId:o}).then(f({apiConfig:n})).then((()=>i({id:a,appId:o},{fqdn:t}).then(f({apiConfig:n}))))}({apiConfig:y,ownerId:w,appId:C,id:n}).then((()=>{d("domainListState",(a=>{a.domains=a.domains.map((a=>({...a,type:"idle",isPrimary:a.id===n})))}));const a=new CustomEvent("domain-management-primary-change",{detail:n});window.dispatchEvent(a),c(g("cc-domain-management.list.primary.success",{domain:r}))})).catch((a=>{console.error(a),S(n,(n=>{n.type="idle"})),3004!==a.id?l(g("cc-domain-management.list.primary.error",{domain:r})):l(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
