'use strict';
import { verify, hash } from '@node-rs/argon2';
import { getNewUuid } from '../../../fd-toolbox/uuids.js';
import { randomBytes } from 'crypto';
import { workspaceConstants } from '../../../fd-toolbox/server/constants/workspace-constants.js';
import { publicImages } from '../../../fd-toolbox/constants/public-files.js';
import { emptyUuid } from '../../../fd-toolbox/constants/constants.js';
import { fileExtensionsWithoutDot, stringEncodingNames } from '../../../base-repo/app-constants/internal-system-constants.js';
import { createUserResource } from './user-db.js';
import { getUserById } from '../../fd-app-toolbox/server/users/user-db.js';
import { crudCreate } from '../../meta/server/basic-crud/cruds.js';
import { getCrudUpdateConfig } from '../../meta/server/meta-core/crud/crud-update-config.js';
import { resourceNames } from '../../../fd-toolbox/resources/resource-names.js';
import { isBeforeNow, getNow } from '../../../fd-toolbox/time/date-time.js';
import { getOAuthProviderByCurrentUser, updateOAuthProviderToken } from '../auth/oauth-service-db.js';
import { createUserSetting } from './user-settings/user-setting-db.js';
import { createBaseResourceProperties } from '../utils/base-resource-properties.js';
import { workspaceStatuses, workspaceCategories } from '../../common/dev/dev-enums.js';
import { createSession, createSessionCookie } from '../../meta/server/auth/fd-sessions.js';
import { deleteUserWorkspaceInvite } from '../workspace/workspace-invite/workspace-user-invites.js';
import { first } from '../../../fd-toolbox/server/collections/single-funcs.js';

