import { x as sendEmailVerificationCode } from "./email-manager-mHSBIi6g.js";
import { ai, ah, ap, am, af, an, ae, aj, ak, ao, al, ag } from "./email-manager-mHSBIi6g.js";
import { createHash } from "crypto";
import { totp } from "otplib";
import { startSpan } from "@sentry/nextjs";
import { W as WatchlistRepository } from "./watchlist.repository-5NbpVXql.js";
import { l as logger, j as getTranslation, p as prisma } from "./orgDomains-eTg-cjth.js";
import { TRPCError } from "@trpc/server";
import { s } from "./utils-zqJxPQSV.js";
import { TOTP } from "@otplib/core";
import { createDigest } from "@otplib/plugin-crypto";
import "@otplib/plugin-thirty-two";
function presenter(watchlistedEmail) {
  return startSpan({ name: "checkIfEmailInWatchlist Presenter", op: "serialize" }, () => {
    return !!watchlistedEmail;
  });
}
async function checkIfEmailIsBlockedInWatchlistController(email) {
  return await startSpan({ name: "checkIfEmailInWatchlist Controller" }, async () => {
    const lowercasedEmail = email.toLowerCase();
    const watchlistRepository = new WatchlistRepository();
    const watchlistedEmail = await watchlistRepository.getBlockedEmailInWatchlist(lowercasedEmail);
    return presenter(watchlistedEmail);
  });
}
const log = 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 getTranslation(language ?? "en", "common");
  const secret = createHash("md5").update(email + process.env.CALENDSO_ENCRYPTION_KEY).digest("hex");
  totp.options = { step: 900 };
  const code = totp.generate(secret);
  await 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 TOTP({
    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 TRPCError({ code: "BAD_REQUEST" });
  const secret = createHash("md5").update(email + process.env.CALENDSO_ENCRYPTION_KEY).digest("hex");
  const isValidToken = totpRawCheck(code, secret, { step: 900 });
  if (!isValidToken)
    throw new TRPCError({ code: "BAD_REQUEST", message: "invalid_code" });
  await prisma.verifiedEmail.create({
    data: {
      email,
      userId: id,
      teamId
    }
  });
  return isValidToken;
};
export {
  ai as AttendeeCancelledEmail,
  ah as AttendeeDeclinedEmail,
  ap as AttendeeRequestEmail,
  am as AttendeeRescheduledEmail,
  af as AttendeeScheduledEmail,
  an as AttendeeUpdatedEmail,
  ae as AttendeeVerifyEmail,
  aj as OrganizerCancelledEmail,
  ak as OrganizerReassignedEmail,
  ao as OrganizerRequestEmail,
  al as OrganizerRescheduledEmail,
  ag as OrganizerScheduledEmail,
  sendEmailVerificationByCode,
  s as sendSignupToOrganizationEmail,
  verifyEmailCodeHandler
};
