"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 TUICore = require("@tencentcloud/tui-core");
const types = require("../types.js");
const TUIRoomEngine = require("@tencentcloud/tuiroom-engine-js");
const avatar = require("../../assets/imgs/avatar.png.js");
const THEME = {
  LIGHT: "LIGHT",
  DARK: "DARK"
};
const _ChatManager = class _ChatManager {
  constructor(service) {
    __publicField(this, "service");
    __publicField(this, "isRoomType", false);
    this.service = service;
    this.bindEventContext();
    this.bindEvent();
    TUICore.TUICore.registerEvent(
      TUICore.TUIConstants.TUIChat.EVENT.CHAT_STATE_CHANGED,
      TUICore.TUIConstants.TUIChat.EVENT_SUB_KEY.CHAT_OPENED,
      this
    );
  }
  static getInstance(ctx) {
    if (!_ChatManager.instance) {
      _ChatManager.instance = new _ChatManager(ctx);
    }
    return _ChatManager.instance;
  }
  static destroyInstance() {
    if (!_ChatManager.instance) return;
    _ChatManager.instance = void 0;
  }
  dispose() {
    this.unbindEvent();
  }
  reset() {
    TUICore.TUICore.unregisterExtension(
      TUICore.TUIConstants.TUIChat.EVENT.CHAT_STATE_CHANGED,
      this
    );
  }
  setChatType(type) {
    this.isRoomType = type;
  }
  async onNotifyEvent(eventName, subKey, options) {
    if (!this.isRoomType) return;
    if (eventName === TUICore.TUIConstants.TUIChat.EVENT.CHAT_STATE_CHANGED) {
      if (subKey === TUICore.TUIConstants.TUIChat.EVENT_SUB_KEY.CHAT_OPENED) {
        TUICore.TUICore.callService({
          serviceName: TUICore.TUIConstants.TUIChat.SERVICE.NAME,
          method: TUICore.TUIConstants.TUIChat.SERVICE.METHOD.SET_CHAT_TYPE,
          params: {
            chatType: TUICore.TUIConstants.TUIChat.TYPE.ROOM
          }
        });
        const result = Object.fromEntries(
          this.service.roomStore.userList.map((item) => [
            item.userId,
            {
              nick: item.userName,
              nameCard: item.nameCard,
              avatar: item.avatarUrl || avatar.default
            }
          ])
        );
        TUICore.TUICore.callService({
          serviceName: TUICore.TUIConstants.TUIChat.SERVICE.NAME,
          method: TUICore.TUIConstants.TUIChat.SERVICE.METHOD.UPDATE_MESSAGE_INFO,
          params: { userInfo: result }
        });
      }
    }
  }
  onLanguageChanged(language) {
    TUICore.TUICore.notifyEvent(
      TUICore.TUIConstants.TUITranslate.EVENT.LANGUAGE_CHANGED,
      TUICore.TUIConstants.TUITranslate.EVENT_SUB_KEY.CHANGE_SUCCESS,
      { language }
    );
  }
  onThemeChanged(theme) {
    const currentTheme = theme === "black" ? THEME.DARK : THEME.LIGHT;
    TUICore.TUICore.notifyEvent(
      TUICore.TUIConstants.TUITheme.EVENT.THEME_CHANGED,
      TUICore.TUIConstants.TUITheme.EVENT_SUB_KEY.CHANGE_SUCCESS,
      { theme: currentTheme }
    );
  }
  onUserNameCardChanged(eventInfo) {
    const { userId, nameCard } = eventInfo.userInfo;
    const result = { [userId]: { nameCard } };
    TUICore.TUICore.callService({
      serviceName: TUICore.TUIConstants.TUIChat.SERVICE.NAME,
      method: TUICore.TUIConstants.TUIChat.SERVICE.METHOD.UPDATE_MESSAGE_INFO,
      params: { userInfo: result }
    });
  }
  bindEventContext() {
    this.onLanguageChanged = this.onLanguageChanged.bind(this);
    this.onThemeChanged = this.onThemeChanged.bind(this);
    this.onUserNameCardChanged = this.onUserNameCardChanged.bind(this);
  }
  bindEvent() {
    this.service.on(types.EventType.LANGUAGE_CHANGED, this.onLanguageChanged);
    this.service.on(types.EventType.THEME_CHANGED, this.onThemeChanged);
    TUIRoomEngine.once("ready", () => {
      var _a;
      (_a = this.service.roomEngine.instance) == null ? void 0 : _a.on(
        TUIRoomEngine.TUIRoomEvents.onUserInfoChanged,
        this.onUserNameCardChanged
      );
    });
  }
  unbindEvent() {
    var _a;
    this.service.off(types.EventType.LANGUAGE_CHANGED, this.onLanguageChanged);
    this.service.off(types.EventType.THEME_CHANGED, this.onThemeChanged);
    (_a = this.service.roomEngine.instance) == null ? void 0 : _a.off(
      TUIRoomEngine.TUIRoomEvents.onUserInfoChanged,
      this.onUserNameCardChanged
    );
  }
};
__publicField(_ChatManager, "instance");
let ChatManager = _ChatManager;
exports.ChatManager = ChatManager;