var __defProp=Object['defineProperty'],__name=(_0x37a016,_0x393681)=>__defProp(_0x37a016,'name',{'value':_0x393681,'configurable':!![]});async function registerUser(_0x4bcbbb,_0x2f4183,_0x2855b7,_0x67f1b8,_0x307854,_0x14104c,_0x3013ef,_0x4a0a35,_0x447b09,_0x1cf041){let _0x403667=await getWorkspaceFromInviteOrCreateWorkspace(_0x4bcbbb,_0x2855b7);const _0x46b988=_0x2f4183?await hashPassword(_0x2f4183):void(-0x593*-0x1+0x1*-0x1fbb+0x1*0x1a28),_0x2f9995=getNewUuid(),_0x16bf41=await createUserResource({'id':_0x2f9995,'userId':_0x2f9995,'passwordHash':_0x46b988,'isDisabled':![],'email':_0x4bcbbb,'securityStamp':randomBytes(0x51e+-0xc83*-0x1+-0x1181)['toString'](stringEncodingNames['hexEncoding']),'imageUrl':'/'+publicImages['user']+'/'+emptyUuid+'.'+fileExtensionsWithoutDot['webp'],'firstName':_0x307854,'lastName':_0x14104c,'homeWorkspaceId':_0x403667,'currentWorkspaceId':_0x403667,..._0x447b09??{}});if(!_0x16bf41)return void(-0x13c6+0x309*-0xc+-0x1*-0x3832);await createSettingForUser(_0x2f9995,_0x307854,_0x14104c);if(_0x3013ef&&_0x4a0a35){const _0xd54ca3={'id':getNewUuid(),'userId':_0x2f9995,'authKey':_0x3013ef,'authType':_0x4a0a35,..._0x1cf041?_0x447b09:{}},_0x1dfc79={...await getCrudUpdateConfig(_0xd54ca3,resourceNames['oAuthProvider']),'throwOnNonSuccess':!![]};await crudCreate(_0x1dfc79);}return _0x67f1b8&&await deleteUserWorkspaceInvite(_0x67f1b8),_0x16bf41;}__name(registerUser,'registerUser');async function createSessionForUser(_0x5d4fa5){let _0xd97b38=await getUserById(_0x5d4fa5);const _0x2c283e=await createSession(_0x5d4fa5,{'homeWorkspaceId':_0xd97b38['homeWorkspaceId']??workspaceConstants['defaultWorkspaceId'],'currentWorkspaceId':_0xd97b38['currentWorkspaceId']??workspaceConstants['defaultWorkspaceId']});return createSessionCookie(_0x2c283e['id']);}__name(createSessionForUser,'createSessionForUser');async function checkValidPassword(_0x471648,_0x170489){if(_0x170489)return verify(_0x170489,_0x471648,{'memoryCost':0x4c00,'timeCost':0x2,'outputLen':0x20,'parallelism':0x1});return ![];}__name(checkValidPassword,'checkValidPassword');async function refreshAccessTokenIfNeeded(_0x2c6e9b,_0x326901,_0x213f17){const _0x35980c=await getOAuthProviderByCurrentUser(_0x326901);if(!_0x35980c?.['accessApiTokenExpiresAt'])return void(-0xf*-0x26f+0x2051+-0x17*0x2fe);if(_0x213f17||isBeforeNow(_0x35980c['accessApiTokenExpiresAt'])){const _0x2cd67f=await _0x2c6e9b(_0x35980c);return updateOAuthProviderToken(_0x35980c,_0x2cd67f);}return void(-0x7*0x6d+-0xd4*0x2+0x4a3);}__name(refreshAccessTokenIfNeeded,'refreshAccessTokenIfNeeded');async function initializeAccessToken(_0x2cfbe4,_0x300eb3){const _0x474192=await getOAuthProviderByCurrentUser(_0x300eb3);if(!_0x474192)return void(-0x1a*-0xeb+-0x1a5*0x13+0x761);const _0x39cfa3=await _0x2cfbe4(_0x474192);return updateOAuthProviderToken(_0x474192,_0x39cfa3);}__name(initializeAccessToken,'initializeAccessToken');async function registerTestUserWithCustomWorkspaceId(_0x699868,_0x1266c0,_0x5abb4c,_0x2eb8cf,_0x32a660,_0x35c4d6,_0x2fc08a,_0x58371d,_0x19446c){const _0x3e2b7c=_0x1266c0?await hashPassword(_0x1266c0):void(0x2243+0x1605+-0x3848),_0x49e5b0=getNewUuid(),_0xb567ca=await createUserResource({'id':_0x49e5b0,'userId':_0x49e5b0,'passwordHash':_0x3e2b7c,'isDisabled':![],'email':_0x699868,'securityStamp':randomBytes(-0x192d+0x5e*0x59+-0x761*0x1)['toString'](stringEncodingNames['hexEncoding']),'imageUrl':'/'+publicImages['user']+'/'+emptyUuid+'.'+fileExtensionsWithoutDot['webp'],'firstName':_0x2eb8cf,'lastName':_0x32a660,'homeWorkspaceId':workspaceConstants['defaultWorkspaceId'],'currentWorkspaceId':_0x5abb4c??workspaceConstants['defaultWorkspaceId'],..._0x58371d??{}});if(!_0xb567ca)return void(-0x28e+-0x1*-0x6b0+-0x422);await createSettingForUser(_0x49e5b0,_0x2eb8cf,_0x32a660);if(_0x35c4d6&&_0x2fc08a){const _0x4a8446={'id':getNewUuid(),'userId':_0x49e5b0,'authKey':_0x35c4d6,'authType':_0x2fc08a,..._0x19446c?_0x58371d:{}},_0x41d2fe={...await getCrudUpdateConfig(_0x4a8446,resourceNames['oAuthProvider']),'throwOnNonSuccess':!![]};await crudCreate(_0x41d2fe);}return _0xb567ca;}__name(registerTestUserWithCustomWorkspaceId,'registerTestUserWithCustomWorkspaceId');async function getWorkspaceFromInviteOrCreateWorkspace(_0x5b6c0b,_0x2ac2d2){let _0x4fcb88;if(_0x2ac2d2?.['workspaceId'])_0x4fcb88=_0x2ac2d2['workspaceId'];else {const _0x17c91a=await createWorkspaceAndWorkspaceSetting(_0x5b6c0b);_0x4fcb88=_0x17c91a['id'];}return _0x4fcb88;}__name(getWorkspaceFromInviteOrCreateWorkspace,'getWorkspaceFromInviteOrCreateWorkspace');async function createWorkspaceAndWorkspaceSetting(_0x12f06d){const _0x583164={'id':getNewUuid(),'name':first(_0x12f06d['split']('@'))},{result:_0x25bc3f}=await crudCreate(await getCrudUpdateConfig(_0x583164,resourceNames['workspace'])),_0x35f3ce={...createBaseResourceProperties(),'name':_0x25bc3f['name'],'workspaceId':_0x25bc3f['id'],'workspaceCategory':workspaceCategories['internal'],'status':workspaceStatuses['active'],'workspace':_0x25bc3f};return await crudCreate(await getCrudUpdateConfig(_0x35f3ce,resourceNames['workspaceSetting'])),_0x25bc3f;}__name(createWorkspaceAndWorkspaceSetting,'createWorkspaceAndWorkspaceSetting');async function hashPassword(_0x35165f){return hash(_0x35165f,{'memoryCost':0x4c00,'timeCost':0x2,'outputLen':0x20,'parallelism':0x1});}__name(hashPassword,'hashPassword');async function createSettingForUser(_0x429788,_0x5b7991,_0x46af11){const _0x2a6260={'id':getNewUuid(),'userId':_0x429788,'firstName':_0x5b7991,'lastName':_0x46af11,'createdDate':getNow(),'createdByUserId':_0x429788,'modifiedDate':getNow(),'modifiedByUserId':_0x429788};return createUserSetting(_0x2a6260);}__name(createSettingForUser,'createSettingForUser');

export { checkValidPassword, createSessionForUser, initializeAccessToken, refreshAccessTokenIfNeeded, registerTestUserWithCustomWorkspaceId, registerUser };
