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 TUIRoomEngine__default, { TUIVideoStreamType, TUIRoomEvents } from "@tencentcloud/tuiroom-engine-js";
class UserManager {
  constructor(service) {
    __publicField(this, "service");
    __publicField(this, "onUserVideoStateChanged", (eventInfo) => {
      const { userId, streamType, hasVideo } = eventInfo;
      let userInfo = this.service.roomStore.userInfoObj[userId];
      if (!userInfo && hasVideo) {
        this.service.roomStore.addUserInfo({ userId, isInRoom: true });
      }
      userInfo = this.service.roomStore.userInfoObj[userId];
      if (!userInfo) {
        return;
      }
      const updateInfo = streamType === TUIVideoStreamType.kScreenStream ? { hasScreenStream: hasVideo } : { hasVideoStream: hasVideo };
      this.service.roomStore.updateUserInfo({ userId, ...updateInfo });
      if (streamType === TUIVideoStreamType.kCameraStream || streamType === TUIVideoStreamType.kScreenStream && hasVideo) {
        const streamInfo = this.service.roomStore.streamInfoObj[`${userId}_${streamType}`];
        if (!streamInfo) {
          this.service.roomStore.addStreamInfo(userId, streamType);
        }
        this.service.roomStore.updateStreamInfo({
          userId,
          streamType,
          hasVideoStream: hasVideo
        });
      } else if (streamType === TUIVideoStreamType.kScreenStream && !hasVideo) {
        this.service.roomStore.removeStreamInfo(userId, streamType);
      }
    });
    this.service = service;
    this.bindRoomEngineEvents();
  }
  async setSelfInfo(options) {
    const { avatarUrl, userName } = await TUIRoomEngine__default.getSelfInfo();
    const info = {
      userName: options.userName || userName,
      avatarUrl: options.avatarUrl || avatarUrl
    };
    this.service.basicStore.setBasicInfo(info);
    return TUIRoomEngine__default.setSelfInfo(info);
  }
  async setCustomInfoForUser(options) {
    const roomEngine = this.service.roomEngine.instance;
    return roomEngine == null ? void 0 : roomEngine.setCustomInfoForUser(options);
  }
  getDisplayName(options) {
    const { nameCard, userName, userId } = options;
    return nameCard || userName || userId;
  }
  setLocalUser(userInfo) {
    this.service.roomStore.addUserInfo(userInfo);
    this.service.roomStore.addStreamInfo(
      userInfo.userId,
      TUIVideoStreamType.kCameraStream
    );
  }
  onRemoteUserEnterRoom(eventInfo) {
    const { userInfo } = eventInfo;
    this.service.roomStore.addUserInfo(
      Object.assign(userInfo, { isInRoom: true })
    );
    if (this.service.roomStore.isFreeSpeakMode) {
      this.service.roomStore.addStreamInfo(
        userInfo.userId,
        TUIVideoStreamType.kCameraStream
      );
    }
  }
  onRemoteUserLeaveRoom(eventInfo) {
    const { userId } = eventInfo.userInfo;
    this.service.roomStore.removeUserInfo(userId);
    this.service.roomStore.removeStreamInfo(
      userId,
      TUIVideoStreamType.kCameraStream
    );
    this.service.roomStore.removeStreamInfo(
      userId,
      TUIVideoStreamType.kScreenStream
    );
  }
  onSeatListChanged(eventInfo) {
    const { seatedList, leftList } = eventInfo;
    seatedList.forEach((seat) => {
      const { userId } = seat;
      const user = this.service.roomStore.userInfoObj[userId];
      if (user) {
        this.service.roomStore.updateUserInfo({ userId, onSeat: true });
      } else {
        this.service.roomStore.addUserInfo({
          userId,
          onSeat: true,
          isInRoom: true
        });
      }
      this.service.roomStore.addStreamInfo(
        userId,
        TUIVideoStreamType.kCameraStream
      );
    });
    leftList == null ? void 0 : leftList.forEach((seat) => {
      const { userId } = seat;
      const user = this.service.roomStore.userInfoObj[userId];
      if (user) {
        Object.assign(user, { onSeat: false });
        this.service.roomStore.updateUserInfo({ userId, onSeat: false });
      }
      this.service.roomStore.removeStreamInfo(
        userId,
        TUIVideoStreamType.kCameraStream
      );
      this.service.roomStore.removeStreamInfo(
        userId,
        TUIVideoStreamType.kScreenStream
      );
    });
  }
  onUserAudioStateChanged(eventInfo) {
    const { userId, hasAudio } = eventInfo;
    let userInfo = this.service.roomStore.userInfoObj[userId];
    if (!userInfo && hasAudio) {
      this.service.roomStore.addUserInfo({ userId, isInRoom: true });
    }
    userInfo = this.service.roomStore.userInfoObj[userId];
    if (!userInfo) {
      return;
    }
    this.service.roomStore.updateUserInfo({ userId, hasAudioStream: hasAudio });
    const streamInfo = this.service.roomStore.streamInfoObj[`${userId}_${TUIVideoStreamType.kCameraStream}`];
    if (!streamInfo) {
      this.service.roomStore.addStreamInfo(
        userId,
        TUIVideoStreamType.kCameraStream
      );
    }
    this.service.roomStore.updateStreamInfo({
      userId,
      streamType: TUIVideoStreamType.kCameraStream,
      hasAudioStream: hasAudio
    });
  }
  bindRoomEngineEvents() {
    TUIRoomEngine__default.once("ready", () => {
      var _a, _b, _c, _d, _e;
      (_a = this.service.roomEngine.instance) == null ? void 0 : _a.on(
        TUIRoomEvents.onRemoteUserEnterRoom,
        this.onRemoteUserEnterRoom.bind(this)
      );
      (_b = this.service.roomEngine.instance) == null ? void 0 : _b.on(
        TUIRoomEvents.onRemoteUserLeaveRoom,
        this.onRemoteUserLeaveRoom.bind(this)
      );
      (_c = this.service.roomEngine.instance) == null ? void 0 : _c.on(
        TUIRoomEvents.onSeatListChanged,
        this.onSeatListChanged.bind(this)
      );
      (_d = this.service.roomEngine.instance) == null ? void 0 : _d.on(
        TUIRoomEvents.onUserVideoStateChanged,
        this.onUserVideoStateChanged.bind(this)
      );
      (_e = this.service.roomEngine.instance) == null ? void 0 : _e.on(
        TUIRoomEvents.onUserAudioStateChanged,
        this.onUserAudioStateChanged.bind(this)
      );
    });
  }
  unbindRoomEngineEvents() {
    var _a, _b, _c, _d, _e;
    (_a = this.service.roomEngine.instance) == null ? void 0 : _a.off(
      TUIRoomEvents.onRemoteUserEnterRoom,
      this.onRemoteUserEnterRoom
    );
    (_b = this.service.roomEngine.instance) == null ? void 0 : _b.off(
      TUIRoomEvents.onRemoteUserLeaveRoom,
      this.onRemoteUserLeaveRoom
    );
    (_c = this.service.roomEngine.instance) == null ? void 0 : _c.off(
      TUIRoomEvents.onSeatListChanged,
      this.onSeatListChanged
    );
    (_d = this.service.roomEngine.instance) == null ? void 0 : _d.off(
      TUIRoomEvents.onUserVideoStateChanged,
      this.onUserVideoStateChanged
    );
    (_e = this.service.roomEngine.instance) == null ? void 0 : _e.off(
      TUIRoomEvents.onUserAudioStateChanged,
      this.onUserAudioStateChanged
    );
  }
}
export {
  UserManager
};
