import{todo_getEmailAddresses as e,todo_getConfirmationEmail as i,todo_addEmailAddress as a,todo_removeEmailAddress as s}from"@clevercloud/client/esm/api/v2/user.js";import{get as r}from"@clevercloud/client/esm/api/v2/organisation.js";import{notify as t,notifyError as d,notifySuccess as n}from"./lib/notifications.js";import{sendToApi as l}from"./lib/send-to-api.js";import{defineSmartComponent as o}from"./lib/smart/define-smart-component.js";import{i18n as m}from"./translation.js";import"./cc-smart-container.js";import"./cc-email-list.js";o({selector:"cc-email-list",params:{apiConfig:{type:Object}},onContextUpdate({component:o,context:c,onEvent:p,updateComponent:y,signal:f}){y("emailListState",{type:"loading"}),y("addEmailFormState",{type:"idle"}),o.resetAddEmailForm();const h=function(t,d){return{fetchEmailAddresses(){return Promise.all([this.fetchPrimaryEmailAddress(),this.fetchSecondaryEmailAddresses()]).then((([e,i])=>({self:e,secondary:i})))},fetchPrimaryEmailAddress:()=>r({}).then(l({apiConfig:t,signal:d})),fetchSecondaryEmailAddresses:()=>e().then(l({apiConfig:t,signal:d})),sendConfirmationEmail:()=>i().then(l({apiConfig:t})),addSecondaryEmailAddress:e=>a({email:e},{}).then(l({apiConfig:t})),deleteSecondaryEmailAddress:e=>s({email:e}).then(l({apiConfig:t})),markSecondaryEmailAddressAsPrimary:e=>a({email:e},{make_primary:!0}).then(l({apiConfig:t}))}}(c.apiConfig,f);function u(e,i){y("emailListState",(a=>{const s=a.emailList.secondaryAddresses.find((i=>i.address===e));null!=s&&i(s)}))}h.fetchEmailAddresses().then((({self:e,secondary:i})=>{y("emailListState",{type:"loaded",emailList:{primaryAddress:{type:"idle",address:e.email,verified:e.emailValidated},secondaryAddresses:i.map((e=>({type:"idle",address:e,verified:!0})))}})})).catch((e=>{console.error(e),y("emailListState",{type:"error"})})),p("cc-email-send-confirmation",(e=>{y("emailListState",(e=>{e.emailList.primaryAddress.type="sending-confirmation-email"})),h.sendConfirmationEmail().then((()=>{t({intent:"info",title:m("cc-email-list.primary.action.resend-confirmation-email.success.title"),message:m("cc-email-list.primary.action.resend-confirmation-email.success.message",{address:e}),options:{timeout:0,closeable:!0}})})).catch((i=>{console.error(i),d(m("cc-email-list.primary.action.resend-confirmation-email.error",{address:e}))})).finally((()=>{y("emailListState",(e=>{e.emailList.primaryAddress.type="idle"}))}))})),p("cc-email-add",(e=>{y("addEmailFormState",(e=>{e.type="adding"})),h.addSecondaryEmailAddress(e).then((()=>{t({intent:"info",title:m("cc-email-list.secondary.action.add.success.title"),message:m("cc-email-list.secondary.action.add.success.message",{address:e}),options:{timeout:0,closeable:!0}}),o.resetAddEmailForm()})).catch((i=>{const a=function(e){if(550===e)return"invalid";if(101===e)return"already-defined";if(1004===e)return"used";return null}(i.id);null==a?(console.error(i),d(m("cc-email-list.secondary.action.add.error",{address:e}))):y("addEmailFormState",(e=>{e.errors={email:a}}))})).finally((()=>{y("addEmailFormState",(e=>{e.type="idle"}))}))})),p("cc-email-delete",(e=>{u(e,(e=>{e.type="deleting"})),h.deleteSecondaryEmailAddress(e).then((()=>{n(m("cc-email-list.secondary.action.delete.success",{address:e})),y("emailListState",(i=>{i.emailList.secondaryAddresses=i.emailList.secondaryAddresses.filter((i=>i.address!==e))}))})).catch((i=>{console.error(i),d(m("cc-email-list.secondary.action.delete.error",{address:e})),u(e,(e=>{e.type="idle"}))}))})),p("cc-email-mark-as-primary",(e=>{u(e,(e=>{e.type="marking-as-primary"})),h.markSecondaryEmailAddressAsPrimary(e).then((()=>{if(n(m("cc-email-list.secondary.action.mark-as-primary.success",{address:e})),"loaded"===o.emailListState.type){const i=o.emailListState.emailList.primaryAddress.address;y("emailListState",(i=>{i.emailList.primaryAddress.address=e})),u(e,(e=>{e.type="idle",e.address=i}))}})).catch((i=>{console.error(i),d(m("cc-email-list.secondary.action.mark-as-primary.error",{address:e})),u(e,(e=>{e.type="idle"}))}))}))}});
//# sourceMappingURL=cc-email-list.smart.js.map
