"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const installation = require("./installation-aMIHG4H9.cjs");
const config = require("./config-FDUPLr4t.cjs");
const scheduleTrigger = require("./scheduleTrigger-_fJQ0gee.cjs");
const defaultHandler = require("./defaultHandler-MSjhitc-.cjs");
const defaultResponder = require("./defaultResponder-pIXL4EeF.cjs");
const z = require("zod");
const index$1 = require("./orgDomains-W_rFTMBa.cjs");
const handler$4 = {
  appType: config.appConfig.type,
  variant: config.appConfig.variant,
  slug: config.appConfig.slug,
  supportsMultipleInstalls: false,
  handlerType: "add",
  redirect: {
    newTab: true,
    url: "/apps/make/setup"
  },
  createCredential: ({ appType, user, slug, teamId }) => installation.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 scheduleTrigger.findValidApiKey(apiKey, "make");
  if (!validKey) {
    return res.status(401).json({ message: "API key not valid" });
  }
  const bookings = await scheduleTrigger.listBookings(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.defaultHandler({
  GET: Promise.resolve({ default: defaultResponder.defaultResponder(handler$3) })
});
const querySchema = z.object({
  apiKey: z.string(),
  id: z.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 scheduleTrigger.findValidApiKey(apiKey, "make");
  if (!validKey) {
    return res.status(401).json({ message: "API key not valid" });
  }
  const deleteEventSubscription = await scheduleTrigger.deleteSubscription({
    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.defaultHandler({
  DELETE: Promise.resolve({ default: defaultResponder.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 scheduleTrigger.findValidApiKey(apiKey, "make");
  if (!validKey) {
    return res.status(401).json({ message: "API key not valid" });
  }
  const { subscriberUrl, triggerEvent } = req.body;
  const createAppSubscription = await scheduleTrigger.addSubscription({
    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.defaultHandler({
  POST: Promise.resolve({ default: defaultResponder.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 scheduleTrigger.findValidApiKey(apiKey, "make");
  if (!validKey) {
    return res.status(401).json({ message: "API key not valid" });
  }
  if (req.method === "GET") {
    try {
      const user = await index$1.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" }));
exports.api = index;
