'use strict';
import { apiUnauthorizedError } from '../../../fd-toolbox/server/web/errors.js';
import { getUserByEmailAndPassword, getUserByOAuth } from '../users/user-db.js';
import { createError } from '../../../fd-toolbox/errors/errors.js';
import { metaErrorMessages } from '../../meta/constants/meta-error-messages-constants.js';
import { createSessionForUser } from '../users/auth.js';
import { apiOkResponse } from '../../meta/server/web/responses.js';
import { metaEnvSettingKeys } from '../../meta/infra/meta-env-setting-keys.js';
import { getBooleanEnvSetting } from '../../../fd-toolbox/infra/env-functions.js';
import { parseJsonBodyFromRequest } from '../../../fd-toolbox/lib/utils.js';
import { serializeCookie } from '../../../fd-toolbox/server/auth/cookies.js';
import { httpHeaders } from '../../../fd-toolbox/server/constants/workspace-constants.js';

var __defProp=Object['defineProperty'],__name=(_0x374465,_0x1e8635)=>__defProp(_0x374465,'name',{'value':_0x1e8635,'configurable':!![]});const invalidDataMessage='Incorrect\x20email\x20or\x20password';async function signInApi(_0x226788){const _0x12556d=await parseJsonBodyFromRequest(_0x226788);if(!_0x12556d)throw apiUnauthorizedError(invalidDataMessage);let _0x4446b7;if(_0x12556d['email']&&_0x12556d['password'])_0x4446b7=await getUserByEmailAndPassword(_0x12556d['email']['toLowerCase'](),_0x12556d['password']);else {if(_0x12556d['oauthKey']&&_0x12556d['oauthProvider'])_0x4446b7=await getUserByOAuth(_0x12556d['oauthKey'],_0x12556d['oauthProvider']);else throw createError(metaErrorMessages['missingAuthParam']);}if(!_0x4446b7)throw apiUnauthorizedError(invalidDataMessage);const _0x2ba270=await createSessionForUser(_0x4446b7['id']),_0x5e936c={'user':_0x4446b7};return getBooleanEnvSetting(metaEnvSettingKeys['storeSessionInHeader'])&&(_0x5e936c['token']=_0x2ba270['value']),apiOkResponse(_0x5e936c,{[httpHeaders['setCookie']]:serializeCookie(_0x2ba270)});}__name(signInApi,'signInApi');

export { signInApi };
