import{getAllMembers as e,addMember as r,removeMemeber as t,updateMember as i}from"@clevercloud/client/esm/api/v2/organisation.js";import{getId as o}from"@clevercloud/client/esm/api/v2/user.js";import{defineSmartComponent as n}from"./lib/define-smart-component.js";import{notifySuccess as a,notifyError as m}from"./lib/notifications.js";import{sendToApi as s}from"./lib/send-to-api.js";import{i18n as l}from"./translation.js";import"./cc-smart-container.js";import{CcOrgaMemberList as c}from"./cc-orga-member-list.js";function d({apiConfig:e,ownerId:r,id:i}){return t({id:r,userId:i}).then(s({apiConfig:e}))}function g({apiConfig:e,ownerId:r,id:t,newRole:o}){return i({id:r,userId:t},{role:o}).then(s({apiConfig:e}))}n({selector:"cc-orga-member-list",params:{apiConfig:{type:Object},ownerId:{type:String}},onContextUpdate({component:t,context:i,onEvent:n,updateComponent:u,signal:b}){const{apiConfig:f,ownerId:p}=i;function h(e){const r="ADMIN"===e||"MANAGER"===e;u("authorisations",{invite:r,edit:r,delete:r})}function v(e,r){u("members",(t=>{const i=t.value.find((r=>r.id===e));null!=i&&r(i)}))}n("cc-orga-member-list:invite",(({email:e,role:i})=>{t.inviteMemberFormState={type:"inviting"},function({apiConfig:e,ownerId:t,email:i,role:o}){return r({id:t},{email:i,role:o,job:null}).then(s({apiConfig:e}))}({apiConfig:f,ownerId:p,email:e.trim(),role:i}).then((()=>{a(l("cc-orga-member-list.invite.submit.success",{userEmail:e})),t.resetInviteMemberForm()})).catch((r=>{console.error(r),6451===r.id?m(l("cc-orga-member-list.error.unauthorised.text"),l("cc-orga-member-list.error.unauthorised.heading")):"You have performed that request too much."===r.message?m(l("cc-orga-member-list.invite.submit.error-rate-limit.message"),l("cc-orga-member-list.invite.submit.error-rate-limit.title")):m(l("cc-orga-member-list.invite.submit.error",{userEmail:e}))})).finally((()=>{t.inviteMemberFormState={type:"idle"}}))})),n("cc-orga-member-list:update",(({id:e,role:r,newRole:i,name:o,email:n,isCurrentUser:s})=>{"loaded"===t.members.state&&(!function(e,r){return"ADMIN"===e&&"MANAGER"===r.find((e=>e.isCurrentUser)).role}(r,t.members.value)?(v(e,(e=>{e.state="updating"})),g({apiConfig:f,ownerId:p,id:e,newRole:i}).then((()=>{a(l("cc-orga-member-list.edit.success",{memberIdentity:o??n})),v(e,(e=>{e.state="loaded",e.role=i})),s&&h(i)})).catch((r=>{console.error(r),6451===r.id||6452===r.id?m(l("cc-orga-member-list.error.unauthorised.text"),l("cc-orga-member-list.error.unauthorised.heading")):6501===r.id?m(l("cc-orga-member-list.error-member-not-found.text"),l("cc-orga-member-list.error-member-not-found.heading")):m(l("cc-orga-member-list.edit.error",{memberIdentity:o??n})),v(e,(e=>{e.state="editing"}))}))):m(l("cc-orga-member-list.error.unauthorised.text"),l("cc-orga-member-list.error.unauthorised.heading")))})),n("cc-orga-member-card:delete",(({id:e,name:r,email:t})=>{v(e,(e=>{e.state="deleting"})),d({apiConfig:f,ownerId:p,id:e}).then((()=>{a(l("cc-orga-member-list.delete.success",{memberIdentity:r??t})),u("members",(r=>{r.value=r.value.filter((r=>r.id!==e))}))})).catch((i=>{console.error(i),6452===i.id?m(l("cc-orga-member-list.error.unauthorised.text"),l("cc-orga-member-list.error.unauthorised.heading")):6501===i.id?m(l("cc-orga-member-list.error-member-not-found.text"),l("cc-orga-member-list.error-member-not-found.heading")):m(l("cc-orga-member-list.delete.error",{memberIdentity:r??t})),v(e,(e=>{e.state="loaded"}))}))})),n("cc-orga-member-list:leave",(({id:e})=>{v(e,(e=>{e.state="deleting"})),u("members",(e=>{e.dangerZoneState="leaving"})),d({apiConfig:f,ownerId:p,id:e}).then((()=>{a(l("cc-orga-member-list.leave.success")),h(),u("members",{state:"error"}),window.dispatchEvent(new Event("orga-member-leave-success"))})).catch((r=>{console.error(r),m(l("cc-orga-member-list.leave.error")),v(e,(e=>{e.state="loaded"})),u("members",(e=>{e.dangerZoneState="idle"}))}))})),u("authorisations",c.INIT_AUTHORISATIONS),t.resetInviteMemberForm(),t.inviteMemberFormState={type:"idle"},u("members",{state:"loading"}),function({apiConfig:r,ownerId:t,signal:i}){return Promise.all([o().then(s({apiConfig:r,signal:i})),e({id:t}).then(s({apiConfig:r,signal:i}))]).then((([{ownerId:e},r])=>r.map((({member:r,role:t,job:i})=>({id:r.id,avatar:r.avatar,name:r.name,jobTitle:i,role:t,email:r.email,isMfaEnabled:"TOTP"===r.preferredMFA,isCurrentUser:r.id===e})))))}({apiConfig:f,ownerId:p,signal:b}).then((e=>{h(e.find((e=>e.isCurrentUser)).role),u("members",{state:"loaded",value:e.map((e=>({state:"loaded",...e}))),identityFilter:"",mfaDisabledOnlyFilter:!1})})).catch((e=>{console.error(e),u("members",{state:"error"})}))}});
//# sourceMappingURL=cc-orga-member-list.smart.js.map
