"use strict";
const common_vendor = require("../../../../common/vendor.js");
require("../../../adapter-vue.js");
const TUIKit_components_common_Toast_index = require("../../common/Toast/index.js");
const TUIKit_components_TUIContact_server = require("../server.js");
const TUIKit_components_common_Toast_type = require("../../common/Toast/type.js");
if (!Math) {
  SelectUser();
}
const SelectUser = () => "../../common/SelectUser/index.js";
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
  __name: "index",
  setup(__props) {
    const TUIContactServer = TUIKit_components_TUIContact_server.TUIContactServer.getInstance();
    const TUIConstants = TUIContactServer.constants;
    const needSearch = common_vendor.ref(false);
    const friendList = common_vendor.ref([]);
    const userList = common_vendor.ref([]);
    const TUISearchServer = common_vendor.ref(null);
    const selectOptions = common_vendor.ref({
      isRadio: false,
      isNeedSearch: false,
      title: ""
    });
    const generateSearchServer = (isNeedSearch) => {
      TUISearchServer.value = common_vendor.R.getService(TUIConstants.TUISearch.SERVICE.NAME);
      if (TUISearchServer.value) {
        needSearch.value = isNeedSearch;
      } else {
        common_vendor.index.__f__("warn", "at TUIKit/components/TUIContact/select-friend/index.vue:41", "请添加 TUISearch 组件");
      }
    };
    common_vendor.watchEffect(() => {
      const params = TUIContactServer.getOnCallParams(TUIConstants.TUIContact.SERVICE.METHOD.SELECT_FRIEND);
      selectOptions.value.title = params.title;
      selectOptions.value.isRadio = params.isRadio;
      selectOptions.value.isNeedSearch = params.isNeedSearch;
      if (params.isNeedSearch) {
        generateSearchServer(params.isNeedSearch);
      }
      common_vendor.ts.getFriendList().then((res) => {
        friendList.value = res.data.map((item) => item.profile);
        userList.value = friendList.value;
      }).catch((err) => {
        common_vendor.index.__f__("warn", "at TUIKit/components/TUIContact/select-friend/index.vue:57", "getFriendList error:", err);
      });
    });
    const handleSelectedResult = (memberList) => {
      common_vendor.Jt.update(common_vendor.o.CUSTOM, "isShowSelectFriendComponent", false);
      const callback = TUIContactServer.getOnCallCallback(TUIConstants.TUIContact.SERVICE.METHOD.SELECT_FRIEND);
      callback && callback(memberList);
    };
    const searchFail = () => {
      TUIKit_components_common_Toast_index.Toast({
        message: common_vendor.Wt.t("TUIGroup.该用户不存在"),
        type: TUIKit_components_common_Toast_type.TOAST_TYPE.ERROR
      });
      userList.value = [...friendList.value];
    };
    const handleSearch = async (val) => {
      if (!val) {
        return userList.value = friendList.value;
      }
      try {
        const imResponse = await TUISearchServer.value.searchUser(val);
        if (!imResponse.data[0]) {
          return searchFail();
        }
        userList.value = imResponse.data;
        const searchAllResult = friendList.value.filter((item) => item.userID === imResponse.data[0].userID);
        friendList.value = searchAllResult.length ? friendList.value : [...friendList.value, ...userList.value];
      } catch (error) {
        return searchFail();
      }
    };
    return (_ctx, _cache) => {
      return {
        a: common_vendor.o$1(handleSearch),
        b: common_vendor.o$1(handleSelectedResult),
        c: common_vendor.p({
          isRadio: common_vendor.unref(selectOptions).isRadio,
          isNeedSearch: common_vendor.unref(selectOptions).isNeedSearch,
          title: common_vendor.unref(selectOptions).title,
          userList: common_vendor.unref(userList)
        })
      };
    };
  }
});
wx.createComponent(_sfc_main);
//# sourceMappingURL=../../../../../.sourcemap/mp-weixin/TUIKit/components/TUIContact/select-friend/index.js.map
