'use strict';
import { Facebook, generateState } from 'arctic';
import { authEnvSettingKeys, authParam, facebookApi, oAuthTokenResponseFields } 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 { signUpByOAuth } from './standard-login.js';
import { getLoginUrl } from '../../../../fd-toolbox/routing/login-routers.js';
import { defaultOAuthProviders, defaultOAuthProvidersNames } from '../../../../fd-toolbox/auth/oauth-provider-types.js';
import { iconNames } from '../../../fd-component-toolbox/icon/Icon.js';
import { buildUrl } from '../../../../fd-toolbox/url/urls.js';
import { getStringEnvSetting } from '../../../../fd-toolbox/infra/env-functions.js';
import { wellKnownMetaProps } from '../../../../fd-toolbox-core/constants/meta-constants.js';
import { wellKnownUserProps } from '../../../../fd-toolbox/constants/common-user-fields.js';
import { commonLowerCaseWords } from '../../../../base-repo/constants/internal-common-strings.js';

var __defProp=Object['defineProperty'],__name=(_0x55ed1c,_0x5a1a3a)=>__defProp(_0x55ed1c,'name',{'value':_0x5a1a3a,'configurable':!![]});const loginParams={'fields':commonLowerCaseWords['fields'],'name':commonLowerCaseWords['name'],'picture':commonLowerCaseWords['picture']};async function onFacebookLogin(){const _0x1e99ea=getStringEnvSetting(authEnvSettingKeys['facebookClientId']),_0x45f30b=getStringEnvSetting(authEnvSettingKeys['facebookClientSecret']);if(!_0x1e99ea||!_0x45f30b)return void(0x1*-0x369+-0xf9*-0x11+0x1e*-0x70);const _0x53941a=new Facebook(_0x1e99ea,_0x45f30b,buildUrl(getLoginUrl(),[{'name':authParam,'value':defaultOAuthProviders['facebook']}])),_0x214c57=generateState(),_0x1dc2f3=await _0x53941a['createAuthorizationURL'](_0x214c57,{'scopes':['public_profile',wellKnownUserProps['email']]});return navigate(_0x1dc2f3['toString']());}__name(onFacebookLogin,'onFacebookLogin');async function facebookOAuth(_0x52779b,_0x2f1bd0){const _0x1be8c3=getStringEnvSetting(authEnvSettingKeys['facebookClientId']),_0x5bdc3c=getStringEnvSetting(authEnvSettingKeys['facebookClientSecret']);if(!_0x1be8c3||!_0x5bdc3c)return;const _0x180698=new Facebook(_0x1be8c3,_0x5bdc3c,buildUrl(getLoginUrl(),[{'name':authParam,'value':defaultOAuthProviders['facebook']}])),_0x1efc7b=await _0x180698['validateAuthorizationCode'](_0x52779b);navigate(window['location']['pathname'],[]);const _0x394947=new URL(facebookApi);_0x394947['searchParams']['set'](oAuthTokenResponseFields['token'],_0x1efc7b['accessToken']),_0x394947['searchParams']['set'](loginParams['fields'],[wellKnownMetaProps['id'],loginParams['name'],loginParams['picture'],wellKnownUserProps['email']]['join'](','));const _0x29187e=await apiBaseGet(_0x394947['toString']()),_0x52454e=_0x29187e['email']||'',[_0x5a12da,_0x4b47b4]=_0x29187e['name']['split']('\x20');await signUpByOAuth(_0x2f1bd0,_0x29187e['id'],defaultOAuthProviders['facebook'],_0x52454e,_0x5a12da,_0x4b47b4);}__name(facebookOAuth,'facebookOAuth');function getFacebookOAuthConfig(){const _0x3ad502={'buttonText':defaultOAuthProvidersNames['facebook'],'oAuthProvider':defaultOAuthProviders['facebook'],'onLogin':onFacebookLogin,'handleOAuth':facebookOAuth,'iconName':iconNames['facebookLogo']};return _0x3ad502;}__name(getFacebookOAuthConfig,'getFacebookOAuthConfig');

export { facebookOAuth, getFacebookOAuthConfig, onFacebookLogin };
