import { defineComponent, watch, onMounted, onBeforeUnmount, createElementBlock, openBlock, normalizeClass, unref, createElementVNode, createVNode, createCommentVNode, createBlock, toDisplayString } from "vue";
import TUIRoomEngine from "@tencentcloud/tuiroom-engine-js";
import { useUIKit, IconLoading } from "@tencentcloud/uikit-base-component-vue3";
import { useDeviceState, useRoomModal } from "tuikit-atomicx-vue3/room";
import CameraButtonH5 from "../../components/CameraButtonH5/index.vue2.mjs";
import JoinRoomButtonH5 from "../../components/JoinRoomButtonH5/index.vue2.mjs";
import LanguageButton from "../../components/LanguageButton/index.vue2.mjs";
import LoginUserInfoH5 from "../../components/LoginUserInfoH5/index.vue2.mjs";
import MicButtonH5 from "../../components/MicButtonH5/index.vue2.mjs";
import StartRoomButtonH5 from "../../components/StartRoomButtonH5/index.vue2.mjs";
import _sfc_main$1 from "../../components/SwitchCameraButtonH5/index.vue.mjs";
import ThemeButton from "../../components/ThemeButton/index.vue2.mjs";
const _hoisted_1 = { class: "header-h5" };
const _hoisted_2 = { class: "user-info-section" };
const _hoisted_3 = { class: "header-actions" };
const _hoisted_4 = { class: "main-h5" };
const _hoisted_5 = { class: "preview-card" };
const _hoisted_6 = { class: "camera-preview-area" };
const _hoisted_7 = { class: "camera-preview-area-header" };
const _hoisted_8 = { class: "attention-info" };
const _hoisted_9 = {
  key: 0,
  class: "off-camera-info"
};
const _hoisted_10 = { class: "media-controls-overlay" };
const _hoisted_11 = { class: "action-buttons" };
const _hoisted_12 = { class: "footer-h5" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "PreviewView",
  emits: ["logout", "create-room", "join-room", "camera-preference-change", "microphone-preference-change"],
  setup(__props, { emit: __emit }) {
    const emit = __emit;
    const { t, theme, language } = useUIKit();
    const {
      isCameraTesting,
      isMicrophoneTesting,
      isCameraTestLoading,
      startCameraTest,
      startMicrophoneTest,
      stopCameraTest,
      stopMicrophoneTest
    } = useDeviceState();
    const { handleErrorWithModal } = useRoomModal();
    watch(isMicrophoneTesting, (newVal) => {
      emit("microphone-preference-change", newVal);
    });
    watch(isCameraTesting, (newVal) => {
      emit("camera-preference-change", newVal);
    });
    const handleCreateRoom = async () => {
      emit("create-room");
    };
    const handleJoinRoom = async () => {
      emit("join-room");
    };
    function handleLogout() {
      emit("logout");
    }
    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);
        }
      });
    });
    onBeforeUnmount(() => {
      stopCameraTest();
      stopMicrophoneTest();
    });
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", {
        class: normalizeClass(["home-container-h5", unref(theme)])
      }, [
        createElementVNode("header", _hoisted_1, [
          createElementVNode("div", _hoisted_2, [
            createVNode(LoginUserInfoH5, { onLogout: handleLogout })
          ]),
          createElementVNode("div", _hoisted_3, [
            createVNode(ThemeButton),
            createVNode(LanguageButton)
          ])
        ]),
        createElementVNode("main", _hoisted_4, [
          createElementVNode("div", _hoisted_5, [
            createElementVNode("div", _hoisted_6, [
              createElementVNode("div", _hoisted_7, [
                createVNode(_sfc_main$1)
              ]),
              _cache[0] || (_cache[0] = createElementVNode("div", {
                id: "room-preview-video",
                class: "video-preview"
              }, null, -1)),
              createElementVNode("div", _hoisted_8, [
                !unref(isCameraTesting) && !unref(isCameraTestLoading) ? (openBlock(), createElementBlock("span", _hoisted_9, toDisplayString(unref(t)("Off Camera")), 1)) : createCommentVNode("", true),
                unref(isCameraTestLoading) ? (openBlock(), createBlock(unref(IconLoading), {
                  key: 1,
                  size: "36",
                  class: "loading"
                })) : createCommentVNode("", true)
              ]),
              createElementVNode("div", _hoisted_10, [
                createVNode(MicButtonH5, {
                  showDescription: false,
                  customStyle: { backgroundColor: "transparent", color: "#fff" }
                }),
                createVNode(CameraButtonH5, {
                  cameraTestContainer: "room-preview-video",
                  showDescription: false,
                  customStyle: { backgroundColor: "transparent", color: "#fff" }
                })
              ])
            ])
          ]),
          createElementVNode("div", _hoisted_11, [
            createVNode(JoinRoomButtonH5, {
              class: "primary-button",
              onJoinRoom: handleJoinRoom
            }),
            createVNode(StartRoomButtonH5, {
              class: "primary-button",
              onStartRoom: handleCreateRoom
            })
          ])
        ]),
        createElementVNode("footer", _hoisted_12, [
          createElementVNode("div", {
            class: normalizeClass(["brand-logo", unref(theme), unref(language)])
          }, null, 2)
        ])
      ], 2);
    };
  }
});
export {
  _sfc_main as default
};
