"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index = require("./i18n-Bs8xJzoY.cjs");
const z = require("zod");
const huddle01AppKeySchema = z.z.object({
  identityToken: z.z.string()
});
async function getHuddle01Credential(userId) {
  const credential = await index.prisma.credential.findFirst({
    where: {
      type: "huddle01_video",
      userId,
      appId: "huddle01"
    }
  });
  if (!credential) {
    throw new Error("Huddle01 credential not found");
  }
  return huddle01AppKeySchema.parse(credential?.key);
}
const API_END_POINT = "https://platform-api.huddle01.workers.dev/api/v2/calendar";
const fetchHuddleAPI = async (userId) => {
  const { identityToken } = await getHuddle01Credential(userId);
  const apiKey = process.env.HUDDLE01_API_TOKEN;
  if (!apiKey) {
    log.error("[Huddle01 Error] -> app key not found");
    throw new Error("Huddle01 app key not found");
  }
  const headers = {
    "x-api-key": apiKey,
    "x-identity-token": identityToken
  };
  return (endpoint, option) => {
    return fetch(`${API_END_POINT}/${endpoint}`, {
      ...option,
      headers: {
        ...option?.headers,
        ...headers
      }
    });
  };
};
const log = index.logger.getSubLogger({ prefix: ["app-store/huddle01video/lib/VideoApiAdapter"] });
const Huddle01ApiAdapter = (credential) => {
  return {
    createMeeting: async (e) => {
      if (!credential.userId) {
        log.error("[Huddle01 Error] -> User is not logged in");
        throw new Error("User is not logged in");
      }
      try {
        const fetch2 = await fetchHuddleAPI(credential.userId);
        const res = await fetch2("createMeeting", {
          method: "POST",
          body: JSON.stringify({
            title: e.title,
            startTime: e.startTime,
            endTime: e.endTime
          }),
          headers: {
            "Content-Type": "application/json"
          }
        });
        const data = await res.json();
        return {
          type: "huddle01_video",
          id: data.roomId,
          password: "",
          url: data.meetingLink
        };
      } catch (e2) {
        log.error("[Huddle01 Error] -> Error while creating meeeting", e2);
        throw Error("Error while creating meeting");
      }
    },
    updateMeeting: async (bookingRef, e) => {
      if (!credential.userId) {
        log.error("[Huddle01 Error] -> User is not logged in");
        throw new Error("User is not logged in");
      }
      const fetch2 = await fetchHuddleAPI(credential.userId);
      const res = await fetch2("updateMeeting", {
        method: "PUT",
        body: JSON.stringify({
          title: e.title,
          startTime: e.startTime,
          endTime: e.endTime,
          meetingId: bookingRef.uid
        }),
        headers: {
          "Content-Type": "application/json"
        }
      });
      const data = await res.json();
      return {
        type: "huddle01_video",
        id: data.roomId,
        password: "",
        url: data.meetingLink
      };
    },
    deleteMeeting: async (meetingId) => {
      if (!credential.userId) {
        log.error("[Huddle01 Error] -> User is not logged in");
        throw new Error("User is not logged in");
      }
      const fetch2 = await fetchHuddleAPI(credential.userId);
      const res = await fetch2("deleteMeeting", {
        method: "DELETE",
        body: JSON.stringify({
          meetingId
        }),
        headers: {
          "Content-Type": "application/json"
        }
      });
      const data = await res.json();
      return {
        type: "huddle01_video",
        id: data.roomId,
        password: "",
        url: data.meetingLink
      };
    },
    getAvailability: async () => {
      return [];
    }
  };
};
exports.default = Huddle01ApiAdapter;
