import { d as defineEventHandler, u as useLogger, a as readBody, w as publicSettings, c as createError, U as User, Y as enqueueMail, X as notifyAdmins } from '../../../nitro/nitro.mjs';
import * as linkify from 'linkifyjs';
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 register_post = defineEventHandler(async (event) => {
  const log = useLogger("register");
  const user = await readBody(event);
  if (!publicSettings.allow_registration) {
    throw createError({ status: 404, message: "Not found" });
  }
  const n_users = await User.count();
  try {
    if (n_users === 0) {
      log.info("First registered user will be an admin");
      user.is_active = user.is_admin = true;
      user.role = "admin";
      const userInstance2 = await User.create(user);
      return userInstance2.get({ plain: true });
    }
    if (!linkify.test(user.email, "email")) {
      throw createError({ status: 400, statusMessage: "Invalid email" });
    }
    log.info("Register user", user.email);
    const userInstance = await User.create(user);
    log.info(`Sending registration email to ${user.email} and registration confirmation to admins`);
    enqueueMail(user.email, "register", { user, config: publicSettings });
    notifyAdmins("admin_register", { user, config: publicSettings });
    return userInstance;
  } catch (e) {
    log.error(String(e));
    throw createError({ status: 400, statusMessage: String(e) });
  }
});

export { register_post as default };
