import { defineComponent, ref, onMounted, onUnmounted, createBlock, openBlock, normalizeClass, unref, withCtx } from "vue";
import { useUIKit, IconApplyLittle, IconApplyActive, TUIToast } from "@tencentcloud/uikit-base-component-vue3";
import { useLoginState, useRoomParticipantState, RoomParticipantEvent, DeviceType } from "tuikit-atomicx-vue3/room";
import IconButton from "../base/IconButton.vue2.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  setup(__props) {
    const { t } = useUIKit();
    const { loginUserInfo } = useLoginState();
    const { requestToOpenDevice, cancelOpenDeviceRequest, subscribeEvent, unsubscribeEvent } = useRoomParticipantState();
    const isRaised = ref(false);
    const handleClick = async () => {
      const tempRaised = isRaised.value;
      try {
        if (isRaised.value) {
          await cancelOpenDeviceRequest({ device: DeviceType.Microphone });
          isRaised.value = false;
        } else {
          await requestToOpenDevice({ device: DeviceType.Microphone, timeout: 30 });
          isRaised.value = true;
        }
      } catch (_error) {
        isRaised.value = tempRaised;
        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: DeviceType.Microphone });
        } catch (_error) {
          console.warn("cancelOpenDeviceRequest failed", _error);
        }
      }
    };
    onMounted(() => {
      subscribeEvent(RoomParticipantEvent.onDeviceRequestApproved, onDeviceRequestApproved);
      subscribeEvent(RoomParticipantEvent.onDeviceRequestRejected, onDeviceRequestRejected);
      subscribeEvent(RoomParticipantEvent.onDeviceRequestTimeout, onDeviceRequestTimeout);
      subscribeEvent(RoomParticipantEvent.onAudiencePromotedToParticipant, onAudiencePromotedToParticipant);
    });
    onUnmounted(() => {
      unsubscribeEvent(RoomParticipantEvent.onDeviceRequestApproved, onDeviceRequestApproved);
      unsubscribeEvent(RoomParticipantEvent.onDeviceRequestRejected, onDeviceRequestRejected);
      unsubscribeEvent(RoomParticipantEvent.onDeviceRequestTimeout, onDeviceRequestTimeout);
      unsubscribeEvent(RoomParticipantEvent.onAudiencePromotedToParticipant, onAudiencePromotedToParticipant);
    });
    return (_ctx, _cache) => {
      return openBlock(), createBlock(IconButton, {
        title: !isRaised.value ? unref(t)("RaiseHands.Raise") : unref(t)("RaiseHands.Lower"),
        class: normalizeClass(["raise-hands-button", { "raise-hands-button-raised": isRaised.value }]),
        onClick: handleClick
      }, {
        default: withCtx(() => [
          !isRaised.value ? (openBlock(), createBlock(unref(IconApplyLittle), {
            key: 0,
            size: 24
          })) : (openBlock(), createBlock(unref(IconApplyActive), {
            key: 1,
            size: 24
          }))
        ]),
        _: 1
      }, 8, ["title", "class"]);
    };
  }
});
export {
  _sfc_main as default
};
