import { ref, computed } from "vue";
import { useBasicStore } from "../../stores/basic.mjs";
import { useRoomStore } from "../../stores/room.mjs";
import renderMsg from "../common/base/Message/Message.mjs";
import { storeToRefs } from "pinia";
import { useI18n } from "../../locales/index.mjs";
import { getUrlWithRoomId } from "../../utils/utils.mjs";
import { isElectron } from "../../utils/environment.mjs";
import InviteIcon from "../common/icons/InviteIcon.vue.mjs";
import ShareIcon from "../common/icons/ShareIcon.vue.mjs";
import "../../services/main.mjs";
import { roomService } from "../../services/roomService.mjs";
import "@tencentcloud/tuiroom-engine-js";
import "mitt";
import "../../services/manager/roomActionManager.mjs";
import "@tencentcloud/tui-core";
import { MESSAGE_DURATION } from "../../constants/message.mjs";
import { clipBoard } from "../../utils/adapter.mjs";
function useRoomInvite() {
  const { t } = useI18n();
  const basicStore = useBasicStore();
  const roomLinkConfig = roomService.getComponentConfig("RoomLink");
  const roomStore = useRoomStore();
  const { remoteEnteredUserList, roomName, isSeatEnabled, password } = storeToRefs(roomStore);
  const {
    roomId,
    shareLink,
    isRoomLinkVisible,
    isSchemeLinkVisible,
    userId,
    userName
  } = storeToRefs(basicStore);
  const { origin, pathname } = location || {};
  const isShowLink = ref(true);
  const showContacts = ref(false);
  const showRoomInvite = ref(false);
  const inviteLink = computed(() => {
    if (shareLink.value) {
      const urlConcatenation = shareLink.value.indexOf("?") !== -1 ? "&" : "?";
      return `${shareLink.value}${urlConcatenation}roomId=${roomId.value}`;
    }
    return `${origin}${pathname}#/home?roomId=${roomId.value}`;
  });
  const schemeLink = computed(
    () => `tuiroom://joinroom?roomId=${roomId.value}`
  );
  const inviteBarTitle = computed(
    () => isRoomLinkVisible.value ? t(
      "You can share the room number or link to invite more people to join the room."
    ) : t(
      "You can share the room number to invite more people to join the room"
    )
  );
  const isShowPassword = computed(() => !!(password == null ? void 0 : password.value));
  const invitationFeatureDetails = ref([
    {
      icon: InviteIcon,
      text: "addMember",
      function: async () => {
        contacts.value = await roomService.scheduleConferenceManager.fetchFriendList();
        showContacts.value = true;
      }
    },
    {
      icon: ShareIcon,
      text: "shareRoom",
      function: () => {
        showRoomInvite.value = true;
      }
    }
  ]);
  const contacts = ref([]);
  async function onCopy(value) {
    try {
      await clipBoard(value);
      renderMsg({
        message: t("Copied successfully"),
        type: "success"
      });
    } catch (error) {
      renderMsg({
        message: t("Copied failure"),
        type: "error"
      });
    }
  }
  const inviteContentList = computed(() => [
    {
      id: 1,
      mobileTitle: "Room ID",
      pcTitle: "Invite by room number",
      content: roomId.value,
      copyLink: roomId.value,
      visible: isShowLink.value
    },
    {
      id: 2,
      mobileTitle: "Room Link",
      pcTitle: "Invite via room link",
      content: inviteLink.value,
      copyLink: inviteLink.value,
      visible: isRoomLinkVisible.value
    },
    {
      id: 3,
      mobileTitle: "scheme",
      pcTitle: "Invite via client scheme",
      content: schemeLink.value,
      copyLink: schemeLink.value,
      visible: isSchemeLinkVisible.value
    }
  ]);
  const visibleInviteContentList = computed(
    () => inviteContentList.value.filter((item) => item.visible)
  );
  const conferenceInviteList = computed(() => [
    {
      title: "Room Name",
      content: roomName.value,
      isShowCopyIcon: false,
      isVisible: true
    },
    {
      title: "Room Type",
      content: `${t(getSeatModeDisplay(isSeatEnabled.value))}`,
      isShowCopyIcon: false,
      isVisible: true
    },
    {
      title: "Room ID",
      content: roomId.value,
      isShowCopyIcon: true,
      isVisible: true
    },
    {
      title: "Room Password",
      content: password == null ? void 0 : password.value,
      isShowCopyIcon: true,
      isVisible: isShowPassword.value
    },
    {
      title: "Room Link",
      content: getUrlWithRoomId(roomId.value),
      isShowCopyIcon: true,
      isVisible: isRoomLinkVisible.value && roomLinkConfig.visible
    }
  ]);
  function getSeatModeDisplay(isSeatEnabled2) {
    return isSeatEnabled2 ? "On-stage Speaking Room" : "Free Speech Room";
  }
  function copyRoomIdAndRoomLink() {
    const invitationList = [
      roomName.value,
      `${t("Room Type")}: ${t(getSeatModeDisplay(isSeatEnabled.value))}`,
      `${t("Room ID")}: ${roomId.value}`
    ];
    if (isShowPassword.value) {
      invitationList.push(`${t("Room Password")}: ${password == null ? void 0 : password.value}`);
    }
    if (isRoomLinkVisible.value && roomLinkConfig.visible) {
      invitationList.push(
        `${t("Room Link")}: ${getUrlWithRoomId(roomId.value)}`
      );
    }
    const invitation = invitationList.join("\n");
    onCopy(invitation);
    showRoomInvite.value = false;
  }
  const contactsConfirm = async (contacts2) => {
    const userIdList = contacts2.map((contacts3) => contacts3.userId);
    await roomService.conferenceInvitationManager.inviteUsers({ userIdList });
    renderMsg({
      type: "success",
      message: t("Invitation sent, waiting for members to join."),
      duration: MESSAGE_DURATION.NORMAL
    });
  };
  return {
    t,
    isRoomLinkVisible,
    roomId,
    origin,
    pathname,
    isElectron,
    inviteLink,
    schemeLink,
    inviteBarTitle,
    onCopy,
    visibleInviteContentList,
    invitationFeatureDetails,
    showContacts,
    contactsConfirm,
    contacts,
    remoteEnteredUserList,
    showRoomInvite,
    userId,
    userName,
    conferenceInviteList,
    copyRoomIdAndRoomLink
  };
}
export {
  useRoomInvite as default
};
