"use strict";
const common_vendor = require("../../../../common/vendor.js");
require("../../../adapter-vue.js");
const common_assets = require("../../../../common/assets.js");
const TUIKit_components_TUIChat_utils_utils = require("../../TUIChat/utils/utils.js");
const TUIKit_utils_env = require("../../../utils/env.js");
if (!Math) {
  (Icon + ContactListItem)();
}
const Icon = () => "../../common/Icon.js";
const ContactListItem = () => "./contact-list-item/index.js";
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
  __name: "index",
  setup(__props) {
    const currentContactListKey = common_vendor.ref("");
    const currentContactInfo = common_vendor.ref({});
    const contactListMap = common_vendor.ref({
      friendApplicationList: {
        key: "friendApplicationList",
        title: "新的联系人",
        list: [],
        unreadCount: 0
      },
      blackList: {
        key: "blackList",
        title: "黑名单",
        list: []
      },
      groupList: {
        key: "groupList",
        title: "我的群聊",
        list: []
      },
      friendList: {
        key: "friendList",
        title: "我的好友",
        list: []
      }
    });
    const contactSearchingStatus = common_vendor.ref(false);
    const contactSearchResult = common_vendor.ref();
    const displayOnlineStatus = common_vendor.ref(false);
    const userOnlineStatusMap = common_vendor.ref();
    const isContactSearchNoResult = common_vendor.computed(() => {
      var _a, _b, _c, _d;
      return !((_b = (_a = contactSearchResult == null ? void 0 : contactSearchResult.value) == null ? void 0 : _a.user) == null ? void 0 : _b.list[0]) && !((_d = (_c = contactSearchResult == null ? void 0 : contactSearchResult.value) == null ? void 0 : _c.group) == null ? void 0 : _d.list[0]);
    });
    common_vendor.onMounted(() => {
      common_vendor.Jt.watch(common_vendor.o.APP, {
        enabledCustomerServicePlugin: onCustomerServiceCommercialPluginUpdated
      });
      common_vendor.Jt.watch(common_vendor.o.GRP, {
        groupList: onGroupListUpdated
      });
      common_vendor.Jt.watch(common_vendor.o.USER, {
        userBlacklist: onUserBlacklistUpdated,
        displayOnlineStatus: onDisplayOnlineStatusUpdated,
        userStatusList: onUserStatusListUpdated
      });
      common_vendor.Jt.watch(common_vendor.o.FRIEND, {
        friendList: onFriendListUpdated,
        friendApplicationList: onFriendApplicationListUpdated,
        friendApplicationUnreadCount: onFriendApplicationUnreadCountUpdated
      });
      common_vendor.Jt.watch(common_vendor.o.CUSTOM, {
        currentContactSearchingStatus: onCurrentContactSearchingStatusUpdated,
        currentContactSearchResult: onCurrentContactSearchResultUpdated,
        currentContactListKey: onCurrentContactListKeyUpdated,
        currentContactInfo: onCurrentContactInfoUpdated
      });
    });
    common_vendor.onUnmounted(() => {
      common_vendor.Jt.unwatch(common_vendor.o.APP, {
        enabledCustomerServicePlugin: onCustomerServiceCommercialPluginUpdated
      });
      common_vendor.Jt.unwatch(common_vendor.o.GRP, {
        groupList: onGroupListUpdated
      });
      common_vendor.Jt.unwatch(common_vendor.o.USER, {
        userBlacklist: onUserBlacklistUpdated,
        displayOnlineStatus: onDisplayOnlineStatusUpdated,
        userStatusList: onUserStatusListUpdated
      });
      common_vendor.Jt.unwatch(common_vendor.o.FRIEND, {
        friendList: onFriendListUpdated,
        friendApplicationList: onFriendApplicationListUpdated,
        friendApplicationUnreadCount: onFriendApplicationUnreadCountUpdated
      });
      common_vendor.Jt.unwatch(common_vendor.o.CUSTOM, {
        currentContactSearchingStatus: onCurrentContactSearchingStatusUpdated,
        currentContactSearchResult: onCurrentContactSearchResultUpdated,
        currentContactListKey: onCurrentContactListKeyUpdated,
        currentContactInfo: onCurrentContactInfoUpdated
      });
    });
    function toggleCurrentContactList(key) {
      if (currentContactListKey.value === key) {
        currentContactListKey.value = "";
        currentContactInfo.value = {};
        common_vendor.Jt.update(common_vendor.o.CUSTOM, "currentContactListKey", "");
        common_vendor.Jt.update(common_vendor.o.CUSTOM, "currentContactInfo", {});
      } else {
        currentContactListKey.value = key;
        common_vendor.Jt.update(common_vendor.o.CUSTOM, "currentContactListKey", key);
        if (key === "friendApplicationList") {
          common_vendor.ts.setFriendApplicationRead();
        }
      }
    }
    function selectItem(item) {
      var _a, _b, _c, _d, _e, _f, _g, _h;
      currentContactInfo.value = item;
      if (contactSearchingStatus.value) {
        let targetListItem;
        if ((_a = currentContactInfo.value) == null ? void 0 : _a.userID) {
          targetListItem = (_d = (_c = (_b = contactListMap.value) == null ? void 0 : _b.friendList) == null ? void 0 : _c.list) == null ? void 0 : _d.find(
            (item2) => {
              var _a2;
              return (item2 == null ? void 0 : item2.userID) === ((_a2 = currentContactInfo.value) == null ? void 0 : _a2.userID);
            }
          );
        } else if ((_e = currentContactInfo.value) == null ? void 0 : _e.groupID) {
          targetListItem = (_h = (_g = (_f = contactListMap.value) == null ? void 0 : _f.groupList) == null ? void 0 : _g.list) == null ? void 0 : _h.find(
            (item2) => {
              var _a2;
              return (item2 == null ? void 0 : item2.groupID) === ((_a2 = currentContactInfo.value) == null ? void 0 : _a2.groupID);
            }
          );
        }
        if (targetListItem) {
          currentContactInfo.value = targetListItem;
        }
      }
      common_vendor.Jt.update(common_vendor.o.CUSTOM, "currentContactInfo", currentContactInfo.value);
    }
    function onDisplayOnlineStatusUpdated(status) {
      displayOnlineStatus.value = status;
    }
    function onUserStatusListUpdated(list) {
      if ((list == null ? void 0 : list.size) > 0) {
        userOnlineStatusMap.value = Object.fromEntries(list == null ? void 0 : list.entries());
      }
    }
    function onCustomerServiceCommercialPluginUpdated(isEnabled) {
      if (!isEnabled) {
        return;
      }
      const contactListExtensionID = common_vendor.E.TUIContact.EXTENSION.CONTACT_LIST.EXT_ID;
      const tuiContactExtensionList = common_vendor.R.getExtensionList(contactListExtensionID);
      const customerData = tuiContactExtensionList.find((extension) => {
        const { name, accountList = [] } = extension.data || {};
        return name === "customer" && accountList.length > 0;
      });
      if (customerData) {
        const { data, text } = customerData;
        const { accountList } = data || {};
        common_vendor.Zt.getUserProfile({ userIDList: accountList }).then((res) => {
          if (res.data.length > 0) {
            const customerList = {
              title: text,
              list: res.data.map((item, index) => {
                return {
                  ...item,
                  renderKey: generateRenderKey("customerList", item, index),
                  infoKeyList: [],
                  btnKeyList: ["enterC2CConversation"]
                };
              }),
              key: "customerList"
            };
            contactListMap.value = { ...contactListMap.value, customerList };
          }
        }).catch(() => {
        });
      }
    }
    function onGroupListUpdated(groupList) {
      updateContactListMap("groupList", groupList);
    }
    function onUserBlacklistUpdated(userBlacklist) {
      updateContactListMap("blackList", userBlacklist);
    }
    function onFriendApplicationUnreadCountUpdated(friendApplicationUnreadCount) {
      contactListMap.value.friendApplicationList.unreadCount = friendApplicationUnreadCount;
    }
    function onFriendListUpdated(friendList) {
      updateContactListMap("friendList", friendList);
    }
    function onFriendApplicationListUpdated(friendApplicationList) {
      updateContactListMap("friendApplicationList", friendApplicationList);
    }
    function updateContactListMap(key, list) {
      contactListMap.value[key].list = list;
      contactListMap.value[key].list.map((item, index) => item.renderKey = generateRenderKey(key, item, index));
      updateCurrentContactInfoFromList(contactListMap.value[key].list, key);
    }
    function updateCurrentContactInfoFromList(list, type) {
      var _a, _b;
      if (!((_a = currentContactInfo.value) == null ? void 0 : _a.userID) && !((_b = currentContactInfo.value) == null ? void 0 : _b.groupID)) {
        return;
      }
      if (type === currentContactListKey.value || contactSearchingStatus.value) {
        currentContactInfo.value = (list == null ? void 0 : list.find(
          (item) => {
            var _a2, _b2;
            return (item == null ? void 0 : item.groupID) && (item == null ? void 0 : item.groupID) === ((_a2 = currentContactInfo.value) == null ? void 0 : _a2.groupID) || (item == null ? void 0 : item.userID) && (item == null ? void 0 : item.userID) === ((_b2 = currentContactInfo.value) == null ? void 0 : _b2.userID);
          }
        )) || {};
        common_vendor.Jt.update(common_vendor.o.CUSTOM, "currentContactInfo", currentContactInfo.value);
      }
    }
    function generateRenderKey(contactListMapKey, contactInfo, index) {
      return `${contactListMapKey}-${contactInfo.userID || contactInfo.groupID || "index" + index}`;
    }
    function onCurrentContactSearchResultUpdated(searchResult) {
      contactSearchResult.value = searchResult;
    }
    function onCurrentContactSearchingStatusUpdated(searchingStatus) {
      contactSearchingStatus.value = searchingStatus;
      common_vendor.Jt.update(common_vendor.o.CUSTOM, "currentContactInfo", {});
      common_vendor.Jt.update(common_vendor.o.CUSTOM, "currentContactListKey", "");
    }
    function onCurrentContactInfoUpdated(contactInfo) {
      currentContactInfo.value = contactInfo;
    }
    function onCurrentContactListKeyUpdated(contactListKey) {
      currentContactListKey.value = contactListKey;
    }
    common_vendor.provide("userOnlineStatusMap", userOnlineStatusMap);
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: !common_vendor.unref(contactSearchingStatus)
      }, !common_vendor.unref(contactSearchingStatus) ? {
        b: common_vendor.f(common_vendor.unref(contactListMap), (contactListObj, key, i0) => {
          return common_vendor.e({
            a: "2682c040-0-" + i0,
            b: common_vendor.p({
              file: common_vendor.unref(currentContactListKey) === key ? common_vendor.unref(common_assets.downSVG) : common_vendor.unref(common_assets.rightIcon),
              width: "16px",
              height: "16px"
            }),
            c: common_vendor.t(common_vendor.unref(common_vendor.Wt).t(`TUIContact.${contactListObj.title}`)),
            d: contactListObj.unreadCount
          }, contactListObj.unreadCount ? {
            e: common_vendor.t(contactListObj.unreadCount)
          } : {}, {
            f: common_vendor.o$1(($event) => toggleCurrentContactList(key), key),
            g: common_vendor.f(contactListObj.list, (contactListItem, k1, i1) => {
              return {
                a: contactListItem.renderKey,
                b: "2682c040-1-" + i0 + "-" + i1,
                c: common_vendor.p({
                  item: common_vendor.unref(TUIKit_components_TUIChat_utils_utils.deepCopy)(contactListItem),
                  displayOnlineStatus: common_vendor.unref(displayOnlineStatus) && key === "friendList"
                }),
                d: contactListItem.renderKey,
                e: common_vendor.o$1(($event) => selectItem(contactListItem), contactListItem.renderKey)
              };
            }),
            h: common_vendor.n(common_vendor.unref(currentContactListKey) === key ? "" : "hidden"),
            i: key
          });
        }),
        c: common_vendor.n(!common_vendor.unref(TUIKit_utils_env.isPC) && "tui-contact-list-h5")
      } : common_vendor.e({
        d: common_vendor.f(common_vendor.unref(contactSearchResult), (item, key, i0) => {
          return common_vendor.e({
            a: item.list[0]
          }, item.list[0] ? {
            b: common_vendor.t(common_vendor.unref(common_vendor.Wt).t(`TUIContact.${item.label}`)),
            c: common_vendor.f(item.list, (listItem, index, i1) => {
              return {
                a: "2682c040-2-" + i0 + "-" + i1,
                b: common_vendor.p({
                  item: listItem,
                  displayOnlineStatus: false
                }),
                c: index,
                d: common_vendor.o$1(($event) => selectItem(listItem), index)
              };
            })
          } : {}, {
            d: key
          });
        }),
        e: common_vendor.unref(isContactSearchNoResult)
      }, common_vendor.unref(isContactSearchNoResult) ? {
        f: common_vendor.t(common_vendor.unref(common_vendor.Wt).t("TUIContact.无搜索结果"))
      } : {}));
    };
  }
});
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-2682c040"]]);
wx.createComponent(Component);
//# sourceMappingURL=../../../../../.sourcemap/mp-weixin/TUIKit/components/TUIContact/contact-list/index.js.map
