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 { TUIStore, StoreName, TUIChatService } from "@tencentcloud/chat-uikit-engine";
import ChatConfig from "./config.mjs";
class TUIChatServer {
  constructor() {
    __publicField(this, "currentConversationID", "");
    __publicField(this, "currentMessageList", []);
    TUICore.registerService(TUIConstants.TUIChat.SERVICE.NAME, this);
    TUICore.registerEvent(TUIConstants.TUITheme.EVENT.THEME_CHANGED, TUIConstants.TUITheme.EVENT_SUB_KEY.CHANGE_SUCCESS, this);
    TUIStore.watch(StoreName.CONV, {
      currentConversationID: (id) => {
        this.currentConversationID = id;
      }
    });
  }
  onCall(method, params, callback) {
    let message;
    switch (method) {
      case TUIConstants.TUIChat.SERVICE.METHOD.UPDATE_MESSAGE_LIST:
        message = params.message;
        if ((message == null ? void 0 : message.conversationID) === this.currentConversationID) {
          TUIChatService.updateMessageList([message], "push");
        }
        break;
      case TUIConstants.TUIChat.SERVICE.METHOD.SEND_CUSTOM_MESSAGE:
        TUIChatService.sendCustomMessage(params).then((res) => {
          callback && callback(res);
        });
        break;
      case TUIConstants.TUIChat.SERVICE.METHOD.SEND_TEXT_MESSAGE:
        TUIChatService.sendTextMessage(params).then((res) => {
          callback && callback(res);
        });
        break;
      case TUIConstants.TUIChat.SERVICE.METHOD.SET_CHAT_TYPE:
        ChatConfig.setChatType(params == null ? void 0 : params.chatType);
        break;
      case TUIConstants.TUIChat.SERVICE.METHOD.CLOSE_MESSAGE_POP_MENU:
        TUIStore.update(StoreName.CUSTOM, "isShowMessagePopMenu", false);
        break;
      case TUIConstants.TUIChat.SERVICE.METHOD.UPDATE_MESSAGE_INFO:
        TUIChatService.updateMessageInfo(params == null ? void 0 : params.userInfo);
        break;
    }
  }
  /**
  * Listen for the success notification of Theme changed
  */
  onNotifyEvent(eventName, subKey, params) {
    if (eventName === TUIConstants.TUITheme.EVENT.THEME_CHANGED) {
      switch (subKey) {
        case TUIConstants.TUITheme.EVENT_SUB_KEY.CHANGE_SUCCESS:
          if (params == null ? void 0 : params.theme) {
            const theme = params.theme || "";
            ChatConfig.setTheme(theme.toLowerCase());
          }
          break;
      }
    }
  }
}
export {
  TUIChatServer as default
};
