"use strict";
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const TUIRoomEngine = require("@tencentcloud/tuiroom-engine-js");
const types = require("../types.js");
const mitt = require("mitt");
class ConferenceInvitationManager {
  constructor(service) {
    __publicField(this, "service");
    __publicField(this, "emitter", mitt());
    __publicField(this, "isBeingInvited", false);
    this.service = service;
    this.bindEventContext();
    this.service.on(types.EventType.SERVICE_READY, () => {
      this.bindEvent();
    });
  }
  dispose() {
    this.unbindEvent();
  }
  on(eventType, callback) {
    this.emitter.on(eventType, callback);
  }
  off(eventType, callback) {
    this.emitter.off(eventType, callback);
  }
  emit(eventType, data) {
    this.emitter.emit(eventType, data);
  }
  bindEventContext() {
    this.onReceiveInvitation = this.onReceiveInvitation.bind(this);
    this.onInvitationHandledByOtherDevice = this.onInvitationHandledByOtherDevice.bind(this);
    this.onInvitationAccepted = this.onInvitationAccepted.bind(this);
    this.onInvitationRejected = this.onInvitationRejected.bind(this);
    this.onInvitationTimeout = this.onInvitationTimeout.bind(this);
    this.onInvitationAdded = this.onInvitationAdded.bind(this);
    this.onInvitationStatusChanged = this.onInvitationStatusChanged.bind(this);
    this.onInvitationRevokedByAdmin = this.onInvitationRevokedByAdmin.bind(this);
  }
  bindEvent() {
    var _a;
    const conferenceInvitationManager = (_a = this.service.roomEngine) == null ? void 0 : _a.instance.getConferenceInvitationManager();
    conferenceInvitationManager.on(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onReceiveInvitation,
      this.onReceiveInvitation
    );
    conferenceInvitationManager.on(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationHandledByOtherDevice,
      this.onInvitationHandledByOtherDevice
    );
    conferenceInvitationManager.on(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationAccepted,
      this.onInvitationAccepted
    );
    conferenceInvitationManager.on(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationRejected,
      this.onInvitationRejected
    );
    conferenceInvitationManager.on(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationTimeout,
      this.onInvitationTimeout
    );
    conferenceInvitationManager.on(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationAdded,
      this.onInvitationAdded
    );
    conferenceInvitationManager.on(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationStatusChanged,
      this.onInvitationStatusChanged
    );
    conferenceInvitationManager.on(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationRevokedByAdmin,
      this.onInvitationRevokedByAdmin
    );
  }
  unbindEvent() {
    var _a;
    const conferenceInvitationManager = (_a = this.service.roomEngine) == null ? void 0 : _a.instance.getConferenceListManager();
    conferenceInvitationManager.off(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onReceiveInvitation,
      this.onReceiveInvitation
    );
    conferenceInvitationManager.off(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationHandledByOtherDevice,
      this.onInvitationHandledByOtherDevice
    );
    conferenceInvitationManager.off(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationAccepted,
      this.onInvitationAccepted
    );
    conferenceInvitationManager.off(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationRejected,
      this.onInvitationRejected
    );
    conferenceInvitationManager.off(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationTimeout,
      this.onInvitationTimeout
    );
    conferenceInvitationManager.off(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationAdded,
      this.onInvitationAdded
    );
    conferenceInvitationManager.off(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationStatusChanged,
      this.onInvitationStatusChanged
    );
    conferenceInvitationManager.off(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationRevokedByAdmin,
      this.onInvitationRevokedByAdmin
    );
  }
  onReceiveInvitation(data) {
    if (this.isBeingInvited || this.service.roomStore.localUser.isInRoom) {
      const reason = this.isBeingInvited ? TUIRoomEngine.TUIInvitationRejectedReason.kRejectToEnter : TUIRoomEngine.TUIInvitationRejectedReason.kInOtherConference;
      this.reject({ roomId: data.roomInfo.roomId, reason });
    } else {
      this.isBeingInvited = true;
      this.emit(TUIRoomEngine.TUIConferenceInvitationManagerEvents.onReceiveInvitation, data);
    }
  }
  onInvitationHandledByOtherDevice(data) {
    this.emit(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationHandledByOtherDevice,
      data
    );
  }
  onInvitationAccepted(data) {
    const { userId, userName, avatarUrl } = data.invitation.invitee;
    const { status } = data.invitation;
    const inviteeInfo = { userId, userName, avatarUrl, status };
    this.service.roomStore.addRemoteUser(inviteeInfo);
    this.emit(TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationAccepted, data);
  }
  onInvitationRejected(data) {
    const { userId, userName, avatarUrl } = data.invitation.invitee;
    const { status } = data.invitation;
    const inviteeInfo = { userId, userName, avatarUrl, status };
    this.service.roomStore.updateInviteeList([inviteeInfo]);
    this.emit(TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationRejected, data);
  }
  onInvitationTimeout(data) {
    const { userId, userName, avatarUrl } = data.invitation.invitee;
    const { status } = data.invitation;
    const inviteeInfo = { userId, userName, avatarUrl, status };
    this.service.roomStore.updateInviteeList([inviteeInfo]);
    this.isBeingInvited = false;
    this.emit(TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationTimeout, data);
  }
  onInvitationAdded(data) {
    const { userId, userName, avatarUrl } = data.invitation.invitee;
    const { status } = data.invitation;
    const inviteeInfo = { userId, userName, avatarUrl, status };
    this.service.roomStore.updateInviteeList([inviteeInfo]);
    this.emit(TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationAdded, data);
  }
  onInvitationStatusChanged(data) {
    const { userId, userName, avatarUrl } = data.invitation.invitee;
    const { status } = data.invitation;
    const inviteeInfo = { userId, userName, avatarUrl, status };
    this.service.roomStore.updateInviteeList([inviteeInfo]);
    this.emit(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationStatusChanged,
      data
    );
  }
  onInvitationRevokedByAdmin(data) {
    this.emit(
      TUIRoomEngine.TUIConferenceInvitationManagerEvents.onInvitationRevokedByAdmin,
      data
    );
  }
  async inviteUsers(options) {
    var _a;
    const { userIdList } = options;
    const inviteParams = {
      roomId: this.service.basicStore.roomId,
      timeout: 60,
      userIdList,
      extensionInfo: ""
    };
    return await ((_a = this.service.roomEngine.instance) == null ? void 0 : _a.getConferenceInvitationManager().inviteUsers(inviteParams));
  }
  async cancelInvitation(options) {
    var _a;
    return await ((_a = this.service.roomEngine.instance) == null ? void 0 : _a.getConferenceInvitationManager().cancelInvitation(options));
  }
  async accept(options) {
    var _a;
    this.isBeingInvited = false;
    return await ((_a = this.service.roomEngine.instance) == null ? void 0 : _a.getConferenceInvitationManager().accept(options));
  }
  async reject(options) {
    var _a;
    this.isBeingInvited = false;
    return await ((_a = this.service.roomEngine.instance) == null ? void 0 : _a.getConferenceInvitationManager().reject(options));
  }
  async getInvitationList(options) {
    var _a;
    return await ((_a = this.service.roomEngine.instance) == null ? void 0 : _a.getConferenceInvitationManager().getInvitationList(options));
  }
}
exports.ConferenceInvitationManager = ConferenceInvitationManager;
Object.keys(TUIRoomEngine).forEach((k) => {
  if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
    enumerable: true,
    get: () => TUIRoomEngine[k]
  });
});
