"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const ewsJavascriptApi = require("ews-javascript-api");
const z = require("zod");
const index = require("./orgDomains-W_rFTMBa.cjs");
const defaultResponder = require("./defaultResponder-pIXL4EeF.cjs");
const auth = require("./auth--eKdiWSi.cjs");
const index$1 = require("./index-UKoiiyY-.cjs");
const formSchema = z.z.object({
  url: z.z.string().url(),
  username: index.emailSchema,
  password: z.z.string(),
  authenticationMethod: z.z.number().default(index$1.ExchangeAuthentication.STANDARD),
  exchangeVersion: z.z.number().default(index$1.ExchangeVersion.Exchange2016),
  useCompression: z.z.boolean().default(false)
}).strict();
async function getHandler(req, res) {
  const session = auth.checkSession(req);
  const body = formSchema.parse(req.body);
  const encrypted = index.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 index$1.ExchangeCalendarService({ id: 0, user: { email: session.user.email || "" }, ...data });
    await service?.listCalendars();
    await index.prisma.credential.create({ data });
  } catch (reason) {
    index.logger.info(reason);
    if (reason instanceof ewsJavascriptApi.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.defaultResponder(getHandler);
exports.default = _postAdd;
exports.getHandler = getHandler;
