"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const uikitBaseComponentVue3 = require("@tencentcloud/uikit-base-component-vue3");
const room = require("tuikit-atomicx-vue3/room");
const IconButton = require("../base/IconButton.vue2.js");
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  __name: "index",
  setup(__props) {
    const { t } = uikitBaseComponentVue3.useUIKit();
    const { loginUserInfo } = room.useLoginState();
    const { requestToOpenDevice, cancelOpenDeviceRequest, subscribeEvent, unsubscribeEvent } = room.useRoomParticipantState();
    const isRaised = vue.ref(false);
    const handleClick = async () => {
      const tempRaised = isRaised.value;
      try {
        if (isRaised.value) {
          await cancelOpenDeviceRequest({ device: room.DeviceType.Microphone });
          isRaised.value = false;
        } else {
          await requestToOpenDevice({ device: room.DeviceType.Microphone, timeout: 30 });
          isRaised.value = true;
        }
      } catch (_error) {
        isRaised.value = tempRaised;
        uikitBaseComponentVue3.TUIToast.error({
          message: t("RaiseHands.Failed")
        });
      }
    };
    const onDeviceRequestApproved = () => {
      isRaised.value = false;
    };
    const onDeviceRequestRejected = () => {
      isRaised.value = false;
    };
    const onDeviceRequestTimeout = () => {
      isRaised.value = false;
    };
    const onAudiencePromotedToParticipant = async ({ userInfo }) => {
      var _a;
      if (userInfo.userId === ((_a = loginUserInfo.value) == null ? void 0 : _a.userId)) {
        try {
          await cancelOpenDeviceRequest({ device: room.DeviceType.Microphone });
        } catch (_error) {
          console.warn("cancelOpenDeviceRequest failed", _error);
        }
      }
    };
    vue.onMounted(() => {
      subscribeEvent(room.RoomParticipantEvent.onDeviceRequestApproved, onDeviceRequestApproved);
      subscribeEvent(room.RoomParticipantEvent.onDeviceRequestRejected, onDeviceRequestRejected);
      subscribeEvent(room.RoomParticipantEvent.onDeviceRequestTimeout, onDeviceRequestTimeout);
      subscribeEvent(room.RoomParticipantEvent.onAudiencePromotedToParticipant, onAudiencePromotedToParticipant);
    });
    vue.onUnmounted(() => {
      unsubscribeEvent(room.RoomParticipantEvent.onDeviceRequestApproved, onDeviceRequestApproved);
      unsubscribeEvent(room.RoomParticipantEvent.onDeviceRequestRejected, onDeviceRequestRejected);
      unsubscribeEvent(room.RoomParticipantEvent.onDeviceRequestTimeout, onDeviceRequestTimeout);
      unsubscribeEvent(room.RoomParticipantEvent.onAudiencePromotedToParticipant, onAudiencePromotedToParticipant);
    });
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createBlock(IconButton.default, {
        title: !isRaised.value ? vue.unref(t)("RaiseHands.Raise") : vue.unref(t)("RaiseHands.Lower"),
        class: vue.normalizeClass(["raise-hands-button", { "raise-hands-button-raised": isRaised.value }]),
        onClick: handleClick
      }, {
        default: vue.withCtx(() => [
          !isRaised.value ? (vue.openBlock(), vue.createBlock(vue.unref(uikitBaseComponentVue3.IconApplyLittle), {
            key: 0,
            size: 24
          })) : (vue.openBlock(), vue.createBlock(vue.unref(uikitBaseComponentVue3.IconApplyActive), {
            key: 1,
            size: 24
          }))
        ]),
        _: 1
      }, 8, ["title", "class"]);
    };
  }
});
exports.default = _sfc_main;
