import { _ as _package } from "./package-IsiXrVNz.js";
import { p as prisma, u as symmetricEncrypt, l as logger } from "./index-Cw1zbw54.js";
import { g as getInstalledAppPath } from "./getInstalledAppPath-6P3H-VDB.js";
import { B as BaseCalendarService } from "./CalendarService-0aXsS_3r.js";
class CalDavCalendarService extends 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 prisma.user.findFirstOrThrow({
      where: {
        id: req.session?.user?.id
      },
      select: {
        id: true,
        email: true
      }
    });
    const data = {
      type: "caldav_calendar",
      key: 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 prisma.credential.create({
        data
      });
    } catch (e) {
      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({ 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.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"
};
export {
  index as api,
  index$1 as lib,
  metadata
};
