import { v4 } from "uuid";
import { g as getAppKeysFromSlug } from "./getAppKeysFromSlug-CJxDAalk.js";
import { x as metadata } from "./CalEventParser-NbcDfXYU.js";
import { t as throwIfNotHaveAdminAccessToTeam } from "./throwIfNotHaveAdminAccessToTeam-liz9YbOX.js";
import { p as prisma } from "./orgDomains-eTg-cjth.js";
import { g as getInstalledAppPath } from "./getInstalledAppPath-vg9hE5pP.js";
const JitsiVideoApiAdapter = () => {
  return {
    getAvailability: () => {
      return Promise.resolve([]);
    },
    createMeeting: async (eventData) => {
      const appKeys = await getAppKeysFromSlug(metadata.slug);
      const meetingPattern = appKeys.jitsiPathPattern || "{uuid}";
      const hostUrl = appKeys.jitsiHost || "https://meet.jit.si/cal";
      const meetingID = meetingPattern.replaceAll("{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: metadata.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({ 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 prisma.credential.findFirst({
      where: {
        type: appType,
        ...installForObject
      }
    });
    if (alreadyInstalled) {
      throw new Error("Already installed");
    }
    const installation = await 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({ variant: "conferencing", slug: "jitsi" }) });
}
const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  add: handler
}, Symbol.toStringTag, { value: "Module" }));
export {
  index as api,
  index$1 as lib,
  metadata
};
