import { defineComponent, ref, watch, createElementBlock, openBlock, normalizeClass, unref, createElementVNode, createVNode, toDisplayString, withCtx, createTextVNode } from "vue";
import { TUIErrorCode } from "@tencentcloud/tuiroom-engine-js";
import { useUIKit, IconBack, TUISwitch, TUIButton, TUIToast } 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-label" };
const _hoisted_7 = { class: "form-input" };
const _hoisted_8 = { class: "room-settings" };
const _hoisted_9 = { class: "form-item toggle-item" };
const _hoisted_10 = { class: "form-label" };
const _hoisted_11 = { class: "form-item toggle-item" };
const _hoisted_12 = { class: "form-label" };
const _hoisted_13 = { class: "footer-h5" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "CreateRoomView",
  props: {
    cameraPreference: { type: Boolean, default: true },
    microphonePreference: { type: Boolean, default: true }
  },
  emits: ["create-room", "back", "camera-preference-change", "microphone-preference-change"],
  setup(__props, { emit: __emit }) {
    const emit = __emit;
    const props = __props;
    const { t, theme } = useUIKit();
    const { loginUserInfo } = useLoginState();
    const { getRoomInfo } = useRoomState();
    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 handleGoBack = () => {
      emit("back");
    };
    const checkRoomExist = async (roomId) => {
      try {
        await getRoomInfo({ roomId });
      } catch (error) {
        if (error && typeof error === "object" && "code" in error && error.code === TUIErrorCode.ERR_ROOM_ID_NOT_EXIST) {
          return false;
        }
      }
      return true;
    };
    const generateRoomId = async () => {
      const roomId = String(Math.floor(Math.random() * 9e5) + 1e5);
      if (await checkRoomExist(roomId)) {
        return generateRoomId();
      }
      return roomId;
    };
    const handleCreateRoom = async () => {
      loading.value = true;
      try {
        const roomId = await generateRoomId();
        emit("create-room", roomId);
      } catch (error) {
        const errorMessage = error && typeof error === "object" && "message" in error ? String(error.message) : t("Room.CreateRoomError");
        TUIToast.error({ message: errorMessage });
      } finally {
        loading.value = false;
      }
    };
    return (_ctx, _cache) => {
      var _a, _b;
      return openBlock(), createElementBlock("div", {
        class: normalizeClass(["create-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.CreateRoom")), 1),
          _cache[2] || (_cache[2] = createElementVNode("div", { class: "header-placeholder" }, null, -1))
        ]),
        createElementVNode("main", _hoisted_3, [
          createElementVNode("div", _hoisted_4, [
            createElementVNode("div", _hoisted_5, [
              createElementVNode("span", _hoisted_6, toDisplayString(unref(t)("User.Nickname")), 1),
              createElementVNode("span", _hoisted_7, toDisplayString(((_a = unref(loginUserInfo)) == null ? void 0 : _a.userName) || ((_b = unref(loginUserInfo)) == null ? void 0 : _b.userId)), 1)
            ])
          ]),
          createElementVNode("div", _hoisted_8, [
            createElementVNode("div", _hoisted_9, [
              createElementVNode("span", _hoisted_10, toDisplayString(unref(t)("Room.OpenMicrophone")), 1),
              createVNode(unref(TUISwitch), {
                modelValue: openMicrophone.value,
                "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => openMicrophone.value = $event),
                size: "large"
              }, null, 8, ["modelValue"])
            ]),
            createElementVNode("div", _hoisted_11, [
              createElementVNode("span", _hoisted_12, toDisplayString(unref(t)("Room.OpenCamera")), 1),
              createVNode(unref(TUISwitch), {
                modelValue: openCamera.value,
                "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => openCamera.value = $event),
                size: "large"
              }, null, 8, ["modelValue"])
            ])
          ])
        ]),
        createElementVNode("div", _hoisted_13, [
          createVNode(unref(TUIButton), {
            type: "primary",
            class: "create-button",
            loading: loading.value,
            onClick: handleCreateRoom
          }, {
            default: withCtx(() => [
              createTextVNode(toDisplayString(unref(t)("Button.CreateRoom")), 1)
            ]),
            _: 1
          }, 8, ["loading"])
        ])
      ], 2);
    };
  }
});
export {
  _sfc_main as default
};
