"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const z = require("zod");
const CalEventParser = require("./CalEventParser-RR7nWuhu.cjs");
const index = require("./i18n-HHdcQbuJ.cjs");
const getAppKeysFromSlug = require("./getAppKeysFromSlug-tS_OdWJj.cjs");
const shimmerAppKeysSchema = z.z.object({
  api_key: z.z.string(),
  api_route: z.z.string()
});
const getShimmerAppKeys = async () => {
  const appKeys = await getAppKeysFromSlug.getAppKeysFromSlug(CalEventParser.config$2.slug);
  return shimmerAppKeysSchema.parse(appKeys);
};
const dailyReturnTypeSchema = z.z.object({
  /** Long UID string ie: 987b5eb5-d116-4a4e-8e2c-14fcb5710966 */
  id: z.z.string(),
  /** Not a real name, just a random generated string ie: "ePR84NQ1bPigp79dDezz" */
  name: z.z.string(),
  api_created: z.z.boolean(),
  privacy: z.z.union([z.z.literal("private"), z.z.literal("public")]),
  /** https://api-demo.daily.co/ePR84NQ1bPigp79dDezz */
  url: z.z.string(),
  created_at: z.z.string(),
  config: z.z.object({
    enable_prejoin_ui: z.z.boolean(),
    enable_people_ui: z.z.boolean(),
    enable_emoji_reactions: z.z.boolean(),
    enable_pip_ui: z.z.boolean(),
    enable_hand_raising: z.z.boolean(),
    enable_network_ui: z.z.boolean(),
    enable_video_processing_ui: z.z.boolean(),
    enable_noise_cancellation_ui: z.z.boolean(),
    enable_advanced_chat: z.z.boolean(),
    //above flags are for prebuilt daily
    enable_chat: z.z.boolean(),
    enable_knocking: z.z.boolean()
  })
});
const fetcher = async (endpoint, init) => {
  const { api_key } = await getShimmerAppKeys();
  const response = await fetch(`https://api.daily.co/v1${endpoint}`, {
    method: "GET",
    headers: {
      Authorization: `Bearer ${api_key}`,
      "Content-Type": "application/json",
      ...init?.headers
    },
    ...init
  }).then(CalEventParser.handleErrorsJson);
  return response;
};
const fetcherShimmer = async (endpoint, init) => {
  const { api_key, api_route } = await getShimmerAppKeys();
  if (!api_route) {
    return Promise.resolve([]);
  }
  const response = await fetch(`${api_route}${endpoint}`, {
    method: "GET",
    headers: {
      Authorization: `Bearer ${api_key}`,
      "Content-Type": "application/json",
      ...init?.headers
    },
    ...init
  });
  return response;
};
const postToShimmerAPI = async (event, endpoint, body) => {
  return fetcherShimmer(endpoint, {
    method: "POST",
    body: JSON.stringify({
      cal: event,
      daily: body
    })
  });
};
function postToDailyAPI(endpoint, body) {
  return fetcher(endpoint, {
    method: "POST",
    body: JSON.stringify(body)
  });
}
const ShimmerDailyVideoApiAdapter = () => {
  async function createOrUpdateMeeting(endpoint, event) {
    if (!event.uid) {
      throw new Error("We need need the booking uid to create the Daily reference in DB");
    }
    const body = await translateEvent();
    const dailyEvent = await postToDailyAPI(endpoint, body).then(dailyReturnTypeSchema.parse);
    await postToShimmerAPI(event, "trackDailyRoom", dailyEvent);
    return Promise.resolve({
      type: "shimmer_video",
      id: dailyEvent.name,
      password: "",
      //   password: meetingToken.token,
      url: `https://app.shimmer.care?videoId=${dailyEvent.name}`
    });
  }
  const translateEvent = async () => {
    return {
      privacy: "private",
      properties: {
        enable_prejoin_ui: true,
        enable_people_ui: true,
        enable_emoji_reactions: true,
        enable_pip_ui: true,
        enable_hand_raising: true,
        enable_network_ui: true,
        enable_video_processing_ui: true,
        enable_noise_cancellation_ui: true,
        enable_advanced_chat: true,
        //above flags are for prebuilt daily
        enable_knocking: true,
        enable_screenshare: true,
        enable_chat: true
      }
    };
  };
  return {
    /** Daily doesn't need to return busy times, so we return empty */
    getAvailability: () => {
      return Promise.resolve([]);
    },
    createMeeting: async (event) => createOrUpdateMeeting("/rooms", event),
    deleteMeeting: async (uid) => {
      await fetcher(`/rooms/${uid}`, { method: "DELETE" });
      return Promise.resolve();
    },
    updateMeeting: (bookingRef, event) => createOrUpdateMeeting(`/rooms/${bookingRef.uid}`, event),
    getRecordings: async (roomName) => {
      try {
        const res = await fetcher(`/recordings?room_name=${roomName}`).then(
          index.getRecordingsResponseSchema.parse
        );
        return Promise.resolve(res);
      } catch (err) {
        throw new Error("Something went wrong! Unable to get recording");
      }
    },
    getRecordingDownloadLink: async (recordingId) => {
      try {
        const res = await fetcher(`/recordings/${recordingId}/access-link?valid_for_secs=43200`).then(
          index.getAccessLinkResponseSchema.parse
        );
        return Promise.resolve(res);
      } catch (err) {
        console.log("err", err);
        throw new Error("Something went wrong! Unable to get recording access link");
      }
    }
  };
};
exports.default = ShimmerDailyVideoApiAdapter;
exports.fetcher = fetcher;
exports.fetcherShimmer = fetcherShimmer;
exports.postToShimmerAPI = postToShimmerAPI;
