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 { TUICore, TUIConstants } from "@tencentcloud/tui-core";
import { EventType } from "../types.mjs";
import TUIRoomEngine__default, { TUIRoomEvents } from "@tencentcloud/tuiroom-engine-js";
import defaultAvatar from "../../assets/imgs/avatar.png.mjs";
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.registerEvent(
      TUIConstants.TUIChat.EVENT.CHAT_STATE_CHANGED,
      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.unregisterExtension(
      TUIConstants.TUIChat.EVENT.CHAT_STATE_CHANGED,
      this
    );
  }
  setChatType(type) {
    this.isRoomType = type;
  }
  async onNotifyEvent(eventName, subKey, options) {
    if (!this.isRoomType) return;
    if (eventName === TUIConstants.TUIChat.EVENT.CHAT_STATE_CHANGED) {
      if (subKey === TUIConstants.TUIChat.EVENT_SUB_KEY.CHAT_OPENED) {
        TUICore.callService({
          serviceName: TUIConstants.TUIChat.SERVICE.NAME,
          method: TUIConstants.TUIChat.SERVICE.METHOD.SET_CHAT_TYPE,
          params: {
            chatType: 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 || defaultAvatar
            }
          ])
        );
        TUICore.callService({
          serviceName: TUIConstants.TUIChat.SERVICE.NAME,
          method: TUIConstants.TUIChat.SERVICE.METHOD.UPDATE_MESSAGE_INFO,
          params: { userInfo: result }
        });
      }
    }
  }
  onLanguageChanged(language) {
    TUICore.notifyEvent(
      TUIConstants.TUITranslate.EVENT.LANGUAGE_CHANGED,
      TUIConstants.TUITranslate.EVENT_SUB_KEY.CHANGE_SUCCESS,
      { language }
    );
  }
  onThemeChanged(theme) {
    const currentTheme = theme === "black" ? THEME.DARK : THEME.LIGHT;
    TUICore.notifyEvent(
      TUIConstants.TUITheme.EVENT.THEME_CHANGED,
      TUIConstants.TUITheme.EVENT_SUB_KEY.CHANGE_SUCCESS,
      { theme: currentTheme }
    );
  }
  onUserNameCardChanged(eventInfo) {
    const { userId, nameCard } = eventInfo.userInfo;
    const result = { [userId]: { nameCard } };
    TUICore.callService({
      serviceName: TUIConstants.TUIChat.SERVICE.NAME,
      method: 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(EventType.LANGUAGE_CHANGED, this.onLanguageChanged);
    this.service.on(EventType.THEME_CHANGED, this.onThemeChanged);
    TUIRoomEngine__default.once("ready", () => {
      var _a;
      (_a = this.service.roomEngine.instance) == null ? void 0 : _a.on(
        TUIRoomEvents.onUserInfoChanged,
        this.onUserNameCardChanged
      );
    });
  }
  unbindEvent() {
    var _a;
    this.service.off(EventType.LANGUAGE_CHANGED, this.onLanguageChanged);
    this.service.off(EventType.THEME_CHANGED, this.onThemeChanged);
    (_a = this.service.roomEngine.instance) == null ? void 0 : _a.off(
      TUIRoomEvents.onUserInfoChanged,
      this.onUserNameCardChanged
    );
  }
};
__publicField(_ChatManager, "instance");
let ChatManager = _ChatManager;
export {
  ChatManager
};
