import { a as createDefaultInstallation } from "./installation-U-19f03A.js";
import { a as appConfig } from "./config-Z7hygIpL.js";
import { f as findValidApiKey, l as listBookings$1, d as deleteSubscription$1, a as addSubscription$1 } from "./scheduleTrigger-3b2xiY-u.js";
import { d as defaultHandler } from "./defaultHandler-yvm7aMmf.js";
import { d as defaultResponder } from "./defaultResponder-s4VQhYfH.js";
import z__default from "zod";
import { p as prisma } from "./index-Cw1zbw54.js";
const handler$4 = {
  appType: appConfig.type,
  variant: appConfig.variant,
  slug: appConfig.slug,
  supportsMultipleInstalls: false,
  handlerType: "add",
  redirect: {
    newTab: true,
    url: "/apps/make/setup"
  },
  createCredential: ({ appType, user, slug, teamId }) => createDefaultInstallation({ appType, user, slug, key: {}, teamId })
};
const handler$5 = handler$4;
async function handler$3(req, res) {
  const apiKey = req.query.apiKey;
  if (!apiKey) {
    return res.status(401).json({ message: "No API key provided" });
  }
  const validKey = await findValidApiKey(apiKey, "make");
  if (!validKey) {
    return res.status(401).json({ message: "API key not valid" });
  }
  const bookings = await listBookings$1(validKey);
  if (!bookings) {
    return res.status(500).json({ message: "Unable to get bookings." });
  }
  if (bookings.length === 0) {
    const requested = validKey.teamId ? `teamId: ${validKey.teamId}` : `userId: ${validKey.userId}`;
    return res.status(404).json({
      message: `There are no bookings to retrieve, please create a booking first. Requested: \`${requested}\``
    });
  }
  res.status(201).json(bookings);
}
const listBookings = defaultHandler({
  GET: Promise.resolve({ default: defaultResponder(handler$3) })
});
const querySchema = z__default.object({
  apiKey: z__default.string(),
  id: z__default.string()
});
async function handler$2(req, res) {
  const { apiKey, id } = querySchema.parse(req.query);
  if (!apiKey) {
    return res.status(401).json({ message: "No API key provided" });
  }
  const validKey = await findValidApiKey(apiKey, "make");
  if (!validKey) {
    return res.status(401).json({ message: "API key not valid" });
  }
  const deleteEventSubscription = await deleteSubscription$1({
    appApiKey: validKey,
    webhookId: id,
    appId: "make"
  });
  if (!deleteEventSubscription) {
    return res.status(500).json({ message: "Could not delete subscription." });
  }
  res.status(204).json({ message: "Subscription is deleted." });
}
const deleteSubscription = defaultHandler({
  DELETE: Promise.resolve({ default: defaultResponder(handler$2) })
});
async function handler$1(req, res) {
  const apiKey = req.query.apiKey;
  if (!apiKey) {
    return res.status(401).json({ message: "No API key provided" });
  }
  const validKey = await findValidApiKey(apiKey, "make");
  if (!validKey) {
    return res.status(401).json({ message: "API key not valid" });
  }
  const { subscriberUrl, triggerEvent } = req.body;
  const createAppSubscription = await addSubscription$1({
    appApiKey: validKey,
    triggerEvent,
    subscriberUrl,
    appId: "make"
  });
  if (!createAppSubscription) {
    return res.status(500).json({ message: "Could not create subscription." });
  }
  res.status(200).json(createAppSubscription);
}
const addSubscription = defaultHandler({
  POST: Promise.resolve({ default: defaultResponder(handler$1) })
});
async function handler(req, res) {
  const apiKey = req.query.apiKey;
  if (!apiKey) {
    return res.status(401).json({ message: "No API key provided" });
  }
  const validKey = await findValidApiKey(apiKey, "make");
  if (!validKey) {
    return res.status(401).json({ message: "API key not valid" });
  }
  if (req.method === "GET") {
    try {
      const user = await prisma.user.findFirst({
        where: {
          id: validKey.userId
        },
        select: {
          username: true
        }
      });
      res.status(201).json(user);
    } catch (error) {
      console.error(error);
      return res.status(500).json({ message: "Unable to get User." });
    }
  }
}
const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  add: handler$5,
  addSubscription,
  deleteSubscription,
  listBookings,
  me: handler
}, Symbol.toStringTag, { value: "Module" }));
export {
  index as api
};
