'use strict';
import { resourceNames } from '../../../fd-toolbox/resources/resource-names.js';
import { getUserId } from '../../meta/server/auth/users/current-user-id-provider.js';
import { crudActionNames } from '../../../fd-toolbox/server/constants/crud-actions-constants.js';
import { getNewUuid } from '../../../fd-toolbox/uuids.js';
import { publishMessage } from '../../meta/server/messaging/messages.js';
import { crudUpdate } from '../../meta/server/basic-crud/cruds.js';
import { toolboxUserConstants } from '../../meta/constants/toolbox-users.js';
import { operationStatusMessages } from '../../../fd-toolbox/server/constants/operation-status-messages.js';
import { getNow } from '../../../fd-toolbox/time/date-time.js';
import { workspaceConstants } from '../../../fd-toolbox/server/constants/workspace-constants.js';
import { createError } from '../../../fd-toolbox/errors/errors.js';
import { metaErrorMessages } from '../../meta/constants/meta-error-messages-constants.js';
import { hasPermission } from '../../meta/server/permission/permission-checker.js';
import { createUser } from './user-creation-service.js';
import { updateUser, generatePasswordResetToken, resetPassword } from './user-manager.js';
import { addUserToRoles } from './user-role.js';
import { createUserSetting, getUserSettingByUserId } from './user-settings/user-setting-db.js';
import { getUserByEmail } from './user-db.js';
import { getUserById } from '../../fd-app-toolbox/server/users/user-db.js';
import { parseUtcStringToDate } from '../../../fd-toolbox/dates/date-time.js';
import { crudActions } from '../../../fd-toolbox/enums/enums.js';

var __defProp=Object['defineProperty'],__name=(_0x687033,_0xab5624)=>__defProp(_0x687033,'name',{'value':_0xab5624,'configurable':!![]});async function createUserWithSettings(_0x40acc5){await checkUserPermission(crudActions['create']);const _0x11ae39={'email':_0x40acc5['email'],'password':_0x40acc5['password'],'userName':_0x40acc5['userName'],'firstName':_0x40acc5['firstName']??'','lastName':_0x40acc5['lastName']??'','userId':getNewUuid(),'homeWorkspaceId':workspaceConstants['defaultWorkspaceId'],'currentWorkspaceId':workspaceConstants['defaultWorkspaceId']};await createUser(_0x11ae39,getNewUuid());const _0x3a3e1c=await getUserByEmail(_0x11ae39['email']);if(!_0x3a3e1c)throw createError(metaErrorMessages['userNotFound']);const _0x4fea08=[resourceNames['user']];await addUserToRoles(_0x4fea08,_0x3a3e1c['id']);const _0x2c8576={'id':getNewUuid(),'userId':_0x3a3e1c['id'],'firstName':_0x40acc5['firstName'],'lastName':_0x40acc5['lastName'],'createdDate':getNow(),'createdByUserId':await getUserId(),'modifiedDate':getNow(),'modifiedByUserId':await getUserId()};createUserSetting(_0x2c8576);const _0x41acbe={'userId':_0x3a3e1c['id'],'userName':_0x40acc5['userName'],'roles':_0x4fea08,'firstName':_0x40acc5['firstName'],'lastName':_0x40acc5['lastName']};await publishMessage(_0x41acbe);}__name(createUserWithSettings,'createUserWithSettings');async function editUser(_0x35c896){await checkUserPermission(crudActions['update']);if(!_0x35c896['id'])throw createError(metaErrorMessages['noUserIdProvided']);const _0x376927=await getUserById(_0x35c896['id']);_0x376927['email']=_0x35c896['email'],_0x376927['firstName']=_0x35c896['firstName'],_0x376927['lastName']=_0x35c896['lastName'],_0x376927['lastLoginDate']=_0x35c896['lastLoginDate']?parseUtcStringToDate(_0x35c896['lastLoginDate']):void(0x4*-0x300+0x1045+0x1*-0x445),await updateUserSettingIfNeeded(_0x35c896,_0x376927);_0x376927['isDisabled']!==_0x35c896['isDisabled']&&(_0x376927['isDisabled']=_0x35c896['isDisabled'],await updateUser(_0x376927));let _0x4a32d1={'isSuccess':!![],'userFriendlyMessage':''};if(_0x35c896['password']){const _0x3183a3=generatePasswordResetToken(_0x376927),_0x254729=await resetPassword(_0x3183a3,_0x376927,_0x35c896['password']);handleIdentityResult(_0x4a32d1,_0x254729);}if(_0x4a32d1['isSuccess']){const _0x210173=await updateUser(_0x376927);handleIdentityResult(_0x4a32d1,_0x210173);}return _0x4a32d1;}__name(editUser,'editUser');async function checkUserPermission(_0x5c0039){if(!hasPermission(resourceNames['user'],_0x5c0039,await getUserId()))throw createError(metaErrorMessages['noPermissionForCrudAction']['replace']('{0}',crudActionNames['delete'])['replace']('{1}',resourceNames['user']));}__name(checkUserPermission,'checkUserPermission');async function updateUserSettingIfNeeded(_0x39f50c,_0x4903b7){const _0x3da881=await getUserSettingByUserId(_0x4903b7['id']);_0x39f50c['firstName']&&_0x3da881['firstName']!==_0x39f50c['firstName']&&(_0x3da881['firstName']=_0x39f50c['firstName']);_0x39f50c['lastName']&&_0x3da881['lastName']!==_0x39f50c['lastName']&&(_0x3da881['lastName']=_0x39f50c['lastName']);if(_0x39f50c['firstName']||_0x39f50c['lastName']){const _0x218c5c={'resource':_0x3da881,'userId':toolboxUserConstants['workerBotId'],'resourceName':resourceNames['userSetting'],'isCalledFromOutside':!![]};await crudUpdate(_0x218c5c);}}__name(updateUserSettingIfNeeded,'updateUserSettingIfNeeded');function handleIdentityResult(_0x234393,_0x5d01c2){_0x234393['isSuccess']=_0x5d01c2['isSuccess'],_0x234393['userFriendlyMessage']=_0x5d01c2['isSuccess']?operationStatusMessages['success']:_0x5d01c2['userFriendlyMessage'];}__name(handleIdentityResult,'handleIdentityResult');

export { checkUserPermission, createUserWithSettings, editUser };
