import{k as G}from"./chunk-7NZWIHPJ.js";import{c as S}from"./chunk-R7INA2TP.js";import{a as b}from"./chunk-UQLLME4S.js";import{B as v,D as y,w as d,x as u}from"./chunk-N445P7BV.js";import{A as f,Y as l,ea as g,ja as h,p as m,u as p}from"./chunk-DZ62WUBN.js";var M=(()=>{class n{constructor(r,i,s,e){this.http=r,this.apiService=i,this.configService=s,this.authService=e,this.getOrganizationPrincipleNameGoogle=l(o=>{let t=o?.body?.access_token;return t?f({organization:this.apiService.GoogleGetTenantIdFromGraph(t),me:this.apiService.getUserInfoFromGoogle(t)}).pipe(p(({organization:c,me:a})=>({tenantId:c?.body?.id,userPrincipalName:a?.body?.email}))):m({error_description:o?.body?.error_description})}),this.getOrganizationPrincipleName=l(o=>{let t=o?.body?.access_token;return t?f({organization:this.MicrosoftGetTenantIdFromGraph(t),me:this.MicrosoftGetPrincipleNameFromGraph(t)}).pipe(p(({organization:c,me:a})=>({tenantId:c?.body?.value?.[0]?.id,userPrincipalName:a?.body?.userPrincipalName}))):m({error_description:o?.body?.error_description})})}MicrosoftConsent(r,i,s,e){return this.apiService.getAccessRefresh(r,i,s,e).pipe(e==="Microsoft"?this.getOrganizationPrincipleName:this.getOrganizationPrincipleNameGoogle)}MicrosoftConsentForPrinciple(r,i,s,e){return this.apiService.getAccessRefresh(r,i,s,e).pipe(l(o=>o?.body?.access_token?e==="Microsoft"?this.MicrosoftGetPrincipleNameFromGraph(o?.body?.access_token).pipe(p(t=>t?.body?.userPrincipalName)):this.apiService.getUserInfoFromGoogle(o?.body?.access_token).pipe(p(t=>t?.body?.email)):m(new v({body:o?.body,status:403,statusText:"Forbidden"}))))}MicrosoftGetAccessRefresh(r,i,s,e,o="authorization_code",t="https://graph.microsoft.com/"){let c=new d({"Content-Type":"application/x-www-form-urlencoded","Cache-Control":"no-cache",Pragma:"no-cache"}),a=new u().set("client_id",i).set("redirect_uri",e).set("client_secret",s).set("code",r).set("grant_type",o).set("resource",t);return this.http.post("https://login.microsoftonline.com/common/oauth2/token",a.toString(),{headers:c,responseType:"json",observe:"response"})}MicrosoftGetTenantIdFromGraph(r){let s=`${this.configService.protocol}${this.configService.baseFolder}/paralite/cloud/${this.configService.cloudName}/proxy/stream?target=`+encodeURIComponent("https://graph.microsoft.com/v1.0/organization"),e=new d({Authorization:`Bearer ${r}`});return this.authService.Token&&(e=e.set("token",this.authService.Token)),this.http.get(s,{headers:e,responseType:"json",observe:"response"})}MicrosoftGetPrincipleNameFromGraph(r){let s=`${this.configService.protocol}${this.configService.baseFolder}/paralite/cloud/${this.configService.cloudName}/proxy/stream?target=`+encodeURIComponent("https://graph.microsoft.com/v1.0/me"),e=new d({Authorization:`Bearer ${r}`});return this.authService.Token&&(e=e.set("token",this.authService.Token)),this.http.get(s,{headers:e,responseType:"json",observe:"response"})}static{this.\u0275fac=function(i){return new(i||n)(h(y),h(G),h(b),h(S))}}static{this.\u0275prov=g({token:n,factory:n.\u0275fac,providedIn:"root"})}}return n})();export{M as a};
