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