import { SoapFaultDetails } from "ews-javascript-api";
import { z } from "zod";
import { _ as emailSchema, Z as symmetricEncrypt, p as prisma, l as logger } from "./user--mpkKOwW.js";
import { d as defaultResponder } from "./defaultResponder-GZldAmhY.js";
import { c as checkSession } from "./auth-_2QcUxTs.js";
import { E as ExchangeAuthentication, a as ExchangeVersion, b as ExchangeCalendarService } from "./index-CYGLOFxB.js";
const formSchema = z.object({
  url: z.string().url(),
  username: emailSchema,
  password: z.string(),
  authenticationMethod: z.number().default(ExchangeAuthentication.STANDARD),
  exchangeVersion: z.number().default(ExchangeVersion.Exchange2016),
  useCompression: z.boolean().default(false)
}).strict();
async function getHandler(req, res) {
  const session = checkSession(req);
  const body = formSchema.parse(req.body);
  const encrypted = symmetricEncrypt(JSON.stringify(body), process.env.CALENDSO_ENCRYPTION_KEY || "");
  const data = {
    type: "exchange_calendar",
    key: encrypted,
    userId: session.user?.id,
    teamId: null,
    appId: "exchange",
    invalid: false,
    delegationCredentialId: null
  };
  try {
    const service = new ExchangeCalendarService({ id: 0, user: { email: session.user.email || "" }, ...data });
    await service?.listCalendars();
    await prisma.credential.create({ data });
  } catch (reason) {
    logger.info(reason);
    if (reason instanceof SoapFaultDetails && reason.message != "") {
      return res.status(500).json({ message: reason.message });
    }
    return res.status(500).json({ message: "Could not add this exchange account" });
  }
  return res.status(200).json({ url: "/apps/installed" });
}
const _postAdd = defaultResponder(getHandler);
export {
  _postAdd as default,
  getHandler
};
