'use strict';
import { apiNoContentResponse } from '../../../meta/server/web/responses.js';
import { passwordMessages } from '../../../../fd-toolbox-core/constants/password-messages.js';
import { emails } from '../../../../fd-toolbox-core/constants/emails.js';
import { readFile } from 'node:fs/promises';
import { apiBadRequestError, apiNotFoundError } from '../../../../fd-toolbox/server/web/errors.js';
import { sendEmail } from '../../fd/fd-toolbox-web/mailing/fd-emails.js';
import { generatePasswordResetToken } from '../../users/user-manager.js';
import { parseJsonBodyFromRequest } from '../../../../fd-toolbox/lib/utils.js';
import { setTestData } from '../../test/data/test-data.js';
import { testDataKeys } from '../../test/data/test-constants.js';
import { resetPasswordQueryParams } from '../../../../base-repo/constants/internal-query-constants.js';
import { routes } from '../../../../fd-toolbox/routing/routes.js';
import { getUserByEmail } from '../../users/user-db.js';
import { getHtmlTemplatesPath } from '../../html/email.js';
import { wellKnownUserProps } from '../../../../fd-toolbox/constants/common-user-fields.js';
import { serverUrlPlaceholders } from '../../constants/url-placeholders.js';

var __defProp=Object['defineProperty'],__name=(_0x3db3b3,_0x4c670a)=>__defProp(_0x3db3b3,'name',{'value':_0x4c670a,'configurable':!![]});async function forgotpasswordPostRoute(_0x1e4984){const _0x4a323d=await parseJsonBodyFromRequest(_0x1e4984);if(!_0x4a323d['email'])throw apiBadRequestError(passwordMessages['emailRequired']);const _0x3d0b48=await getUserByEmail(_0x4a323d['email']);if(!_0x3d0b48)throw apiNotFoundError();const _0x598c31=generatePasswordResetToken(_0x3d0b48);await setTestData(testDataKeys['lastForgotPasswordToken'],_0x598c31);const _0x15e3b2=new URL(_0x1e4984['url']),_0x560ec4=_0x15e3b2['origin']+'/'+routes['resetPassword']+'?'+resetPasswordQueryParams['code']+'='+_0x598c31+'&'+wellKnownUserProps['email']+'='+_0x3d0b48['email'],_0x1aa1e5=getHtmlTemplatesPath('send-email-form.html'),_0xa15aea=await readFile(_0x1aa1e5,{'encoding':'utf-8'}),_0x362414=_0xa15aea['replace'](serverUrlPlaceholders['callbackUrlPlaceholder'],_0x560ec4),_0x3be0f8={'to':[_0x4a323d['email']],'from':emails['from']['replace']('{0}',_0x15e3b2['hostname']),'subject':emails['resetPasswordSubject'],'body':_0x362414};return await sendEmail(_0x3be0f8),apiNoContentResponse();}__name(forgotpasswordPostRoute,'forgotpasswordPostRoute');

export { forgotpasswordPostRoute };
