import { a as createDefaultInstallation } from "./installation-A6q4n0tL.js";
import { y as config, h as handleErrorsJson } from "./CalEventParser-ZzmnrFso.js";
import { z } from "zod";
import { a9 as getRecordingsResponseSchema, aa as getAccessLinkResponseSchema } from "./user--mpkKOwW.js";
import { g as getAppKeysFromSlug } from "./getAppKeysFromSlug-IDaQR-Xd.js";
const handler = {
  appType: config.type,
  variant: config.variant,
  slug: config.slug,
  supportsMultipleInstalls: false,
  handlerType: "add",
  createCredential: ({ appType, user, slug, teamId }) => createDefaultInstallation({ appType, user, slug, key: {}, teamId })
};
const handler$1 = handler;
const index$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  add: handler$1
}, Symbol.toStringTag, { value: "Module" }));
const shimmerAppKeysSchema = z.object({
  api_key: z.string(),
  api_route: z.string()
});
const getShimmerAppKeys = async () => {
  const appKeys = await getAppKeysFromSlug(config.slug);
  return shimmerAppKeysSchema.parse(appKeys);
};
const dailyReturnTypeSchema = z.object({
  /** Long UID string ie: 987b5eb5-d116-4a4e-8e2c-14fcb5710966 */
  id: z.string(),
  /** Not a real name, just a random generated string ie: "ePR84NQ1bPigp79dDezz" */
  name: z.string(),
  api_created: z.boolean(),
  privacy: z.union([z.literal("private"), z.literal("public")]),
  /** https://api-demo.daily.co/ePR84NQ1bPigp79dDezz */
  url: z.string(),
  created_at: z.string(),
  config: z.object({
    enable_prejoin_ui: z.boolean(),
    enable_people_ui: z.boolean(),
    enable_emoji_reactions: z.boolean(),
    enable_pip_ui: z.boolean(),
    enable_hand_raising: z.boolean(),
    enable_network_ui: z.boolean(),
    enable_video_processing_ui: z.boolean(),
    enable_noise_cancellation_ui: z.boolean(),
    enable_advanced_chat: z.boolean(),
    //above flags are for prebuilt daily
    enable_chat: z.boolean(),
    enable_knocking: 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(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(
          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(
          getAccessLinkResponseSchema.parse
        );
        return Promise.resolve(res);
      } catch (err) {
        console.log("err", err);
        throw new Error("Something went wrong! Unable to get recording access link");
      }
    }
  };
};
const ShimmerDailyVideoApiAdapter$1 = ShimmerDailyVideoApiAdapter;
const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  VideoApiAdapter: ShimmerDailyVideoApiAdapter$1
}, Symbol.toStringTag, { value: "Module" }));
export {
  index$1 as api,
  index as lib
};
