'use strict';
import { postBaseApi } from '../../../fd-toolbox/api/base-api.js';
import { forgetAuthToken, rememberAuthToken, getAuthToken } from '../../../fd-toolbox/auth/tokens.js';
import { removeLoginState, isLoggedIn, setLoginState } from '../../../fd-toolbox/auth/internal-login-states.js';
import { jwtDecode } from 'jwt-decode';
import { emptyUuid } from '../../../fd-toolbox/constants/constants.js';
import { apiGet } from '../../../fd-toolbox/api/api-client.js';
import { apiPathNames } from '../../../fd-toolbox/api/api-path-names.js';
import { routes } from '../../../fd-toolbox/routing/routes.js';
import { publicImages } from '../../../fd-toolbox/constants/public-files.js';
import { fileExtensionsWithoutDot } from '../../../base-repo/app-constants/internal-system-constants.js';
import { removePermissions } from './permissions/permission-checker.js';
import { createError } from '../../../fd-toolbox/errors/errors.js';
import { authErrorMessages } from '../../../fd-toolbox/constants/auth-error-messages.js';
import { folders } from '../../../base-repo/app-constants/project-paths.js';
import { wellKnownUserProps } from '../../../fd-toolbox/constants/common-user-fields.js';

var __defProp=Object['defineProperty'],__name=(_0x37e1b1,_0x301f12)=>__defProp(_0x37e1b1,'name',{'value':_0x301f12,'configurable':!![]});function loginWithoutToken(_0xb802da){return postBaseApi(apiPathNames['login'],getEncodedFormDataFromObject(getLoginRequestData(_0xb802da)));}__name(loginWithoutToken,'loginWithoutToken');async function loginWithAuthToken(_0x547e59){let _0x55469f=await postBaseApi(apiPathNames['token'],getEncodedFormDataFromObject(getAuthTokenRequestData(_0x547e59)));_0x55469f&&await checkAuthToken(!![],_0x55469f['access_token']);}__name(loginWithAuthToken,'loginWithAuthToken');function signOut(){return apiGet(routes['logout']);}__name(signOut,'signOut');async function logout(){removeLoginState(),forgetAuthToken(),removePermissions(),await signOut();}__name(logout,'logout');async function checkAuthToken(_0x39bf3f=!![],_0x549c2b){_0x549c2b?(_0x39bf3f&&rememberAuthToken(_0x549c2b),!isLoggedIn()&&setLoginState({'loggedIn':!![],'displayName':'','userId':emptyUuid,'email':'','imageUrl':'/'+publicImages['user']+'/'+emptyUuid+'.'+fileExtensionsWithoutDot['webp']}),await addUserToLoginState()):logout();}__name(checkAuthToken,'checkAuthToken');function getLoginRequestData(_0x5813fe){return {'Email':_0x5813fe['email'],'Password':_0x5813fe['password'],'ReturnUrl':_0x5813fe['returnUrl']};}__name(getLoginRequestData,'getLoginRequestData');function getAuthTokenRequestData(_0x28fc8a){return {'client_id':'ro.client','scope':folders['api'],'grant_type':wellKnownUserProps['password'],'Email':_0x28fc8a['email'],[wellKnownUserProps['password']]:_0x28fc8a['password']};}__name(getAuthTokenRequestData,'getAuthTokenRequestData');function getEncodedFormDataFromObject(_0x3df63b){const _0x39a708=new URLSearchParams();return Object['entries'](_0x3df63b)['forEach'](([_0x422f68,_0x48fadb])=>{_0x39a708['append'](_0x422f68,_0x48fadb);}),_0x39a708;}__name(getEncodedFormDataFromObject,'getEncodedFormDataFromObject');async function addUserToLoginState(){const _0x211f44={'loggedIn':![],'displayName':'','userId':emptyUuid,'imageUrl':'/'+publicImages['user']+'/'+emptyUuid+'.'+fileExtensionsWithoutDot['webp'],'email':''},_0x4e1cbf=getAuthToken();if(!_0x4e1cbf){setLoginState(_0x211f44);throw createError(authErrorMessages['tokenNotFound']);}const _0x15e842=jwtDecode(_0x4e1cbf);if(!_0x15e842?.['sub']){setLoginState(_0x211f44);throw createError(authErrorMessages['invalidToken']);}const _0x195645=await apiGet(apiPathNames['user'],_0x15e842['sub']);if(!_0x195645['data']){setLoginState(_0x211f44);throw createError(authErrorMessages['userNotFound']);}setLoginState({'loggedIn':!![],'displayName':_0x195645['data']?.['firstName']+'\x20'+_0x195645['data']?.['lastName'],'userId':_0x195645['data']?.['id'],'email':_0x195645['data']?.['email'],'imageUrl':_0x195645['data']?.['imageUrl']??''});}__name(addUserToLoginState,'addUserToLoginState');

export { loginWithAuthToken, loginWithoutToken, logout, signOut };
