import { defineComponent, ref, computed, createElementBlock, openBlock, createVNode, unref, withCtx, createElementVNode, toDisplayString, createTextVNode } from "vue";
import { useUIKit, Dropdown, IconInvite, IconShare, TUIDialog, TUIButton, TUIToast } from "@tencentcloud/uikit-base-component-vue3";
import { useContactListState } from "tuikit-atomicx-vue3/chat";
import { useLoginState, useRoomState, useRoomParticipantState, RoomParticipantStatus, UserPicker } from "tuikit-atomicx-vue3/room";
import IconButton from "../base/IconButton.vue2.mjs";
import RoomShare from "./RoomShare.vue2.mjs";
const _hoisted_1 = { class: "operate-list" };
const _hoisted_2 = { class: "operate-item-text" };
const _hoisted_3 = { class: "operate-item-text" };
const _hoisted_4 = { class: "user-picker-footer" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  setup(__props) {
    const { t } = useUIKit();
    const { loginUserInfo } = useLoginState();
    const { currentRoom, callUserToRoom } = useRoomState();
    const { participantList, pendingParticipantList } = useRoomParticipantState();
    const { friendList } = useContactListState();
    const userPickerRef = ref();
    const userPickerVisible = ref(false);
    const roomShareVisible = ref(false);
    const userPickerData = computed(() => friendList.value.filter((item) => !participantList.value.some((participant) => participant.userId === item.userID)).filter((item) => !pendingParticipantList.value.some((user) => user.userId === item.userID && user.roomStatus === RoomParticipantStatus.InCalling)).map((item) => ({
      key: item.userID,
      label: item.nick,
      avatarUrl: item.avatar,
      extraData: item
    })));
    const handleUserPickerConfirm = async () => {
      var _a;
      try {
        const selectedUsers = userPickerRef.value.getSelectedItems();
        if (selectedUsers.length === 0) {
          TUIToast.error({ message: t("Invite.PleaseSelectUser") });
          return;
        }
        await callUserToRoom({
          roomId: (_a = currentRoom.value) == null ? void 0 : _a.roomId,
          userIdList: selectedUsers.map((item) => item.key),
          timeout: 60
        });
        userPickerVisible.value = false;
        TUIToast.success({ message: t("Invite.InviteSuccess") });
      } catch (error) {
        console.error("Failed to invite users to room:", error);
        TUIToast.error({ message: t("Invite.InviteFailed") });
      }
    };
    return (_ctx, _cache) => {
      var _a, _b;
      return openBlock(), createElementBlock("div", null, [
        createVNode(unref(Dropdown), {
          trigger: "click",
          placement: "top",
          teleported: false,
          hideOnClick: false
        }, {
          dropdown: withCtx(() => [
            createElementVNode("div", _hoisted_1, [
              createElementVNode("div", {
                class: "operate-item",
                onClick: _cache[0] || (_cache[0] = ($event) => userPickerVisible.value = true)
              }, [
                createVNode(unref(IconInvite), { size: 18 }),
                createElementVNode("span", _hoisted_2, toDisplayString(unref(t)("Invite.AddMember")), 1)
              ]),
              createElementVNode("div", {
                class: "operate-item",
                onClick: _cache[1] || (_cache[1] = ($event) => roomShareVisible.value = true)
              }, [
                createVNode(unref(IconShare), { size: 18 }),
                createElementVNode("span", _hoisted_3, toDisplayString(unref(t)("Invite.ShareRoom")), 1)
              ])
            ])
          ]),
          default: withCtx(() => [
            createVNode(IconButton, {
              title: unref(t)("Invite.Title")
            }, {
              default: withCtx(() => [
                createVNode(unref(IconInvite), { size: 24 })
              ]),
              _: 1
            }, 8, ["title"])
          ]),
          _: 1
        }),
        createVNode(unref(TUIDialog), {
          visible: userPickerVisible.value,
          "onUpdate:visible": _cache[3] || (_cache[3] = ($event) => userPickerVisible.value = $event),
          "append-to": "body",
          title: `${unref(t)("Contacts")} (${userPickerData.value.length})`
        }, {
          footer: withCtx(() => [
            createElementVNode("div", _hoisted_4, [
              createVNode(unref(TUIButton), {
                onClick: _cache[2] || (_cache[2] = ($event) => userPickerVisible.value = false)
              }, {
                default: withCtx(() => [
                  createTextVNode(toDisplayString(unref(t)("Room.Cancel")), 1)
                ]),
                _: 1
              }),
              createVNode(unref(TUIButton), {
                type: "primary",
                onClick: handleUserPickerConfirm
              }, {
                default: withCtx(() => [
                  createTextVNode(toDisplayString(unref(t)("Room.Confirm")), 1)
                ]),
                _: 1
              })
            ])
          ]),
          default: withCtx(() => [
            createVNode(unref(UserPicker), {
              ref_key: "userPickerRef",
              ref: userPickerRef,
              class: "room-user-picker",
              "data-source": userPickerData.value,
              "display-mode": "list"
            }, null, 8, ["data-source"])
          ]),
          _: 1
        }, 8, ["visible", "title"]),
        createVNode(unref(TUIDialog), {
          visible: roomShareVisible.value,
          "onUpdate:visible": _cache[4] || (_cache[4] = ($event) => roomShareVisible.value = $event),
          "append-to": "body",
          title: unref(t)("Room.InviteToMeeting", { userName: ((_a = unref(loginUserInfo)) == null ? void 0 : _a.userName) || ((_b = unref(loginUserInfo)) == null ? void 0 : _b.userId) }),
          "custom-classes": ["room-share-dialog"]
        }, {
          footer: withCtx(() => [..._cache[5] || (_cache[5] = [
            createElementVNode("div", null, null, -1)
          ])]),
          default: withCtx(() => [
            createVNode(RoomShare, { "room-info": unref(currentRoom) }, null, 8, ["room-info"])
          ]),
          _: 1
        }, 8, ["visible", "title"])
      ]);
    };
  }
});
export {
  _sfc_main as default
};
