import { y as defineEventHandler, b0 as useLogger, aE as readBody, v as createError, U as User, aY as setResponseStatus } from '../../../nitro/nitro.mjs';
import 'luxon';
import '@sequelize/core';
import 'html-to-text';
import 'zod';
import 'crypto';
import 'node:fs';
import 'sharp';
import 'path';
import 'node:stream';
import 'jsdom';
import 'dompurify';
import 'mime-types';
import 'fs';
import 'fs/promises';
import 'linkify-html';
import '@peertube/http-signature';
import 'jsonld';
import 'lodash.intersection';
import 'email-templates';
import 'node:http';
import 'node:https';
import 'node:crypto';
import 'node:events';
import 'node:buffer';
import 'node:path';
import 'vue';
import 'vue-router';
import 'dotenv';
import 'url';
import 'bcryptjs';
import 'slugify';
import 'umzug';
import 'consola';
import '@sequelize/core/decorators-legacy';
import 'util';
import 'mitt';
import 'ical.js';
import 'node:url';
import 'fast-xml-parser';

const recover_password_post = defineEventHandler(async (event) => {
  const log = useLogger("user/recover");
  const { recover_code, password } = await readBody(event);
  if (!recover_code || !password) {
    throw createError({ statusCode: 400, message: "Wrong password or recover code" });
  }
  const user = await User.findOne({ where: { recover_code, is_active: true } });
  if (!user) {
    log.error("User not found");
    throw createError({ statusCode: 400, message: "User not found" });
  }
  try {
    await user.update({ recover_code: "", password });
    setResponseStatus(event, 201);
  } catch {
    setResponseStatus(event, 400);
  }
});

export { recover_password_post as default };
