import { defineComponent, ref, watch, createBlock, openBlock, unref, withCtx, createVNode, withKeys } from "vue";
import { useUIKit, TUIDialog, TUIInput, TUIToast } from "@tencentcloud/uikit-base-component-vue3";
import { useRoomState } from "tuikit-atomicx-vue3/room";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  props: {
    modelValue: { type: Boolean },
    roomId: {}
  },
  emits: ["update:modelValue", "success", "cancel", "error"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const { t } = useUIKit();
    const { joinRoom } = useRoomState();
    const visible = ref(false);
    const password = ref("");
    const isJoining = ref(false);
    watch(() => props.modelValue, (val) => {
      visible.value = val;
    });
    watch(visible, (val) => {
      emit("update:modelValue", val);
      if (!val) {
        password.value = "";
      }
    });
    const handleConfirm = async () => {
      if (!password.value) {
        TUIToast.error({ message: t("Room.EnterPassword") });
        return;
      }
      if (isJoining.value) {
        return;
      }
      try {
        isJoining.value = true;
        await joinRoom({
          roomId: props.roomId,
          password: password.value
        });
        visible.value = false;
        password.value = "";
        emit("success", { roomId: props.roomId, password: password.value });
      } catch (error) {
        console.error("Failed to join room with password:", error);
        emit("error", error);
      } finally {
        isJoining.value = false;
      }
    };
    const handleCancel = () => {
      visible.value = false;
      password.value = "";
      emit("cancel");
    };
    return (_ctx, _cache) => {
      return openBlock(), createBlock(unref(TUIDialog), {
        visible: visible.value,
        "onUpdate:visible": _cache[1] || (_cache[1] = ($event) => visible.value = $event),
        title: unref(t)("Room.EnterPassword"),
        "show-close": false,
        modal: false,
        onConfirm: handleConfirm,
        onCancel: handleCancel
      }, {
        default: withCtx(() => [
          createVNode(unref(TUIInput), {
            modelValue: password.value,
            "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => password.value = $event),
            type: "number",
            "show-password": "",
            "max-length": "6",
            placeholder: unref(t)("Room.EnterPasswordPlaceholder"),
            onKeyup: withKeys(handleConfirm, ["enter"])
          }, null, 8, ["modelValue", "placeholder"])
        ]),
        _: 1
      }, 8, ["visible", "title"]);
    };
  }
});
export {
  _sfc_main as default
};
