"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 _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__ */ vue.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 } = uikitBaseComponentVue3.useUIKit();
    const { loginUserInfo } = room.useLoginState();
    const { getRoomInfo } = room.useRoomState();
    const openMicrophone = vue.ref(props.microphonePreference);
    const openCamera = vue.ref(props.cameraPreference);
    const loading = vue.ref(false);
    vue.watch(openMicrophone, (newVal) => {
      emit("microphone-preference-change", newVal);
    });
    vue.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 === TUIRoomEngine.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");
        uikitBaseComponentVue3.TUIToast.error({ message: errorMessage });
      } finally {
        loading.value = false;
      }
    };
    return (_ctx, _cache) => {
      var _a, _b;
      return vue.openBlock(), vue.createElementBlock("div", {
        class: vue.normalizeClass(["create-room-container-h5", vue.unref(theme)])
      }, [
        vue.createElementVNode("header", _hoisted_1, [
          vue.createVNode(vue.unref(uikitBaseComponentVue3.IconBack), {
            size: "22",
            class: "back-button",
            onClick: handleGoBack
          }),
          vue.createElementVNode("h1", _hoisted_2, vue.toDisplayString(vue.unref(t)("Button.CreateRoom")), 1),
          _cache[2] || (_cache[2] = vue.createElementVNode("div", { class: "header-placeholder" }, null, -1))
        ]),
        vue.createElementVNode("main", _hoisted_3, [
          vue.createElementVNode("div", _hoisted_4, [
            vue.createElementVNode("div", _hoisted_5, [
              vue.createElementVNode("span", _hoisted_6, vue.toDisplayString(vue.unref(t)("User.Nickname")), 1),
              vue.createElementVNode("span", _hoisted_7, vue.toDisplayString(((_a = vue.unref(loginUserInfo)) == null ? void 0 : _a.userName) || ((_b = vue.unref(loginUserInfo)) == null ? void 0 : _b.userId)), 1)
            ])
          ]),
          vue.createElementVNode("div", _hoisted_8, [
            vue.createElementVNode("div", _hoisted_9, [
              vue.createElementVNode("span", _hoisted_10, vue.toDisplayString(vue.unref(t)("Room.OpenMicrophone")), 1),
              vue.createVNode(vue.unref(uikitBaseComponentVue3.TUISwitch), {
                modelValue: openMicrophone.value,
                "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => openMicrophone.value = $event),
                size: "large"
              }, null, 8, ["modelValue"])
            ]),
            vue.createElementVNode("div", _hoisted_11, [
              vue.createElementVNode("span", _hoisted_12, vue.toDisplayString(vue.unref(t)("Room.OpenCamera")), 1),
              vue.createVNode(vue.unref(uikitBaseComponentVue3.TUISwitch), {
                modelValue: openCamera.value,
                "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => openCamera.value = $event),
                size: "large"
              }, null, 8, ["modelValue"])
            ])
          ])
        ]),
        vue.createElementVNode("div", _hoisted_13, [
          vue.createVNode(vue.unref(uikitBaseComponentVue3.TUIButton), {
            type: "primary",
            class: "create-button",
            loading: loading.value,
            onClick: handleCreateRoom
          }, {
            default: vue.withCtx(() => [
              vue.createTextVNode(vue.toDisplayString(vue.unref(t)("Button.CreateRoom")), 1)
            ]),
            _: 1
          }, 8, ["loading"])
        ])
      ], 2);
    };
  }
});
exports.default = _sfc_main;
