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