"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const vue = require("vue");
const uikitBaseComponentVue3 = require("@tencentcloud/uikit-base-component-vue3");
const room = require("tuikit-atomicx-vue3/room");
function useRoomTips() {
  const { t } = uikitBaseComponentVue3.useUIKit();
  const { currentRoom, subscribeEvent: subscribeRoomEvent, unsubscribeEvent: unsubscribeRoomEvent } = room.useRoomState();
  const {
    localParticipant,
    muteParticipantMessage,
    approveOpenDeviceRequest,
    rejectOpenDeviceRequest,
    subscribeEvent,
    unsubscribeEvent
  } = room.useRoomParticipantState();
  async function onOwnerChanged({ newOwner }) {
    var _a, _b;
    if (newOwner.userId === ((_a = localParticipant.value) == null ? void 0 : _a.userId)) {
      uikitBaseComponentVue3.TUIToast.info({
        message: t("RoomNotifications.BecomeOwner")
      });
    }
    if ((_b = localParticipant.value) == null ? void 0 : _b.isMessageDisabled) {
      await muteParticipantMessage({
        userId: newOwner.userId,
        mute: false
      });
    }
  }
  async function onAdminSet({ userInfo }) {
    var _a;
    if (userInfo.userId === ((_a = localParticipant.value) == null ? void 0 : _a.userId)) {
      uikitBaseComponentVue3.TUIToast.info({
        message: t("RoomNotifications.BecomeAdmin")
      });
    }
  }
  function onAdminRevoked({ userInfo }) {
    var _a;
    if (userInfo.userId === ((_a = localParticipant.value) == null ? void 0 : _a.userId)) {
      uikitBaseComponentVue3.TUIToast.info({
        message: t("RoomNotifications.AdminRevoked")
      });
    }
  }
  function onKickedFromRoom({ reason }) {
    let notice = "";
    switch (reason) {
      case room.KickedOutOfRoomReason.KickedByAdmin:
        notice = t("RoomNotifications.KickedByAdmin");
        break;
      case room.KickedOutOfRoomReason.ReplacedByAnotherDevice:
        notice = t("RoomNotifications.ReplacedByAnotherDevice");
        break;
      case room.KickedOutOfRoomReason.KickedByServer:
        notice = t("RoomNotifications.KickedByServer");
        break;
      case room.KickedOutOfRoomReason.ConnectionTimeout:
        notice = t("RoomNotifications.ConnectionTimeout");
        break;
      case room.KickedOutOfRoomReason.InvalidStatusOnReconnect:
        notice = t("RoomNotifications.InvalidStatusOnReconnect");
        break;
      case room.KickedOutOfRoomReason.RoomLimitExceeded:
        notice = t("RoomNotifications.RoomLimitExceeded");
        break;
      default:
        notice = t("RoomNotifications.DefaultKickedOut");
        break;
    }
    uikitBaseComponentVue3.TUIMessageBox.alert({
      title: t("RoomCommon.Notification"),
      content: notice
    });
  }
  function onParticipantDeviceClosed({ device }) {
    if (device === room.DeviceType.Microphone) {
      uikitBaseComponentVue3.TUIToast.warning({
        message: t("RoomNotifications.MicrophoneClosed")
      });
    }
    if (device === room.DeviceType.Camera) {
      uikitBaseComponentVue3.TUIToast.warning({
        message: t("RoomNotifications.CameraClosed")
      });
    }
    if (device === room.DeviceType.ScreenShare) {
      uikitBaseComponentVue3.TUIToast.warning({
        message: t("RoomNotifications.ScreenShareClosed")
      });
    }
  }
  function onParticipantMessageMuted({ muted }) {
    uikitBaseComponentVue3.TUIToast({
      type: muted ? uikitBaseComponentVue3.TOAST_TYPE.WARNING : uikitBaseComponentVue3.TOAST_TYPE.INFO,
      message: muted ? t("RoomNotifications.MessageMuted") : t("RoomNotifications.MessageUnmuted")
    });
  }
  function onAllDevicesDisabled({ device, disable }) {
    switch (device) {
      case room.DeviceType.Microphone:
        uikitBaseComponentVue3.TUIToast({
          type: disable ? uikitBaseComponentVue3.TOAST_TYPE.WARNING : uikitBaseComponentVue3.TOAST_TYPE.INFO,
          message: disable ? t("RoomNotifications.AllMicrophonesDisabled") : t("RoomNotifications.AllMicrophonesEnabled")
        });
        break;
      case room.DeviceType.Camera:
        uikitBaseComponentVue3.TUIToast({
          type: disable ? uikitBaseComponentVue3.TOAST_TYPE.WARNING : uikitBaseComponentVue3.TOAST_TYPE.INFO,
          message: disable ? t("RoomNotifications.AllCamerasDisabled") : t("RoomNotifications.AllCamerasEnabled")
        });
        break;
      case room.DeviceType.ScreenShare:
        uikitBaseComponentVue3.TUIToast({
          type: disable ? uikitBaseComponentVue3.TOAST_TYPE.WARNING : uikitBaseComponentVue3.TOAST_TYPE.INFO,
          message: disable ? t("RoomNotifications.AllScreenSharesDisabled") : t("RoomNotifications.AllScreenSharesEnabled")
        });
        break;
    }
  }
  function onAllMessagesDisabled({ disable }) {
    uikitBaseComponentVue3.TUIToast({
      type: disable ? uikitBaseComponentVue3.TOAST_TYPE.WARNING : uikitBaseComponentVue3.TOAST_TYPE.INFO,
      message: disable ? t("RoomNotifications.AllMessagesDisabled") : t("RoomNotifications.AllMessagesEnabled")
    });
  }
  const messageBoxMap = /* @__PURE__ */ new Map();
  function onDeviceInvitationReceived({ invitation }) {
    const { deviceType, senderUserId, senderNameCard, senderUserName } = invitation;
    const senderName = senderNameCard || senderUserName || senderUserId;
    let content = "";
    let confirmText = "";
    switch (deviceType) {
      case room.DeviceType.Microphone:
        content = t("RoomNotifications.MicrophoneInvitationReceived", { senderName });
        confirmText = t("RoomNotifications.OpenMicrophone");
        break;
      case room.DeviceType.Camera:
        content = t("RoomNotifications.CameraInvitationReceived", { senderName });
        confirmText = t("RoomNotifications.OpenCamera");
        break;
      case room.DeviceType.ScreenShare:
        content = t("RoomNotifications.ScreenShareInvitationReceived", { senderName });
        confirmText = t("RoomNotifications.OpenScreenShare");
        break;
    }
    const messageBox = uikitBaseComponentVue3.TUIMessageBox.confirm({
      title: t("RoomCommon.Notification"),
      modal: false,
      showClose: false,
      content,
      confirmText,
      cancelText: t("RoomNotifications.KeepOff"),
      callback: async (action) => {
        if (action === "confirm") {
          await approveOpenDeviceRequest({ device: deviceType, userId: senderUserId });
        }
        if (action === "cancel") {
          await rejectOpenDeviceRequest({ device: deviceType, userId: senderUserId });
        }
        messageBoxMap.delete(`${deviceType}-${senderUserId}`);
      }
    });
    messageBoxMap.set(`${deviceType}-${senderUserId}`, messageBox);
  }
  function onDeviceRequestRejected({ request }) {
    var _a;
    const { deviceType } = request;
    if (((_a = currentRoom.value) == null ? void 0 : _a.roomType) === room.RoomType.Webinar) {
      uikitBaseComponentVue3.TUIToast.warning({
        message: t("RoomNotifications.RaiseHandsRequestRejected")
      });
      return;
    }
    switch (deviceType) {
      case room.DeviceType.Microphone:
        uikitBaseComponentVue3.TUIToast.warning({
          message: t("RoomNotifications.DeviceRequestRejected", { deviceType: t("RoomNotifications.Microphone") })
        });
        break;
      case room.DeviceType.Camera:
        uikitBaseComponentVue3.TUIToast.warning({
          message: t("RoomNotifications.DeviceRequestRejected", { deviceType: t("RoomNotifications.Camera") })
        });
        break;
      case room.DeviceType.ScreenShare:
        uikitBaseComponentVue3.TUIToast.warning({
          message: t("RoomNotifications.DeviceRequestRejected", { deviceType: t("RoomNotifications.ScreenShare") })
        });
        break;
    }
  }
  function onDeviceInvitationCancelled({ invitation }) {
    const { deviceType, senderUserId } = invitation;
    const messageBox = messageBoxMap.get(`${deviceType}-${senderUserId}`);
    if (messageBox) {
      messageBox.close();
      messageBoxMap.delete(`${deviceType}-${senderUserId}`);
    }
  }
  function onDeviceInvitationTimeout({ invitation }) {
    const { deviceType, senderUserId } = invitation;
    const messageBox = messageBoxMap.get(`${deviceType}-${senderUserId}`);
    if (messageBox) {
      messageBox.close();
      messageBoxMap.delete(`${deviceType}-${senderUserId}`);
    }
  }
  function onAudiencePromotedToParticipant({ userInfo }) {
    var _a;
    if (userInfo.userId === ((_a = localParticipant.value) == null ? void 0 : _a.userId)) {
      uikitBaseComponentVue3.TUIToast.info({
        message: t("RoomNotifications.YouArePromotedToParticipant")
      });
      return;
    }
    uikitBaseComponentVue3.TUIToast.info({
      message: t("RoomNotifications.AudiencePromotedToParticipant", { userName: userInfo.userName || userInfo.userId })
    });
  }
  function onParticipantDemotedToAudience({ userInfo }) {
  }
  function onRoomEnded() {
    uikitBaseComponentVue3.TUIMessageBox.alert({
      type: "info",
      title: t("Room.Notify"),
      showClose: false,
      modal: false,
      content: t("Room.RoomEnded")
    });
  }
  vue.onMounted(() => {
    subscribeEvent(room.RoomParticipantEvent.onOwnerChanged, onOwnerChanged);
    subscribeEvent(room.RoomParticipantEvent.onAdminSet, onAdminSet);
    subscribeEvent(room.RoomParticipantEvent.onAdminRevoked, onAdminRevoked);
    subscribeEvent(room.RoomParticipantEvent.onKickedFromRoom, onKickedFromRoom);
    subscribeEvent(room.RoomParticipantEvent.onParticipantDeviceClosed, onParticipantDeviceClosed);
    subscribeEvent(room.RoomParticipantEvent.onParticipantMessageMuted, onParticipantMessageMuted);
    subscribeEvent(room.RoomParticipantEvent.onAllDevicesDisabled, onAllDevicesDisabled);
    subscribeEvent(room.RoomParticipantEvent.onAllMessagesDisabled, onAllMessagesDisabled);
    subscribeEvent(room.RoomParticipantEvent.onDeviceInvitationReceived, onDeviceInvitationReceived);
    subscribeEvent(room.RoomParticipantEvent.onDeviceRequestRejected, onDeviceRequestRejected);
    subscribeEvent(room.RoomParticipantEvent.onDeviceInvitationCancelled, onDeviceInvitationCancelled);
    subscribeEvent(room.RoomParticipantEvent.onDeviceInvitationTimeout, onDeviceInvitationTimeout);
    subscribeEvent(room.RoomParticipantEvent.onAudiencePromotedToParticipant, onAudiencePromotedToParticipant);
    subscribeEvent(room.RoomParticipantEvent.onParticipantDemotedToAudience, onParticipantDemotedToAudience);
    subscribeRoomEvent(room.RoomEvent.onRoomEnded, onRoomEnded);
  });
  vue.onUnmounted(() => {
    unsubscribeEvent(room.RoomParticipantEvent.onOwnerChanged, onOwnerChanged);
    unsubscribeEvent(room.RoomParticipantEvent.onAdminSet, onAdminSet);
    unsubscribeEvent(room.RoomParticipantEvent.onAdminRevoked, onAdminRevoked);
    unsubscribeEvent(room.RoomParticipantEvent.onKickedFromRoom, onKickedFromRoom);
    unsubscribeEvent(room.RoomParticipantEvent.onParticipantDeviceClosed, onParticipantDeviceClosed);
    unsubscribeEvent(room.RoomParticipantEvent.onParticipantMessageMuted, onParticipantMessageMuted);
    unsubscribeEvent(room.RoomParticipantEvent.onAllDevicesDisabled, onAllDevicesDisabled);
    unsubscribeEvent(room.RoomParticipantEvent.onAllMessagesDisabled, onAllMessagesDisabled);
    unsubscribeEvent(room.RoomParticipantEvent.onDeviceInvitationReceived, onDeviceInvitationReceived);
    unsubscribeEvent(room.RoomParticipantEvent.onDeviceRequestRejected, onDeviceRequestRejected);
    unsubscribeEvent(room.RoomParticipantEvent.onDeviceInvitationCancelled, onDeviceInvitationCancelled);
    unsubscribeEvent(room.RoomParticipantEvent.onDeviceInvitationTimeout, onDeviceInvitationTimeout);
    unsubscribeEvent(room.RoomParticipantEvent.onAudiencePromotedToParticipant, onAudiencePromotedToParticipant);
    unsubscribeEvent(room.RoomParticipantEvent.onParticipantDemotedToAudience, onParticipantDemotedToAudience);
    unsubscribeRoomEvent(room.RoomEvent.onRoomEnded, onRoomEnded);
  });
}
exports.useRoomTips = useRoomTips;
