import { defineComponent, ref, watch, onMounted, onUnmounted, openBlock, createElementBlock, unref, createElementVNode, toDisplayString, createCommentVNode, createVNode, withCtx, createBlock, Fragment, renderList, normalizeClass } from "vue";
import "../../../../adapter-vue.mjs";
import { TUIUserService, TUIStore, StoreName, TUITranslateService, TUIGroupService } from "@tencentcloud/chat-uikit-engine";
import Icon from "../../../common/Icon.vue.mjs";
import Avatar from "../../../common/Avatar/index.vue.mjs";
import Drawer from "../../../common/Drawer/index.vue.mjs";
import closeIcon from "../../../../assets/icon/close-dark.svg.mjs";
import { isMobile, isPC } from "../../../../utils/env.mjs";
const _hoisted_1 = {
  key: 0,
  class: "application-tips"
};
const _hoisted_2 = {
  class: /* @__PURE__ */ normalizeClass({
    "application-contaienr": true
  })
};
const _hoisted_3 = { class: "application-header" };
const _hoisted_4 = { key: 1 };
const _hoisted_5 = { class: "application-item-info" };
const _hoisted_6 = { class: "application-item-nick" };
const _hoisted_7 = { class: "application-item-note" };
const _hoisted_8 = { class: "application-item-operation" };
const _hoisted_9 = ["onClick"];
const _hoisted_10 = ["onClick"];
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  props: {
    groupID: { default: "" }
  },
  setup(__props) {
    const props = __props;
    const drawerDomInstanceRef = ref();
    const groupApplicationCount = ref(0);
    const isGroupApplicationDrawerShow = ref(false);
    const customGroupApplicationList = ref([]);
    watch(isGroupApplicationDrawerShow, (newVal) => {
      if (newVal) {
        generateCustomGroupApplicationList().then((list) => {
          customGroupApplicationList.value = list;
          groupApplicationCount.value = list.length;
        });
      }
    });
    watch(() => customGroupApplicationList.value.length, (newVal, oldVal) => {
      if (oldVal > 0 && newVal === 0) {
        isGroupApplicationDrawerShow.value = false;
      }
    });
    async function getCurrentGroupApplicationList() {
      const result = await TUIGroupService.getGroupApplicationList();
      const currentGroupApplicationList = result.data.applicationList.filter((application) => application.groupID === props.groupID);
      return currentGroupApplicationList;
    }
    function toggleGroupApplicationDrawerShow() {
      isGroupApplicationDrawerShow.value = !isGroupApplicationDrawerShow.value;
    }
    async function generateCustomGroupApplicationList() {
      const applicationList = await getCurrentGroupApplicationList();
      if (applicationList.length === 0) {
        return [];
      }
      const userIDList = applicationList.map((application) => application.applicationType === 0 ? application.applicant : application.userID);
      const { data: userProfileList } = await TUIUserService.getUserProfile({ userIDList });
      const mappingFromUserID2Profile = {};
      userProfileList.forEach((profile) => {
        mappingFromUserID2Profile[profile.userID] = profile;
      });
      const groupApplicationList = applicationList.map((application) => {
        const profile = mappingFromUserID2Profile[application.applicationType === 0 ? application.applicant : application.userID];
        return {
          nick: profile.nick || profile.userID || "anonymous",
          avatar: profile.avatar || "",
          isRemoved: false,
          application
        };
      });
      return groupApplicationList;
    }
    function handleApplication(customApplication, action, index) {
      TUIGroupService.handleGroupApplication({
        handleAction: action,
        application: customApplication.application
      }).then(() => {
        customGroupApplicationList.value[index].isRemoved = true;
        setTimeout(() => {
          customGroupApplicationList.value.splice(index, 1);
          groupApplicationCount.value -= 1;
        }, 150);
      }).catch(() => {
      });
    }
    onMounted(() => {
      getCurrentGroupApplicationList().then((applicationList) => {
        groupApplicationCount.value = applicationList.length;
      });
      TUIStore.watch(StoreName.GRP, {
        groupSystemNoticeList: onGroupSystemNoticeListUpdated
      });
    });
    onUnmounted(() => {
      TUIStore.unwatch(StoreName.GRP, {
        groupSystemNoticeList: onGroupSystemNoticeListUpdated
      });
    });
    function onGroupSystemNoticeListUpdated() {
      generateCustomGroupApplicationList().then((list) => {
        customGroupApplicationList.value = list;
        groupApplicationCount.value = list.length;
      });
    }
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", null, [
        unref(groupApplicationCount) > 0 ? (openBlock(), createElementBlock("div", _hoisted_1, [
          createElementVNode("div", null, toDisplayString(unref(groupApplicationCount)) + toDisplayString(unref(TUITranslateService).t("TUIChat.条入群申请")), 1),
          createElementVNode("div", {
            class: "application-tips-btn",
            onClick: toggleGroupApplicationDrawerShow
          }, toDisplayString(unref(TUITranslateService).t("TUIChat.点击处理")), 1)
        ])) : createCommentVNode("", true),
        createVNode(Drawer, {
          ref_key: "drawerDomInstanceRef",
          ref: drawerDomInstanceRef,
          visible: unref(isGroupApplicationDrawerShow),
          zIndex: 998,
          popDirection: unref(isMobile) ? "bottom" : "right",
          isFullScreen: unref(isMobile),
          overlayColor: unref(isMobile) ? void 0 : "transparent",
          drawerStyle: {
            bottom: {
              minHeight: "60vh",
              maxHeight: "80vh",
              borderRadius: "12px 12px 0 0"
            },
            right: {
              width: "360px",
              borderRadius: "12px 0 0 12px",
              boxShadow: "0 0 10px 0 #d0d0d0"
            }
          },
          onOnOverlayClick: toggleGroupApplicationDrawerShow
        }, {
          default: withCtx(() => [
            createElementVNode("div", _hoisted_2, [
              createElementVNode("header", _hoisted_3, [
                createElementVNode("div", { onClick: toggleGroupApplicationDrawerShow }, [
                  unref(isPC) ? (openBlock(), createBlock(Icon, {
                    key: 0,
                    file: unref(closeIcon),
                    size: "16px"
                  }, null, 8, ["file"])) : (openBlock(), createElementBlock("div", _hoisted_4, toDisplayString(unref(TUITranslateService).t("关闭")), 1))
                ])
              ]),
              createElementVNode("main", null, [
                (openBlock(true), createElementBlock(Fragment, null, renderList(unref(customGroupApplicationList), (item, index) => {
                  return openBlock(), createElementBlock("div", {
                    key: item.nick,
                    class: normalizeClass({
                      "application-item": true,
                      "removed": item.isRemoved
                    })
                  }, [
                    createVNode(Avatar, {
                      style: {
                        flex: "0 0 auto"
                      },
                      url: item.avatar,
                      useSkeletonAnimation: true
                    }, null, 8, ["url"]),
                    createElementVNode("div", _hoisted_5, [
                      createElementVNode("div", _hoisted_6, toDisplayString(item.nick), 1),
                      createElementVNode("div", _hoisted_7, toDisplayString(unref(TUITranslateService).t("TUIChat.申请加入")), 1)
                    ]),
                    createElementVNode("div", _hoisted_8, [
                      createElementVNode("div", {
                        class: "agree",
                        onClick: ($event) => handleApplication(item, "Agree", index)
                      }, toDisplayString(unref(TUITranslateService).t("TUIChat.同意")), 9, _hoisted_9),
                      createElementVNode("div", {
                        class: "reject",
                        onClick: ($event) => handleApplication(item, "Reject", index)
                      }, toDisplayString(unref(TUITranslateService).t("TUIChat.拒绝")), 9, _hoisted_10)
                    ])
                  ], 2);
                }), 128))
              ])
            ])
          ]),
          _: 1
        }, 8, ["visible", "popDirection", "isFullScreen", "overlayColor"])
      ]);
    };
  }
});
export {
  _sfc_main as default
};
