import { defineComponent, useCssVars, computed, ref, watch, createElementBlock, openBlock, normalizeClass, unref, createElementVNode, createVNode, toDisplayString, withCtx, createTextVNode } from "vue";
import { TUIErrorCode } from "@tencentcloud/tuiroom-engine-js";
import { useUIKit, IconBack, TUIInput, TUISwitch, TUIButton, TUIToast, TUIMessageBox } from "@tencentcloud/uikit-base-component-vue3";
import { useLoginState, useRoomState } from "tuikit-atomicx-vue3/room";
const _hoisted_1 = { class: "header-h5" };
const _hoisted_2 = { class: "title" };
const _hoisted_3 = { class: "main-h5" };
const _hoisted_4 = { class: "room-info" };
const _hoisted_5 = { class: "form-item" };
const _hoisted_6 = { class: "form-item" };
const _hoisted_7 = { class: "form-label" };
const _hoisted_8 = { class: "form-input" };
const _hoisted_9 = { class: "room-settings" };
const _hoisted_10 = { class: "form-item toggle-item" };
const _hoisted_11 = { class: "form-label" };
const _hoisted_12 = { class: "form-item toggle-item" };
const _hoisted_13 = { class: "form-label" };
const _hoisted_14 = { class: "footer-h5" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "JoinRoomView",
  props: {
    cameraPreference: { type: Boolean, default: true },
    microphonePreference: { type: Boolean, default: true }
  },
  emits: ["join-room", "back", "camera-preference-change", "microphone-preference-change"],
  setup(__props, { emit: __emit }) {
    useCssVars((_ctx) => ({
      "v0a779934": labelWidth.value
    }));
    const emit = __emit;
    const props = __props;
    const { t, theme, language } = useUIKit();
    const { loginUserInfo } = useLoginState();
    const { getRoomInfo } = useRoomState();
    const roomId = ref("");
    const openMicrophone = ref(props.microphonePreference);
    const openCamera = ref(props.cameraPreference);
    const loading = ref(false);
    watch(openMicrophone, (newVal) => {
      emit("microphone-preference-change", newVal);
    });
    watch(openCamera, (newVal) => {
      emit("camera-preference-change", newVal);
    });
    const labelWidth = computed(() => {
      if (language.value === "en-US") {
        return "115px";
      }
      return "80px";
    });
    const handleGoBack = () => {
      emit("back");
    };
    const checkRoomExist = async (id) => {
      try {
        await getRoomInfo({ roomId: id });
      } catch (error) {
        if (error && typeof error === "object" && "code" in error && error.code === TUIErrorCode.ERR_ROOM_ID_NOT_EXIST) {
          return false;
        }
      }
      return true;
    };
    const handleJoinRoom = async () => {
      loading.value = true;
      if (!roomId.value.trim()) {
        TUIToast.error({ message: t("Room.RoomIdRequired") });
        return;
      }
      try {
        const exists = await checkRoomExist(roomId.value.trim());
        if (!exists) {
          TUIMessageBox.alert({
            type: "error",
            modal: false,
            showClose: false,
            title: t("Room.Alert"),
            content: t("Room.RoomNotFound")
          });
          return;
        }
        emit("join-room", roomId.value.trim());
      } catch (error) {
        const errorMessage = error && typeof error === "object" && "message" in error ? String(error.message) : t("Room.JoinRoomError");
        TUIToast.error({ message: errorMessage });
      } finally {
        loading.value = false;
      }
    };
    return (_ctx, _cache) => {
      var _a, _b;
      return openBlock(), createElementBlock("div", {
        class: normalizeClass(["join-room-container-h5", unref(theme)])
      }, [
        createElementVNode("header", _hoisted_1, [
          createVNode(unref(IconBack), {
            size: "22",
            class: "back-button",
            onClick: handleGoBack
          }),
          createElementVNode("h1", _hoisted_2, toDisplayString(unref(t)("Button.JoinRoom")), 1),
          _cache[3] || (_cache[3] = createElementVNode("div", { class: "header-placeholder" }, null, -1))
        ]),
        createElementVNode("main", _hoisted_3, [
          createElementVNode("div", _hoisted_4, [
            createElementVNode("div", _hoisted_5, [
              createVNode(unref(TUIInput), {
                modelValue: roomId.value,
                "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => roomId.value = $event),
                label: unref(t)("Room.RoomId"),
                class: "form-input",
                placeholder: unref(t)("Room.EnterRoomId"),
                "max-length": "6"
              }, null, 8, ["modelValue", "label", "placeholder"])
            ]),
            createElementVNode("div", _hoisted_6, [
              createElementVNode("span", _hoisted_7, toDisplayString(unref(t)("User.Nickname")), 1),
              createElementVNode("span", _hoisted_8, toDisplayString(((_a = unref(loginUserInfo)) == null ? void 0 : _a.userName) || ((_b = unref(loginUserInfo)) == null ? void 0 : _b.userId)), 1)
            ])
          ]),
          createElementVNode("div", _hoisted_9, [
            createElementVNode("div", _hoisted_10, [
              createElementVNode("span", _hoisted_11, toDisplayString(unref(t)("Room.OpenMicrophone")), 1),
              createVNode(unref(TUISwitch), {
                modelValue: openMicrophone.value,
                "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => openMicrophone.value = $event),
                size: "large"
              }, null, 8, ["modelValue"])
            ]),
            createElementVNode("div", _hoisted_12, [
              createElementVNode("span", _hoisted_13, toDisplayString(unref(t)("Room.OpenCamera")), 1),
              createVNode(unref(TUISwitch), {
                modelValue: openCamera.value,
                "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => openCamera.value = $event),
                size: "large"
              }, null, 8, ["modelValue"])
            ])
          ])
        ]),
        createElementVNode("div", _hoisted_14, [
          createVNode(unref(TUIButton), {
            type: "primary",
            class: "join-button",
            loading: loading.value,
            onClick: handleJoinRoom
          }, {
            default: withCtx(() => [
              createTextVNode(toDisplayString(unref(t)("Button.JoinRoom")), 1)
            ]),
            _: 1
          }, 8, ["loading"])
        ])
      ], 2);
    };
  }
});
export {
  _sfc_main as default
};
