"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const TUIRoomEngine = require("@tencentcloud/tuiroom-engine-js");
const uikitBaseComponentVue3 = require("@tencentcloud/uikit-base-component-vue3");
const room = require("tuikit-atomicx-vue3/room");
const index$4 = require("../../components/CameraButton/index.vue2.js");
const index$6 = require("../../components/JoinRoomButton/index.vue2.js");
const index$1 = require("../../components/LanguageButton/index.vue2.js");
const index$2 = require("../../components/LoginUserInfo/index.vue2.js");
const index$3 = require("../../components/MicButton/index.vue2.js");
const index$7 = require("../../components/ScheduledRoomButton/index.vue2.js");
const index$5 = require("../../components/StartRoomButton/index.vue2.js");
const index = require("../../components/ThemeButton/index.vue2.js");
const _hoisted_1 = { class: "header" };
const _hoisted_2 = { class: "header-left" };
const _hoisted_3 = { class: "header-right" };
const _hoisted_4 = { class: "main" };
const _hoisted_5 = { class: "title" };
const _hoisted_6 = { class: "main-container" };
const _hoisted_7 = { class: "room-preview-container" };
const _hoisted_8 = { class: "camera-preview-area" };
const _hoisted_9 = { class: "attention-info" };
const _hoisted_10 = {
  key: 0,
  class: "off-camera-info"
};
const _hoisted_11 = { class: "control-container" };
const _hoisted_12 = { class: "media-control-region" };
const _hoisted_13 = { class: "room-control-region" };
const _hoisted_14 = { class: "schedule-list" };
const MAX_ROOM_ID_RETRIES = 5;
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  __name: "PreviewView",
  emits: ["create-room", "join-room", "camera-preference-change", "microphone-preference-change", "logout"],
  setup(__props, { emit: __emit }) {
    const emit = __emit;
    const { t, theme, language } = uikitBaseComponentVue3.useUIKit();
    const { getRoomInfo } = room.useRoomState();
    const {
      isMicrophoneTesting,
      isCameraTesting,
      isCameraTestLoading,
      startCameraTest,
      startMicrophoneTest,
      stopCameraTest,
      stopMicrophoneTest
    } = room.useDeviceState();
    const { handleErrorWithModal } = room.useRoomModal();
    vue.watch(isMicrophoneTesting, () => {
      emit("microphone-preference-change", isMicrophoneTesting.value);
    });
    vue.watch(isCameraTesting, () => {
      emit("camera-preference-change", isCameraTesting.value);
    });
    const checkRoomExist = async (roomId) => {
      try {
        await getRoomInfo({ roomId });
      } catch (error) {
        if (error.code === TUIRoomEngine.TUIErrorCode.ERR_ROOM_ID_NOT_EXIST) {
          return false;
        }
      }
      return true;
    };
    const generateRoomId = async (roomType, retriesLeft = MAX_ROOM_ID_RETRIES) => {
      const roomId = String(Math.floor(Math.random() * 9e5) + 1e5);
      if (!await checkRoomExist(roomId)) {
        return roomType === room.RoomType.Standard ? roomId : `webinar_${roomId}`;
      }
      if (retriesLeft <= 1) {
        const fallbackId = `${Math.floor(Math.random() * 9e5) + 1e5}_${Date.now()}`;
        return roomType === room.RoomType.Standard ? fallbackId : `webinar_${fallbackId}`;
      }
      return generateRoomId(roomType, retriesLeft - 1);
    };
    const handleStartRoom = async (roomType) => {
      const roomId = await generateRoomId(roomType);
      sessionStorage.setItem(`room-${roomId}-isCreate`, "true");
      emit("create-room", roomId, roomType);
    };
    const handleJoinRoom = async (roomId) => {
      if (!roomId) {
        uikitBaseComponentVue3.TUIToast.error({ message: t("Room.RoomIdRequired") });
        return;
      }
      if (await checkRoomExist(roomId)) {
        const isWebinar = roomId.startsWith("webinar_");
        emit("join-room", roomId, isWebinar ? room.RoomType.Webinar : room.RoomType.Standard);
      } else {
        uikitBaseComponentVue3.TUIMessageBox.alert({
          type: "error",
          modal: false,
          showClose: false,
          title: t("Room.Alert"),
          content: t("Room.RoomNotFound")
        });
      }
    };
    function handleLogout() {
      emit("logout");
    }
    vue.onMounted(() => {
      const roomPreviewVideo = document.getElementById("room-preview-video");
      TUIRoomEngine.once("ready", async () => {
        if (roomPreviewVideo) {
          try {
            await startCameraTest({ view: roomPreviewVideo });
          } catch (error) {
            handleErrorWithModal(error);
          }
        }
        try {
          await startMicrophoneTest();
        } catch (error) {
          handleErrorWithModal(error);
        }
      });
    });
    vue.onBeforeUnmount(() => {
      stopCameraTest();
      stopMicrophoneTest();
    });
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock("div", {
        class: vue.normalizeClass(["home-container", vue.unref(theme)])
      }, [
        vue.createElementVNode("header", _hoisted_1, [
          vue.createElementVNode("div", _hoisted_2, [
            vue.createVNode(index.default)
          ]),
          vue.createElementVNode("div", _hoisted_3, [
            vue.createVNode(index$1.default),
            vue.createVNode(index$2.default, { onLogout: handleLogout })
          ])
        ]),
        vue.createElementVNode("main", _hoisted_4, [
          vue.createElementVNode("div", _hoisted_5, [
            vue.createElementVNode("div", {
              class: vue.normalizeClass(["logo", vue.unref(language)])
            }, null, 2)
          ]),
          vue.createElementVNode("div", _hoisted_6, [
            vue.createElementVNode("div", _hoisted_7, [
              vue.createElementVNode("div", _hoisted_8, [
                _cache[1] || (_cache[1] = vue.createElementVNode("div", {
                  id: "room-preview-video",
                  class: "video-preview"
                }, null, -1)),
                vue.createElementVNode("div", _hoisted_9, [
                  !vue.unref(isCameraTesting) && !vue.unref(isCameraTestLoading) ? (vue.openBlock(), vue.createElementBlock("span", _hoisted_10, vue.toDisplayString(vue.unref(t)("Off Camera")), 1)) : vue.createCommentVNode("", true),
                  vue.unref(isCameraTestLoading) ? (vue.openBlock(), vue.createBlock(vue.unref(uikitBaseComponentVue3.IconLoading), {
                    key: 1,
                    size: "36",
                    class: "loading"
                  })) : vue.createCommentVNode("", true)
                ])
              ]),
              vue.createElementVNode("div", _hoisted_11, [
                vue.createElementVNode("div", _hoisted_12, [
                  vue.createVNode(index$3.default),
                  vue.createVNode(index$4.default, { cameraTestContainer: "room-preview-video" })
                ]),
                vue.createElementVNode("div", _hoisted_13, [
                  vue.createVNode(index$5.default, {
                    class: "button-item",
                    onStartRoom: handleStartRoom
                  }),
                  vue.createVNode(index$6.default, {
                    class: "button-item",
                    onJoinRoom: handleJoinRoom
                  }),
                  vue.createVNode(index$7.default, { class: "button-item" })
                ])
              ])
            ]),
            vue.createElementVNode("div", _hoisted_14, [
              vue.createVNode(vue.unref(room.ScheduledRoomList), {
                onJoinRoom: _cache[0] || (_cache[0] = (roomInfo) => handleJoinRoom(roomInfo.roomId))
              })
            ])
          ])
        ])
      ], 2);
    };
  }
});
exports.default = _sfc_main;
