import { defineComponent, ref, computed, watch, openBlock, createBlock, unref, withCtx, createVNode } from "vue";
import { TUIUserService, TUIGroupService } from "@tencentcloud/chat-uikit-engine";
import "../../../../adapter-vue.mjs";
import Dialog from "../../../common/Dialog/index.vue.mjs";
import Transfer from "../../../common/Transfer/index.vue.mjs";
import { isPC } from "../../../../utils/env.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  props: {
    // type: voiceCall/groupCall/...
    type: {
      type: String,
      default: ""
    },
    currentConversation: {
      type: Object,
      default: () => ({})
    },
    isGroup: {
      type: Boolean,
      default: false
    }
  },
  emits: ["submit", "cancel"],
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const show = ref(false);
    const groupID = ref("");
    const memberList = ref([]);
    const searchMemberList = ref([]);
    const selfUserID = ref("");
    const titleMap = {
      voiceCall: "发起群语音",
      videoCall: "发起群视频"
    };
    const title = computed(() => {
      return titleMap[props.type] ? titleMap[props.type] : "";
    });
    TUIUserService.getUserProfile().then((res) => {
      var _a;
      if ((_a = res == null ? void 0 : res.data) == null ? void 0 : _a.userID) {
        selfUserID.value = res.data.userID;
      }
    });
    watch(
      () => {
        var _a;
        return [(_a = props == null ? void 0 : props.currentConversation) == null ? void 0 : _a.conversationID, show.value];
      },
      (newVal, oldVal) => {
        if (newVal && newVal !== oldVal) {
          if (props.isGroup && show.value) {
            groupID.value = props.currentConversation.groupProfile.groupID;
            TUIGroupService.getGroupMemberList({
              groupID: groupID.value
            }).then((res) => {
              var _a, _b;
              memberList.value = (_b = (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.memberList) == null ? void 0 : _b.filter(
                (user) => (user == null ? void 0 : user.userID) !== selfUserID.value
              );
              searchMemberList.value = memberList.value;
            });
          } else {
            groupID.value = "";
            memberList.value = [];
            searchMemberList.value = memberList.value;
          }
        }
      },
      {
        immediate: true
      }
    );
    const search = (searchInfo) => {
      var _a;
      const results = (_a = memberList.value) == null ? void 0 : _a.filter(
        (member) => (member == null ? void 0 : member.userID) === searchInfo
      );
      searchMemberList.value = (results == null ? void 0 : results.length) ? results : memberList.value;
    };
    const submit = (selectedMemberList) => {
      const userIDList = [];
      selectedMemberList == null ? void 0 : selectedMemberList.forEach((user) => {
        (user == null ? void 0 : user.userID) && userIDList.push(user.userID);
      });
      if (props.type === "voiceCall") {
        emits("submit", { userIDList, groupID: groupID.value, type: 1 });
      } else if (props.type === "videoCall") {
        emits("submit", { userIDList, groupID: groupID.value, type: 2 });
      }
      searchMemberList.value = memberList.value;
      toggleShow(false);
    };
    const cancel = () => {
      searchMemberList.value = memberList.value;
      emits("cancel");
      toggleShow(false);
    };
    const toggleShow = (showStatus) => {
      show.value = showStatus;
    };
    __expose({
      toggleShow
    });
    return (_ctx, _cache) => {
      return openBlock(), createBlock(Dialog, {
        show: unref(show),
        isH5: !unref(isPC),
        isHeaderShow: false,
        isFooterShow: false,
        background: false,
        "onUpdate:show": toggleShow
      }, {
        default: withCtx(() => [
          createVNode(Transfer, {
            isSearch: true,
            title: unref(title),
            list: unref(searchMemberList),
            isH5: !unref(isPC),
            isRadio: false,
            onSearch: search,
            onSubmit: submit,
            onCancel: cancel
          }, null, 8, ["title", "list", "isH5"])
        ]),
        _: 1
      }, 8, ["show", "isH5"]);
    };
  }
});
export {
  _sfc_main as default
};
