"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const _package = require("./package-lfiOWXvq.cjs");
const index$2 = require("./orgDomains-W_rFTMBa.cjs");
const getInstalledAppPath = require("./getInstalledAppPath-oSgU9RsM.cjs");
const CalendarService = require("./CalendarService-F9oW7jb8.cjs");
class CalDavCalendarService extends CalendarService.BaseCalendarService {
  constructor(credential) {
    super(credential, "caldav_calendar");
  }
}
const index$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  CalendarService: CalDavCalendarService
}, Symbol.toStringTag, { value: "Module" }));
async function handler(req, res) {
  if (req.method === "POST") {
    const { username, password, url } = req.body;
    const user = await index$2.prisma.user.findFirstOrThrow({
      where: {
        id: req.session?.user?.id
      },
      select: {
        id: true,
        email: true
      }
    });
    const data = {
      type: "caldav_calendar",
      key: index$2.symmetricEncrypt(
        JSON.stringify({ username, password, url }),
        process.env.CALENDSO_ENCRYPTION_KEY || ""
      ),
      userId: user.id,
      teamId: null,
      appId: "caldav-calendar",
      invalid: false,
      delegationCredentialId: null
    };
    try {
      const dav = new CalDavCalendarService({
        id: 0,
        ...data,
        user: { email: user.email }
      });
      await dav?.listCalendars();
      await index$2.prisma.credential.create({
        data
      });
    } catch (e) {
      index$2.logger.error("Could not add this caldav account", e);
      if (e instanceof Error) {
        let message = e.message;
        if (e.message.indexOf("Invalid credentials") > -1 && url.indexOf("dav.php") > -1) {
          const parsedUrl = new URL(url);
          const adminUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}${parsedUrl.port ? `:${parsedUrl.port}` : ""}/admin/?/settings/standard/`;
          message = `Couldn't connect to caldav account, please verify WebDAV authentication type is set to "Basic"`;
          return res.status(500).json({ message, actionUrl: adminUrl });
        }
      }
      return res.status(500).json({ message: "Could not add this caldav account" });
    }
    return res.status(200).json({ url: getInstalledAppPath.getInstalledAppPath({ variant: "calendar", slug: "caldav-calendar" }) });
  }
  if (req.method === "GET") {
    return res.status(200).json({ url: "/apps/caldav-calendar/setup" });
  }
}
const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  add: handler
}, Symbol.toStringTag, { value: "Module" }));
const metadata = {
  name: "CalDav (Beta)",
  description: _package._package.description,
  installed: true,
  type: "caldav_calendar",
  title: "CalDav (Beta)",
  variant: "calendar",
  category: "calendar",
  categories: ["calendar"],
  logo: "icon.svg",
  publisher: "Cal.com",
  slug: "caldav-calendar",
  url: "https://cal.com/",
  email: "help@cal.com",
  dirName: "caldavcalendar"
};
exports.api = index;
exports.lib = index$1;
exports.metadata = metadata;
