"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_TUIGroup_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_TUIGroup_server.TUIGroupServer.getInstance();
    const TUIConstants = TUIContactServer.constants;
    const needSearch = common_vendor.ref(false);
    const memberList = common_vendor.ref([]);
    const userList = common_vendor.ref([]);
    const group = common_vendor.ref({});
    const selectOptions = common_vendor.ref({
      groupID: "",
      isRadio: false,
      isNeedSearch: false,
      title: "",
      filterUserIDList: []
    });
    const TUISearchServer = common_vendor.ref(null);
    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/TUIGroup/select-member/index.vue:48", "请添加 TUISearch 组件");
      }
    };
    common_vendor.watchEffect(() => {
      const params = TUIContactServer.getOnCallParams(TUIConstants.TUIContact.SERVICE.METHOD.SELECT_FRIEND);
      selectOptions.value.groupID = params.groupID;
      selectOptions.value.title = params.title;
      selectOptions.value.isRadio = params.isRadio;
      selectOptions.value.isNeedSearch = params.isNeedSearch;
      selectOptions.value.filterUserIDList = params.filterUserIDList;
      if (params.isNeedSearch) {
        generateSearchServer(params.isNeedSearch);
      }
    });
    common_vendor.Jt.watch(common_vendor.o.GRP, {
      currentGroup: (data) => {
        group.value = data;
      },
      currentGroupMemberList: (List) => {
        memberList.value = List.map((item) => {
          if (selectOptions.value.filterUserIDList.indexOf(item.userID) > -1) {
            item.isDisabled = true;
          }
        });
        userList.value = memberList.value;
      }
    });
    const getMember = async () => {
      const groupID = group.value.groupID;
      const options = {
        groupID,
        count: 100,
        offset: memberList.value.length
      };
      await common_vendor.es.getGroupMemberList(options);
    };
    const handleSelectedResult = (memberList2) => {
      common_vendor.Jt.update(common_vendor.o.GRP, "isShowSelectComponent", false);
      const callback = TUIContactServer.getOnCallCallback(TUIConstants.TUIGroup.SERVICE.METHOD.SELECT_GROUP_MEMBER);
      callback && callback(memberList2);
    };
    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 = [...memberList.value];
    };
    const handleSearch = async (val) => {
      if (!val) {
        return userList.value = memberList.value;
      }
      try {
        const imResponse = await TUISearchServer.value.searchGroupMember(val);
        if (!imResponse.data[0]) {
          return searchFail();
        }
        userList.value = imResponse.data;
        const searchAllResult = memberList.value.filter((item) => item.userID === imResponse.data[0].userID);
        memberList.value = searchAllResult.length ? memberList.value : [...memberList.value, ...userList.value];
      } catch (error) {
        return searchFail();
      }
    };
    return (_ctx, _cache) => {
      return {
        a: common_vendor.o$1(getMember),
        b: common_vendor.o$1(handleSearch),
        c: common_vendor.o$1(handleSelectedResult),
        d: 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),
          total: common_vendor.unref(group).memberCount
        })
      };
    };
  }
});
wx.createComponent(_sfc_main);
//# sourceMappingURL=../../../../../.sourcemap/mp-weixin/TUIKit/components/TUIGroup/select-member/index.js.map
