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);
import { TUIConferenceInvitationManagerEvents, TUIInvitationRejectedReason } from "@tencentcloud/tuiroom-engine-js";
export * from "@tencentcloud/tuiroom-engine-js";
import { EventType } from "../types.mjs";
import mitt from "mitt";
class ConferenceInvitationManager {
  constructor(service) {
    __publicField(this, "service");
    __publicField(this, "emitter", mitt());
    __publicField(this, "isBeingInvited", false);
    this.service = service;
    this.bindEventContext();
    this.service.on(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(
      TUIConferenceInvitationManagerEvents.onReceiveInvitation,
      this.onReceiveInvitation
    );
    conferenceInvitationManager.on(
      TUIConferenceInvitationManagerEvents.onInvitationHandledByOtherDevice,
      this.onInvitationHandledByOtherDevice
    );
    conferenceInvitationManager.on(
      TUIConferenceInvitationManagerEvents.onInvitationAccepted,
      this.onInvitationAccepted
    );
    conferenceInvitationManager.on(
      TUIConferenceInvitationManagerEvents.onInvitationRejected,
      this.onInvitationRejected
    );
    conferenceInvitationManager.on(
      TUIConferenceInvitationManagerEvents.onInvitationTimeout,
      this.onInvitationTimeout
    );
    conferenceInvitationManager.on(
      TUIConferenceInvitationManagerEvents.onInvitationAdded,
      this.onInvitationAdded
    );
    conferenceInvitationManager.on(
      TUIConferenceInvitationManagerEvents.onInvitationStatusChanged,
      this.onInvitationStatusChanged
    );
    conferenceInvitationManager.on(
      TUIConferenceInvitationManagerEvents.onInvitationRevokedByAdmin,
      this.onInvitationRevokedByAdmin
    );
  }
  unbindEvent() {
    var _a;
    const conferenceInvitationManager = (_a = this.service.roomEngine) == null ? void 0 : _a.instance.getConferenceListManager();
    conferenceInvitationManager.off(
      TUIConferenceInvitationManagerEvents.onReceiveInvitation,
      this.onReceiveInvitation
    );
    conferenceInvitationManager.off(
      TUIConferenceInvitationManagerEvents.onInvitationHandledByOtherDevice,
      this.onInvitationHandledByOtherDevice
    );
    conferenceInvitationManager.off(
      TUIConferenceInvitationManagerEvents.onInvitationAccepted,
      this.onInvitationAccepted
    );
    conferenceInvitationManager.off(
      TUIConferenceInvitationManagerEvents.onInvitationRejected,
      this.onInvitationRejected
    );
    conferenceInvitationManager.off(
      TUIConferenceInvitationManagerEvents.onInvitationTimeout,
      this.onInvitationTimeout
    );
    conferenceInvitationManager.off(
      TUIConferenceInvitationManagerEvents.onInvitationAdded,
      this.onInvitationAdded
    );
    conferenceInvitationManager.off(
      TUIConferenceInvitationManagerEvents.onInvitationStatusChanged,
      this.onInvitationStatusChanged
    );
    conferenceInvitationManager.off(
      TUIConferenceInvitationManagerEvents.onInvitationRevokedByAdmin,
      this.onInvitationRevokedByAdmin
    );
  }
  onReceiveInvitation(data) {
    if (this.isBeingInvited || this.service.roomStore.localUser.isInRoom) {
      const reason = this.isBeingInvited ? TUIInvitationRejectedReason.kRejectToEnter : TUIInvitationRejectedReason.kInOtherConference;
      this.reject({ roomId: data.roomInfo.roomId, reason });
    } else {
      this.isBeingInvited = true;
      this.emit(TUIConferenceInvitationManagerEvents.onReceiveInvitation, data);
    }
  }
  onInvitationHandledByOtherDevice(data) {
    this.emit(
      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(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(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(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(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(
      TUIConferenceInvitationManagerEvents.onInvitationStatusChanged,
      data
    );
  }
  onInvitationRevokedByAdmin(data) {
    this.emit(
      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));
  }
}
export {
  ConferenceInvitationManager
};
