"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const uuid = require("uuid");
const getAppKeysFromSlug = require("./getAppKeysFromSlug-RzLz5H5D.cjs");
const index$2 = require("./email-manager-398UKAw1.cjs");
const throwIfNotHaveAdminAccessToTeam = require("./throwIfNotHaveAdminAccessToTeam-4XNQZG2Q.cjs");
const index$3 = require("./index-3NyJcp7J.cjs");
const getInstalledAppPath = require("./getInstalledAppPath-3orMA3sa.cjs");
const JitsiVideoApiAdapter = () => {
  return {
    getAvailability: () => {
      return Promise.resolve([]);
    },
    createMeeting: async (eventData) => {
      const appKeys = await getAppKeysFromSlug.getAppKeysFromSlug(index$2.metadata$2.slug);
      const meetingPattern = appKeys.jitsiPathPattern || "{uuid}";
      const hostUrl = appKeys.jitsiHost || "https://meet.jit.si/cal";
      const meetingID = meetingPattern.replaceAll("{uuid}", uuid.v4()).replaceAll("{Title}", eventData.title).replaceAll("{Event Type Title}", eventData.type).replaceAll("{Scheduler}", eventData.attendees.map((a) => a.name).join("-")).replaceAll("{Organizer}", eventData.organizer.name).replaceAll("{Location}", eventData.location || "").replaceAll("{Team}", eventData.team?.name || "").replaceAll(" ", "-");
      return Promise.resolve({
        type: index$2.metadata$2.type,
        id: meetingID,
        password: "",
        url: `${hostUrl}/${encodeURIComponent(meetingID)}`
      });
    },
    deleteMeeting: async () => {
      Promise.resolve();
    },
    updateMeeting: (bookingRef) => {
      return Promise.resolve({
        type: "jitsi_video",
        id: bookingRef.meetingId,
        password: bookingRef.meetingPassword,
        url: bookingRef.meetingUrl
      });
    }
  };
};
const JitsiVideoApiAdapter$1 = JitsiVideoApiAdapter;
const index$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  VideoApiAdapter: JitsiVideoApiAdapter$1
}, Symbol.toStringTag, { value: "Module" }));
async function handler(req, res) {
  if (!req.session?.user?.id) {
    return res.status(401).json({ message: "You must be logged in to do this" });
  }
  const { teamId, returnTo } = req.query;
  await throwIfNotHaveAdminAccessToTeam.throwIfNotHaveAdminAccessToTeam({ teamId: Number(teamId) ?? null, userId: req.session.user.id });
  const installForObject = teamId ? { teamId: Number(teamId) } : { userId: req.session.user.id };
  const appType = "jitsi_video";
  try {
    const alreadyInstalled = await index$3.prisma.credential.findFirst({
      where: {
        type: appType,
        ...installForObject
      }
    });
    if (alreadyInstalled) {
      throw new Error("Already installed");
    }
    const installation = await index$3.prisma.credential.create({
      data: {
        type: appType,
        key: {},
        ...installForObject,
        appId: "jitsi"
      }
    });
    if (!installation) {
      throw new Error("Unable to create user credential for jitsivideo");
    }
  } catch (error) {
    if (error instanceof Error) {
      return res.status(500).json({ message: error.message });
    }
    return res.status(500);
  }
  return res.status(200).json({ url: returnTo ?? getInstalledAppPath.getInstalledAppPath({ variant: "conferencing", slug: "jitsi" }) });
}
const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  add: handler
}, Symbol.toStringTag, { value: "Module" }));
exports.metadata = index$2.metadata$2;
exports.api = index;
exports.lib = index$1;
