import{a as h}from"./chunk-ORHSDUMX.js";import{b as c}from"./chunk-XFS4SIQZ.js";import{a as m}from"./chunk-ZSSEAP7S.js";import{Ha as r,e as t,l as d,o,x as n}from"./chunk-LH5Z5GGI.js";var g=class l{api=o(h);auth=o(m);i18n=o(c);_orgs=n([]);_selectedId=n(null);_members=n([]);_invites=n([]);_incoming=n([]);_loading=n(!1);_busy=n(!1);_error=n(null);_notice=n(null);orgs=this._orgs.asReadonly();members=this._members.asReadonly();invites=this._invites.asReadonly();incoming=this._incoming.asReadonly();loading=this._loading.asReadonly();busy=this._busy.asReadonly();error=this._error.asReadonly();notice=this._notice.asReadonly();selectedOrg=r(()=>this._orgs().find(e=>e.id===this._selectedId())??null);myRole=r(()=>this.selectedOrg()?.role??null);canManage=r(()=>this.myRole()==="owner"||this.myRole()==="admin");isOwner=r(()=>this.myRole()==="owner");myUserId=r(()=>this.auth.user()?.id??null);async init(){this._loading.set(!0),this._error.set(null);try{let[e,i]=await Promise.all([t(this.api.orgs()),t(this.api.incomingInvitations()).catch(()=>[])]);this._orgs.set(e),this._incoming.set(i);let a=e.find(u=>u.id===this._selectedId())??e[0]??null;a?await this.select(a.id):(this._selectedId.set(null),this._members.set([]),this._invites.set([]))}catch(e){this._error.set(this.msg(e,this.i18n.t("iam.loadTeamsFailed")))}finally{this._loading.set(!1)}}async select(e){this._selectedId.set(e),this._error.set(null);try{let i=await t(this.api.members(e));this._members.set(i)}catch(i){this._members.set([]),this._error.set(this.msg(i,this.i18n.t("iam.loadMembersFailed")))}if(this.canManage())try{this._invites.set(await t(this.api.invitations(e)))}catch{this._invites.set([])}else this._invites.set([])}async createOrg(e,i){return this.run(async()=>{let s=await t(this.api.createOrg(e,i));await this.init(),await this.select(s.id),this._notice.set(this.i18n.t("iam.orgCreated",{name:s.name}))})}async renameOrg(e){let i=this._selectedId();return i?this.run(async()=>{await t(this.api.renameOrg(i,e)),await this.init(),this._notice.set(this.i18n.t("iam.orgRenamed"))}):!1}async deleteOrg(){let e=this._selectedId();return e?this.run(async()=>{await t(this.api.deleteOrg(e)),this._selectedId.set(null),await this.init(),this._notice.set(this.i18n.t("iam.orgDeleted"))}):!1}async invite(e,i){let s=this._selectedId();return s?this.run(async()=>{await t(this.api.invite(s,e,i)),this._invites.set(await t(this.api.invitations(s))),this._notice.set(this.i18n.t("iam.inviteSent",{email:e}))}):!1}async changeRole(e,i){let s=this._selectedId();return s?this.run(async()=>{await t(this.api.changeMemberRole(s,e,i)),this._members.set(await t(this.api.members(s))),this._notice.set(this.i18n.t("iam.roleUpdated"))}):!1}async removeMember(e){let i=this._selectedId();return i?this.run(async()=>{await t(this.api.removeMember(i,e)),this._members.set(await t(this.api.members(i))),this._notice.set(this.i18n.t("iam.memberRemoved"))}):!1}async leave(){let e=this._selectedId();return e?this.run(async()=>{await t(this.api.leaveOrg(e)),this._selectedId.set(null),await this.init(),this._notice.set(this.i18n.t("iam.leftTeam"))}):!1}async revokeInvite(e){let i=this._selectedId();return i?this.run(async()=>{await t(this.api.revokeInvitation(i,e)),this._invites.set(await t(this.api.invitations(i))),this._notice.set(this.i18n.t("iam.inviteRevoked"))}):!1}async acceptIncoming(e){return this.run(async()=>{let i=await t(this.api.acceptInvitation(e));await this.init(),await this.select(i.id),this._notice.set(this.i18n.t("iam.joinedOrg",{name:i.name}))})}clearNotice(){this._notice.set(null)}async run(e){this._busy.set(!0),this._error.set(null),this._notice.set(null);try{return await e(),!0}catch(i){return this._error.set(this.msg(i,this.i18n.t("iam.actionFailed"))),!1}finally{this._busy.set(!1)}}msg(e,i){let a=e?.error?.message;return Array.isArray(a)?a.join(" "):typeof a=="string"&&a&&a!=="EMAIL_NOT_VERIFIED"?a:i}static \u0275fac=function(i){return new(i||l)};static \u0275prov=d({token:l,factory:l.\u0275fac,providedIn:"root"})};export{g as a};
