'use strict';
import { getLoginResult } from '../../../meta/auth/auth-handler.js';
import { authTypes } from '../../../meta/auth/login.js';
import { apiPost, apiGet, apiPut } from '../../../../fd-toolbox/api/api-client.js';
import { registerByOAuth } from '../auth-register.js';
import { rememberAuthToken } from '../../../../fd-toolbox/auth/tokens.js';
import { getAfterLoginRedirectUrl, removeAfterLoginRedirectUrl, setLoginState } from '../../../../fd-toolbox/auth/internal-login-states.js';
import { getNowUtc } from '../../../../fd-toolbox/dates/date-time.js';
import { routes } from '../../../../fd-toolbox/routing/routes.js';
import { apiPathNames } from '../../../../fd-toolbox/api/api-path-names.js';
import { removePermissions, setPermissions } from '../../../meta/auth/permissions/permission-checker.js';

var __defProp=Object['defineProperty'],__name=(_0x508590,_0x34a6d7)=>__defProp(_0x508590,'name',{'value':_0x34a6d7,'configurable':!![]});async function defaultLogin(_0x5c46cf,_0x3cf99f,_0x379af9){return login({'email':_0x3cf99f,'authType':authTypes['default'],'password':_0x379af9},_0x5c46cf);}__name(defaultLogin,'defaultLogin');async function loginByOAuth(_0x4936e5,_0x2cffd5,_0x3d9d3b,_0x5679a6){return login({'email':_0x4936e5,'oauthKey':_0x2cffd5,'oauthProvider':_0x3d9d3b,'authType':authTypes['oauth']},_0x5679a6);}__name(loginByOAuth,'loginByOAuth');async function signUpByOAuth(_0x4eaaac,_0x48bd89,_0x59e99d,_0x39dd09,_0x1943c1,_0x326971){return await registerByOAuth(_0x48bd89,_0x59e99d,_0x39dd09,_0x1943c1,_0x326971),loginByOAuth(_0x39dd09,_0x48bd89,_0x59e99d,_0x4eaaac);}__name(signUpByOAuth,'signUpByOAuth');async function login(_0x1ed60c,_0xf14ae0){const _0x544d9f=await apiPost(routes['login'],_0x1ed60c);return handleLoginResponse(_0x544d9f,_0xf14ae0);}__name(login,'login');async function onLoginFormPermissions(){const _0x1a3499=''+apiPathNames['userPermission'],_0x19949b=(await apiGet(_0x1a3499))['data'];_0x19949b&&Object['keys'](_0x19949b)['length']>0x2513+0x21a8+-0x46bb&&(removePermissions(),setPermissions(_0x19949b));}__name(onLoginFormPermissions,'onLoginFormPermissions');function redirectAfterLogin(_0x51d362){const _0x269b40=determineRedirectUrl();if(_0x269b40)_0x51d362['push'](_0x269b40);else _0x51d362['push'](routes['root']);}__name(redirectAfterLogin,'redirectAfterLogin');function determineRedirectUrl(){const _0x3a5e22=getAfterLoginRedirectUrl();if(_0x3a5e22)return removeAfterLoginRedirectUrl(),_0x3a5e22;return routes['root'];}__name(determineRedirectUrl,'determineRedirectUrl');async function handleLoginResponse(_0x8eb9ee,_0x1d4abb){const _0x455e59=getLoginResult(_0x8eb9ee);return _0x455e59['isSuccess']&&(_0x455e59['token']&&rememberAuthToken(_0x455e59['token']),await onLoginFormPermissions(),setLoginState({'loggedIn':!![],'userId':_0x455e59['user']['id'],'displayName':_0x455e59['user']?.['firstName']+'\x20'+_0x455e59['user']?.['lastName'],'email':_0x455e59['user']['email'],'imageUrl':_0x455e59['user']['imageUrl']}),await apiPut(routes['otherUser'],{'id':_0x455e59['user']['id'],'lastLoginDate':getNowUtc()}),redirectAfterLogin(_0x1d4abb)),_0x455e59;}__name(handleLoginResponse,'handleLoginResponse');

export { defaultLogin, login, loginByOAuth, onLoginFormPermissions, redirectAfterLogin, signUpByOAuth };
