import { defineComponent, openBlock, createElementBlock, createElementVNode, withDirectives, createVNode, toDisplayString, unref, normalizeClass, createCommentVNode, withModifiers, createTextVNode, withCtx, isRef, vModelText, Fragment, renderList, createBlock, vShow } from "vue";
import { TUIRole } from "@tencentcloud/tuiroom-engine-js";
import useEndControl from "./useEndControlHooks.mjs";
import logger from "../../../utils/common/logger/index.mjs";
import popup from "../../common/base/PopUpH5.vue.mjs";
import SvgIcon from "../../common/base/SvgIcon.vue.mjs";
import CorrectIcon from "../../common/icons/CorrectIcon.vue.mjs";
import SearchIcon from "../../common/icons/SearchIcon.vue.mjs";
import Avatar from "../../common/Avatar.vue.mjs";
import vTap from "../../../directives/vTap.mjs";
import EndRoomIcon from "../../common/icons/EndRoomIcon.vue.mjs";
import "../../../services/main.mjs";
import { roomService } from "../../../services/roomService.mjs";
import "../../../locales/index.mjs";
import "mitt";
import "../../../services/manager/roomActionManager.mjs";
import "@tencentcloud/tui-core";
const _hoisted_1 = { class: "end-control-container" };
const _hoisted_2 = {
  class: "end-button",
  tabindex: "1"
};
const _hoisted_3 = { class: "end-button-title" };
const _hoisted_4 = {
  key: 0,
  class: "end-main-content"
};
const _hoisted_5 = { key: 0 };
const _hoisted_6 = {
  key: 0,
  class: "end-dialog-header"
};
const _hoisted_7 = {
  key: 0,
  class: "end-dialog-text"
};
const _hoisted_8 = { key: 1 };
const _hoisted_9 = {
  key: 1,
  class: "dialog-middle-content"
};
const _hoisted_10 = { key: 2 };
const _hoisted_11 = { style: { "height": "100%" } };
const _hoisted_12 = { class: "transfer-list-container" };
const _hoisted_13 = { class: "transfer-header" };
const _hoisted_14 = { class: "search-container" };
const _hoisted_15 = ["placeholder"];
const _hoisted_16 = { class: "transfer-body" };
const _hoisted_17 = ["onClick"];
const _hoisted_18 = { class: "member-basic-info" };
const _hoisted_19 = { class: "user-name" };
const _hoisted_20 = {
  key: 0,
  class: "member-has-no-data"
};
const _hoisted_21 = { class: "transfer-button" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "EndControlH5",
  setup(__props) {
    const {
      t,
      isShowLeaveRoomDialog,
      roomStore,
      roomEngine,
      stopMeeting,
      cancel,
      DialogType,
      logPrefix,
      currentDialogType,
      visible,
      resetState,
      toggleMangeMemberSidebar,
      searchName,
      hasNoData,
      handleShowMemberControl,
      filteredList,
      selectedUser,
      showSideBar,
      remoteEnteredUserList,
      isMasterWithOneRemoteUser,
      isMasterWithRemoteUser
    } = useEndControl();
    function handleEndBtnClick() {
      stopMeeting();
    }
    function handleEndLeaveClick() {
      if (!roomStore.isMaster) {
        leaveRoom();
        return;
      }
      if (isMasterWithRemoteUser.value) {
        selectedUser.value = remoteEnteredUserList.value[0].userId;
        if (isMasterWithOneRemoteUser.value) {
          transferAndLeave();
          return;
        }
        currentDialogType.value = DialogType.TransferDialog;
        toggleMangeMemberSidebar();
        resetState();
        return;
      }
    }
    async function dismissRoom() {
      try {
        logger.log(`${logPrefix}dismissRoom: enter`);
        resetState();
        await roomService.dismissRoom();
      } catch (error) {
        logger.error(`${logPrefix}dismissRoom error:`, error);
      }
    }
    async function leaveRoom() {
      try {
        resetState();
        await roomService.leaveRoom();
      } catch (error) {
        logger.error(`${logPrefix}leaveRoom error:`, error);
      }
    }
    async function transferAndLeave() {
      var _a;
      if (!selectedUser.value) {
        return;
      }
      try {
        const userId = selectedUser.value;
        const changeUserRoleResponse = await ((_a = roomEngine.instance) == null ? void 0 : _a.changeUserRole({
          userId,
          userRole: TUIRole.kRoomOwner
        }));
        logger.log(`${logPrefix}transferAndLeave:`, changeUserRoleResponse);
        resetState();
        await roomService.leaveRoom();
      } catch (error) {
        logger.error(`${logPrefix}transferAndLeave error:`, error);
      }
    }
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", null, [
        createElementVNode("div", _hoisted_1, [
          withDirectives((openBlock(), createElementBlock("div", _hoisted_2, [
            createVNode(SvgIcon, { icon: EndRoomIcon }),
            createElementVNode("span", _hoisted_3, toDisplayString(unref(t)("EndH5")), 1)
          ])), [
            [unref(vTap), handleEndBtnClick]
          ])
        ]),
        unref(visible) ? (openBlock(), createElementBlock("div", _hoisted_4, [
          createElementVNode("div", {
            class: normalizeClass(
              unref(isShowLeaveRoomDialog) ? "end-dialog-leave" : "end-dialog-dismiss"
            )
          }, [
            unref(currentDialogType) === unref(DialogType).BasicDialog ? (openBlock(), createElementBlock("div", _hoisted_5, [
              unref(roomStore).isMaster ? (openBlock(), createElementBlock("div", _hoisted_6, [
                unref(roomStore).isMaster ? (openBlock(), createElementBlock("span", _hoisted_7, toDisplayString(unref(t)(
                  "If you do not want to end the meeting, please designate a new host before leaving the meeting."
                )), 1)) : (openBlock(), createElementBlock("span", _hoisted_8, toDisplayString(unref(t)("Are you sure you want to leave this room?")), 1))
              ])) : createCommentVNode("", true)
            ])) : createCommentVNode("", true),
            unref(currentDialogType) === unref(DialogType).BasicDialog ? (openBlock(), createElementBlock("div", _hoisted_9, [
              unref(roomStore).isMaster ? (openBlock(), createElementBlock("span", {
                key: 0,
                class: normalizeClass(
                  unref(isShowLeaveRoomDialog) ? "end-button-dismiss" : "end-button-dismiss-single"
                ),
                onClick: withModifiers(dismissRoom, ["stop"])
              }, toDisplayString(unref(t)("Dismiss")), 3)) : createCommentVNode("", true),
              unref(isShowLeaveRoomDialog) ? withDirectives((openBlock(), createElementBlock("span", {
                key: 1,
                class: normalizeClass(
                  unref(roomStore).isMaster ? "end-button-leave" : "end-button-leave-single"
                )
              }, [
                createTextVNode(toDisplayString(unref(t)("Leave")), 1)
              ], 2)), [
                [unref(vTap), handleEndLeaveClick]
              ]) : createCommentVNode("", true),
              createElementVNode("span", {
                class: "end-button-cancel",
                onClick: _cache[0] || (_cache[0] = withModifiers(
                  //@ts-ignore
                  (...args) => unref(cancel) && unref(cancel)(...args),
                  ["stop"]
                ))
              }, toDisplayString(unref(t)("Cancel")), 1)
            ])) : createCommentVNode("", true),
            unref(currentDialogType) === unref(DialogType).TransferDialog ? (openBlock(), createElementBlock("div", _hoisted_10, [
              createElementVNode("span", {
                class: "end-button-cancel",
                onClick: _cache[1] || (_cache[1] = withModifiers(
                  //@ts-ignore
                  (...args) => unref(cancel) && unref(cancel)(...args),
                  ["stop"]
                ))
              }, toDisplayString(unref(t)("Cancel")), 1)
            ])) : createCommentVNode("", true)
          ], 2)
        ])) : createCommentVNode("", true),
        withDirectives(createVNode(popup, {
          title: unref(t)("Appoint a new host"),
          class: "transfer-container"
        }, {
          sidebarContent: withCtx(() => [
            createElementVNode("div", _hoisted_11, [
              createElementVNode("div", _hoisted_12, [
                createElementVNode("div", _hoisted_13, [
                  createElementVNode("div", _hoisted_14, [
                    createVNode(SvgIcon, { icon: SearchIcon }),
                    withDirectives(createElementVNode("input", {
                      "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => isRef(searchName) ? searchName.value = $event : null),
                      type: "text",
                      class: "searching-input",
                      placeholder: unref(t)("Search for conference attendees"),
                      enterkeyhint: "done"
                    }, null, 8, _hoisted_15), [
                      [vModelText, unref(searchName)]
                    ])
                  ])
                ]),
                createElementVNode("div", _hoisted_16, [
                  (openBlock(true), createElementBlock(Fragment, null, renderList(unref(filteredList), (user) => {
                    return openBlock(), createElementBlock("div", {
                      key: user.userId,
                      class: "transfer-list-content",
                      onClick: ($event) => unref(handleShowMemberControl)(user.userId)
                    }, [
                      createElementVNode("div", _hoisted_18, [
                        createVNode(Avatar, {
                          class: "avatar-url",
                          "img-src": user.avatarUrl
                        }, null, 8, ["img-src"]),
                        createElementVNode("div", _hoisted_19, toDisplayString(unref(roomService).getDisplayName(user)), 1),
                        unref(selectedUser) === user.userId ? (openBlock(), createBlock(SvgIcon, {
                          key: 0,
                          icon: CorrectIcon,
                          class: "correct"
                        })) : createCommentVNode("", true)
                      ])
                    ], 8, _hoisted_17);
                  }), 128)),
                  unref(hasNoData) ? (openBlock(), createElementBlock("div", _hoisted_20, toDisplayString(unref(t)("No relevant user found.")), 1)) : createCommentVNode("", true)
                ])
              ])
            ])
          ]),
          sidebarFooter: withCtx(() => [
            createElementVNode("div", {
              class: "transfer-leave",
              onClick: transferAndLeave
            }, [
              createElementVNode("span", _hoisted_21, toDisplayString(unref(t)("Transfer and leave")), 1)
            ])
          ]),
          _: 1
        }, 8, ["title"]), [
          [vShow, unref(showSideBar)]
        ])
      ]);
    };
  }
});
export {
  _sfc_main as default
};
