"use strict";
const common_vendor = require("../../../common/vendor.js");
const TUIKit_constant = require("../../constant.js");
const TUIKit_utils_env = require("../../utils/env.js");
const common_assets = require("../../../common/assets.js");
const TUIKit_utils_enableSampleTaskStatus = require("../../utils/enableSampleTaskStatus.js");
class TUIConversationServer {
  constructor() {
    this.hideConversationHeader = () => {
      common_vendor.Jt.update(common_vendor.o.CUSTOM, "isShowConversationHeader", false);
    };
    common_vendor.R.registerService(common_vendor.E.TUIConversation.SERVICE.NAME, this);
    common_vendor.R.registerExtension(common_vendor.E.TUISearch.EXTENSION.SEARCH_MORE.EXT_ID, this);
    this.onCallParamsMap = /* @__PURE__ */ new Map();
    this.onCallCallbackMap = /* @__PURE__ */ new Map();
    this.constants = common_vendor.E;
  }
  static getInstance() {
    if (!TUIConversationServer.instance) {
      TUIConversationServer.instance = new TUIConversationServer();
    }
    return TUIConversationServer.instance;
  }
  getOnCallParams(method) {
    return this.onCallParamsMap.get(method);
  }
  getOnCallCallback(method) {
    return this.onCallCallbackMap.get(method);
  }
  onCall(method, params, callback) {
    this.onCallParamsMap.set(method, params);
    this.onCallCallbackMap.set(method, callback);
    switch (method) {
      case common_vendor.E.TUIConversation.SERVICE.METHOD.CREATE_CONVERSATION:
        this.createConversation(params);
        break;
      case common_vendor.E.TUIConversation.SERVICE.METHOD.HIDE_CONVERSATION_HEADER:
        this.hideConversationHeader();
        break;
    }
  }
  onGetExtension(extensionID) {
    if (extensionID === common_vendor.E.TUISearch.EXTENSION.SEARCH_MORE.EXT_ID) {
      const list = [
        {
          weight: 100,
          icon: common_assets.C2C,
          text: common_vendor.Wt.t("TUIConversation.发起单聊"),
          data: {
            name: TUIKit_constant.CONV_CREATE_TYPE.TYPEC2C
          },
          listener: {
            onClicked: this.createConversation.bind(this)
          }
        },
        {
          weight: 100,
          icon: common_assets.createGroup,
          text: common_vendor.Wt.t("TUIConversation.发起群聊"),
          data: {
            name: TUIKit_constant.CONV_CREATE_TYPE.TYPEGROUP
          },
          listener: {
            onClicked: this.createConversation.bind(this)
          }
        }
      ];
      return list;
    }
  }
  createConversation(item) {
    common_vendor.R.callService({
      serviceName: common_vendor.E.TUIContact.SERVICE.NAME,
      method: common_vendor.E.TUIContact.SERVICE.METHOD.SELECT_FRIEND,
      params: {
        title: item.text,
        isRadio: item.data.name !== TUIKit_constant.CONV_CREATE_TYPE.TYPEGROUP,
        isNeedSearch: !common_vendor.Jt.getData(common_vendor.o.APP, "isOfficial")
      },
      callback: async (memberList) => {
        if (!memberList || memberList.length === 0) {
          return this.routerForward(null);
        }
        if (item.data.name === TUIKit_constant.CONV_CREATE_TYPE.TYPEGROUP) {
          this.createGroup(memberList);
        } else {
          const { userID } = memberList[0];
          await this.generateConversation(`C2C${userID}`);
          this.routerForward(`C2C${userID}`);
        }
      }
    });
  }
  createGroup(memberList) {
    common_vendor.R.callService({
      serviceName: common_vendor.E.TUIGroup.SERVICE.NAME,
      method: common_vendor.E.TUIGroup.SERVICE.METHOD.CREATE_GROUP,
      params: {
        title: common_vendor.Wt.t("TUIConversation.发起群聊"),
        memberList
      },
      callback: async (group) => {
        let conversationID = null;
        if (group) {
          const { groupID } = group;
          await this.generateConversation(`GROUP${groupID}`);
          conversationID = `GROUP${groupID}`;
        }
        this.routerForward(conversationID);
      }
    });
  }
  async routerForward(conversationID) {
    var _a, _b;
    if (TUIKit_utils_env.isUniFrameWork) {
      await ((_a = common_vendor.i) == null ? void 0 : _a.reLaunch({
        url: "/TUIKit/components/TUIConversation/index"
      }));
      if (conversationID) {
        (_b = common_vendor.i) == null ? void 0 : _b.navigateTo({
          url: "/TUIKit/components/TUIChat/index"
        });
      }
    }
  }
  generateConversation(conversationID) {
    common_vendor.Xt.switchConversation(conversationID).then(() => {
      if (conversationID.startsWith("GROUP")) {
        TUIKit_utils_enableSampleTaskStatus.enableSampleTaskStatus("groupChat");
      }
      common_vendor.index.__f__("warn", "at TUIKit/components/TUIConversation/server.ts:153", "打开会话成功");
    }).catch((err) => {
      common_vendor.index.__f__("warn", "at TUIKit/components/TUIConversation/server.ts:156", "打开会话失败", err.code, err.msg);
    });
  }
}
exports.TUIConversationServer = TUIConversationServer;
//# sourceMappingURL=../../../../.sourcemap/mp-weixin/TUIKit/components/TUIConversation/server.js.map
