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{notifySuccess as m,notifyError as n}from"./lib/notifications.js";import{sendToApi as a}from"./lib/send-to-api.js";import{defineSmartComponent as s}from"./lib/smart/define-smart-component.js";import{i18n as c}from"./translation.js";import"./cc-smart-container.js";import{CcOrgaMemberLeftEvent as l}from"./components/cc-orga-member-list/cc-orga-member-list.events.js";import{CcOrgaMemberList as d}from"./cc-orga-member-list.js";function b({apiConfig:e,ownerId:r,id:i}){return t({id:r,userId:i}).then(a({apiConfig:e}))}s({selector:"cc-orga-member-list",params:{apiConfig:{type:Object},ownerId:{type:String}},onContextUpdate({component:t,context:s,onEvent:g,updateComponent:u,signal:p}){const{apiConfig:f,ownerId:h}=s;function v(e){const r="ADMIN"===e||"MANAGER"===e;u("authorisations",{invite:r,edit:r,delete:r})}function I(e,r){u("memberListState",(t=>{const i=t.memberList.find((r=>r.id===e));null!=i&&r(i)}))}g("cc-orga-member-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(a({apiConfig:e}))}({apiConfig:f,ownerId:h,email:e.trim(),role:i}).then((()=>{m(c("cc-orga-member-list.invite.submit.success",{userEmail:e})),t.resetInviteMemberForm()})).catch((r=>{console.error(r),6451===r.id?n(c("cc-orga-member-list.error.unauthorised.text"),c("cc-orga-member-list.error.unauthorised.heading")):"You have performed that request too much."===r.message?n(c("cc-orga-member-list.invite.submit.error-rate-limit.message"),c("cc-orga-member-list.invite.submit.error-rate-limit.title")):n(c("cc-orga-member-list.invite.submit.error",{userEmail:e}))})).finally((()=>{t.inviteMemberFormState={type:"idle"}}))})),g("cc-orga-member-update",(({id:e,role:r,newRole:o,name:s,email:l,isCurrentUser:d})=>{"loaded"===t.memberListState.type&&(!function(e,r){return"ADMIN"===e&&"MANAGER"===r.find((e=>e.isCurrentUser)).role}(r,t.memberListState.memberList)?(I(e,(e=>{e.type="updating"})),function({apiConfig:e,ownerId:r,id:t,newRole:o}){return i({id:r,userId:t},{role:o}).then(a({apiConfig:e}))}({apiConfig:f,ownerId:h,id:e,newRole:o}).then((()=>{m(c("cc-orga-member-list.edit.success",{memberIdentity:s??l})),I(e,(e=>{e.type="loaded",e.role=o})),d&&v(o)})).catch((r=>{console.error(r),6451===r.id||6452===r.id?n(c("cc-orga-member-list.error.unauthorised.text"),c("cc-orga-member-list.error.unauthorised.heading")):6501===r.id?n(c("cc-orga-member-list.error-member-not-found.text"),c("cc-orga-member-list.error-member-not-found.heading")):n(c("cc-orga-member-list.edit.error",{memberIdentity:s??l})),I(e,(e=>{e.type="editing"}))}))):n(c("cc-orga-member-list.error.unauthorised.text"),c("cc-orga-member-list.error.unauthorised.heading")))})),g("cc-orga-member-delete",(({id:e,name:r,email:t})=>{I(e,(e=>{e.type="deleting"})),b({apiConfig:f,ownerId:h,id:e}).then((()=>{m(c("cc-orga-member-list.delete.success",{memberIdentity:r??t})),u("memberListState",(r=>{r.memberList=r.memberList.filter((r=>r.id!==e))}))})).catch((i=>{console.error(i),6452===i.id?n(c("cc-orga-member-list.error.unauthorised.text"),c("cc-orga-member-list.error.unauthorised.heading")):6501===i.id?n(c("cc-orga-member-list.error-member-not-found.text"),c("cc-orga-member-list.error-member-not-found.heading")):n(c("cc-orga-member-list.delete.error",{memberIdentity:r??t})),I(e,(e=>{e.type="loaded"}))}))})),g("cc-orga-member-leave",(e=>{I(e.id,(e=>{e.type="deleting"})),u("memberListState",(e=>{e.dangerZoneState="leaving"})),b({apiConfig:f,ownerId:h,id:e.id}).then((()=>{m(c("cc-orga-member-list.leave.success")),v(),u("memberListState",{type:"error"}),t.dispatchEvent(new l(e))})).catch((r=>{console.error(r),n(c("cc-orga-member-list.leave.error")),I(e.id,(e=>{e.type="loaded"})),u("memberListState",(e=>{e.dangerZoneState="idle"}))}))})),u("authorisations",d.INIT_AUTHORISATIONS),t.resetInviteMemberForm(),t.inviteMemberFormState={type:"idle"},u("memberListState",{type:"loading"}),function({apiConfig:r,ownerId:t,signal:i}){return Promise.all([o().then(a({apiConfig:r,signal:i})),e({id:t}).then(a({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:h,signal:p}).then((e=>{v(e.find((e=>e.isCurrentUser)).role),u("memberListState",{type:"loaded",memberList:e.map((e=>({type:"loaded",...e}))),identityFilter:"",mfaDisabledOnlyFilter:!1,dangerZoneState:"idle"})})).catch((e=>{console.error(e),u("memberListState",{type:"error"})}))}});
//# sourceMappingURL=cc-orga-member-list.smart.js.map
