'use strict';
import { apiBadRequestError, apiMethodNotAllowedError } from '../../../fd-toolbox/server/web/errors.js';
import { authTypes } from '../../meta/auth/login.js';
import { apiOkResponse } from '../../meta/server/web/responses.js';
import { registerUser } from '../users/auth.js';
import { getUserByOAuth, getUserByEmail } from '../users/user-db.js';
import { parseJsonBodyFromRequest } from '../../../fd-toolbox/lib/utils.js';
import { checkUserWorkspaceInviteCode } from '../workspace/workspace-invite/workspace-user-invites.js';
import { userFriendlyWorkspaceInviteMessages } from '../../fd-app-toolbox/constants/user-friendly-workspace-invite-messages.js';

var __defProp=Object['defineProperty'],__name=(_0x4371eb,_0x197ba9)=>__defProp(_0x4371eb,'name',{'value':_0x197ba9,'configurable':!![]});const registrationFailedMessage='User\x20registration\x20failed',emailAlreadyUse='Email\x20address\x20is\x20already\x20in\x20use.\x20Please\x20try\x20a\x20different\x20email\x20or\x20log\x20in\x20to\x20your\x20existing\x20account.';async function signUpApi(_0x424b38){const _0x271347=await parseJsonBodyFromRequest(_0x424b38);if(!_0x271347)throw apiBadRequestError(registrationFailedMessage);let _0x2be87d,_0x50cf2e;if(_0x271347['authType']===authTypes['oauth'])_0x2be87d=await getUserByOAuth(_0x271347['oauthKey'],_0x271347['oauthProvider']);else _0x2be87d=await getUserByEmail(_0x271347['email']['toLowerCase']());if(_0x2be87d)throw apiMethodNotAllowedError(emailAlreadyUse);if(_0x271347['workspaceUserInviteId']){_0x50cf2e=await checkUserWorkspaceInviteCode(_0x271347['workspaceUserInviteId']);if(_0x50cf2e?.['isValid']===![])return apiOkResponse({'message':_0x50cf2e?.['userFriendlyErrorMessage']??userFriendlyWorkspaceInviteMessages['notValidInviteCode']});}_0x2be87d??=await registerUser(_0x271347['email']['toLowerCase'](),_0x271347['password'],_0x50cf2e,_0x271347['workspaceUserInviteId'],_0x271347['firstName'],_0x271347['lastName'],_0x271347['oauthKey'],_0x271347['oauthProvider']);if(!_0x2be87d)throw apiBadRequestError(registrationFailedMessage);return apiOkResponse({'user':_0x2be87d});}__name(signUpApi,'signUpApi');

export { signUpApi };
