"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index = require("./email-manager-w-Fd9mb6.cjs");
const crypto = require("crypto");
const otplib = require("otplib");
const Sentry = require("@sentry/nextjs");
const watchlist_repository = require("./watchlist.repository-vLhXWRP3.cjs");
const index$1 = require("./orgDomains-W_rFTMBa.cjs");
const server = require("@trpc/server");
const utils = require("./utils-RvZGPQkR.cjs");
const core = require("@otplib/core");
const pluginCrypto = require("@otplib/plugin-crypto");
require("@otplib/plugin-thirty-two");
function presenter(watchlistedEmail) {
  return Sentry.startSpan({ name: "checkIfEmailInWatchlist Presenter", op: "serialize" }, () => {
    return !!watchlistedEmail;
  });
}
async function checkIfEmailIsBlockedInWatchlistController(email) {
  return await Sentry.startSpan({ name: "checkIfEmailInWatchlist Controller" }, async () => {
    const lowercasedEmail = email.toLowerCase();
    const watchlistRepository = new watchlist_repository.WatchlistRepository();
    const watchlistedEmail = await watchlistRepository.getBlockedEmailInWatchlist(lowercasedEmail);
    return presenter(watchlistedEmail);
  });
}
const log = index$1.logger.getSubLogger({ prefix: [`[[Auth] `] });
const sendEmailVerificationByCode = async ({
  email,
  language,
  username,
  isVerifyingEmail
}) => {
  if (await checkIfEmailIsBlockedInWatchlistController(email)) {
    log.warn("Email is blocked - not sending verification email", email);
    return { ok: false, skipped: false };
  }
  const translation = await index$1.getTranslation(language ?? "en", "common");
  const secret = crypto.createHash("md5").update(email + process.env.CALENDSO_ENCRYPTION_KEY).digest("hex");
  otplib.totp.options = { step: 900 };
  const code = otplib.totp.generate(secret);
  await index.sendEmailVerificationCode({
    language: translation,
    verificationEmailCode: code,
    user: {
      email,
      name: username
    },
    isVerifyingEmail
  });
  return { ok: true, skipped: false };
};
const totpRawCheck = (token, secret, opts = {}) => {
  const { window = [1, 0], ...rest } = opts;
  const authenticator = new core.TOTP({
    createDigest: pluginCrypto.createDigest,
    window,
    ...rest
  });
  return authenticator.check(token, secret);
};
const verifyEmailCodeHandler = async ({ ctx, input }) => {
  const { code, email, teamId } = input;
  const { id } = ctx.user;
  if (!code || !email)
    throw new server.TRPCError({ code: "BAD_REQUEST" });
  const secret = crypto.createHash("md5").update(email + process.env.CALENDSO_ENCRYPTION_KEY).digest("hex");
  const isValidToken = totpRawCheck(code, secret, { step: 900 });
  if (!isValidToken)
    throw new server.TRPCError({ code: "BAD_REQUEST", message: "invalid_code" });
  await index$1.prisma.verifiedEmail.create({
    data: {
      email,
      userId: id,
      teamId
    }
  });
  return isValidToken;
};
exports.AttendeeCancelledEmail = index.AttendeeCancelledEmail;
exports.AttendeeDeclinedEmail = index.AttendeeDeclinedEmail;
exports.AttendeeRequestEmail = index.AttendeeRequestEmail;
exports.AttendeeRescheduledEmail = index.AttendeeRescheduledEmail;
exports.AttendeeScheduledEmail = index.AttendeeScheduledEmail;
exports.AttendeeUpdatedEmail = index.AttendeeUpdatedEmail;
exports.AttendeeVerifyEmail = index.AttendeeVerifyEmail;
exports.OrganizerCancelledEmail = index.OrganizerCancelledEmail;
exports.OrganizerReassignedEmail = index.OrganizerReassignedEmail;
exports.OrganizerRequestEmail = index.OrganizerRequestEmail;
exports.OrganizerRescheduledEmail = index.OrganizerRescheduledEmail;
exports.OrganizerScheduledEmail = index.OrganizerScheduledEmail;
exports.sendSignupToOrganizationEmail = utils.sendSignupToOrganizationEmail;
exports.sendEmailVerificationByCode = sendEmailVerificationByCode;
exports.verifyEmailCodeHandler = verifyEmailCodeHandler;
