'use strict';
import { MicrosoftEntraId, generateState, generateCodeVerifier } from 'arctic';
import { authEnvSettingKeys, entraId, microsoftApi, authParam } from '../../../../fd-toolbox/constants/internal-auth-constants.js';
import { navigate } from '../../../../fd-toolbox/redirect/redirect-functions.js';
import { apiBaseGet } from '../../../../fd-toolbox/api/base-api.js';
import { getLoginUrl } from '../../../../fd-toolbox/routing/login-routers.js';
import { normalizeEmail } from './normalize-email.js';
import { signUpByOAuth } from './standard-login.js';
import { setSessionStorage, getSessionStorage } from '../../../../fd-toolbox/auth/session-storage.js';
import { defaultOAuthProviders, defaultOAuthProvidersNames } from '../../../../fd-toolbox/auth/oauth-provider-types.js';
import { iconNames } from '../../../fd-component-toolbox/icon/Icon.js';
import { combineEndpointPath } from '../../../../fd-toolbox/http/url/urls.js';
import { getStringEnvSetting } from '../../../../fd-toolbox/infra/env-functions.js';
import { wellKnownUserProps } from '../../../../fd-toolbox/constants/common-user-fields.js';
import { codeVerifierString } from './google-login.js';
import { oauthCallbackQueryParams } from '../../../../base-repo/constants/internal-query-constants.js';

var __defProp=Object['defineProperty'],__name=(_0x49a504,_0x4741c3)=>__defProp(_0x49a504,'name',{'value':_0x4741c3,'configurable':!![]});function getBearerAccessToken(_0x4f5e4d){return 'Bearer\x20'+_0x4f5e4d;}__name(getBearerAccessToken,'getBearerAccessToken');async function onMicrosoftLogin(){const _0x4fa927=getStringEnvSetting(authEnvSettingKeys['microsoftClientId']);if(!_0x4fa927)return void(-0x1a75+-0x562*-0x4+0x4ed);const _0x180c47=new MicrosoftEntraId(entraId,_0x4fa927,'',combineEndpointPath(getLoginUrl(),defaultOAuthProviders['microsoft'])),_0x476136=generateState(),_0x43a82e=generateCodeVerifier(),_0x412ab5=await _0x180c47['createAuthorizationURL'](_0x476136,_0x43a82e,{'scopes':[wellKnownUserProps['profile'],wellKnownUserProps['email'],wellKnownUserProps['openid']]});return setSessionStorage(codeVerifierString,_0x43a82e),navigate(_0x412ab5['toString']());}__name(onMicrosoftLogin,'onMicrosoftLogin');async function microsoftOAuth(_0xbef47f,_0x5d7d7c){const _0x5a1abc=getStringEnvSetting(authEnvSettingKeys['microsoftClientId']);if(!_0x5a1abc)return;const _0x502dbb=new MicrosoftEntraId(entraId,_0x5a1abc,'',combineEndpointPath(getLoginUrl(),defaultOAuthProviders['microsoft'])),_0x3dbc24=getSessionStorage(codeVerifierString),_0x2a8e01=await _0x502dbb['validateAuthorizationCode'](_0xbef47f,_0x3dbc24),_0x36553b=await apiBaseGet(microsoftApi,new Headers({'Authorization':getBearerAccessToken(_0x2a8e01['accessToken'])})),_0x4f90c6=_0x36553b['mail']??normalizeEmail(_0x36553b['userPrincipalName']);await signUpByOAuth(_0x5d7d7c,_0x36553b['id'],defaultOAuthProviders['microsoft'],_0x4f90c6,_0x36553b['givenName'],_0x36553b['surname']);}__name(microsoftOAuth,'microsoftOAuth');function setMicrosoftAuthParams(_0xc2cde5){return [{'name':authParam,'value':defaultOAuthProviders['microsoft']},{'name':oauthCallbackQueryParams['code'],'value':_0xc2cde5??''}];}__name(setMicrosoftAuthParams,'setMicrosoftAuthParams');function getMicrosoftOAuthConfig(){const _0x84fae9={'buttonText':defaultOAuthProvidersNames['microsoft'],'oAuthProvider':defaultOAuthProviders['microsoft'],'onLogin':onMicrosoftLogin,'handleOAuth':microsoftOAuth,'iconName':iconNames['microsoftLogo']};return _0x84fae9;}__name(getMicrosoftOAuthConfig,'getMicrosoftOAuthConfig');

export { getBearerAccessToken, getMicrosoftOAuthConfig, microsoftOAuth, onMicrosoftLogin, setMicrosoftAuthParams };
