"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const ewsJavascriptApi = require("ews-javascript-api");
const z = require("zod");
const index$1 = require("./index-QSf6WJj0.cjs");
const indexBrowser = require("./index-browser-RtwB1hqI.cjs");
const defaultResponder = require("./defaultResponder-ZNP4idbH.cjs");
const auth = require("./auth--eKdiWSi.cjs");
const index = require("./index-y2swSTpn.cjs");
const formSchema = z.z.object({
  url: z.z.string().url(),
  username: indexBrowser.emailSchema,
  password: z.z.string(),
  authenticationMethod: z.z.number().default(index.ExchangeAuthentication.STANDARD),
  exchangeVersion: z.z.number().default(index.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$1.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.ExchangeCalendarService({ id: 0, user: { email: session.user.email || "" }, ...data });
    await service?.listCalendars();
    await index$1.prisma.credential.create({ data });
  } catch (reason) {
    index$1.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;
