import { defineComponent, openBlock, createElementBlock, createVNode, withCtx, createTextVNode, toDisplayString, unref, isRef, createBlock, createCommentVNode, createElementVNode, Fragment, renderList } from "vue";
import TuiButton from "../../common/base/Button.vue.mjs";
import Select from "../../common/base/Select/index.mjs";
import OPtion from "../../common/base/Option/index.mjs";
import Dialog from "../../common/base/Dialog/index.mjs";
import { TUIRole } from "@tencentcloud/tuiroom-engine-js";
import useEndControl from "./useEndControlHooks.mjs";
import logger from "../../../utils/common/logger/index.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 = { key: 0 };
const _hoisted_3 = { key: 1 };
const _hoisted_4 = { key: 0 };
const _hoisted_5 = { key: 1 };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "EndControlPC",
  setup(__props) {
    const {
      t,
      isShowLeaveRoomDialog,
      roomStore,
      roomEngine,
      stopMeeting,
      cancel,
      selectedUser,
      DialogType,
      showEndDialogContent,
      logPrefix,
      title,
      showEndButtonContent,
      currentDialogType,
      visible,
      resetState,
      isMasterWithOneRemoteUser,
      isMasterWithRemoteUser,
      remoteEnteredUserList
    } = 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;
      }
    }
    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", _hoisted_1, [
        createVNode(TuiButton, {
          type: "primary",
          class: "end-button",
          onClick: handleEndBtnClick
        }, {
          default: withCtx(() => [
            createTextVNode(toDisplayString(unref(showEndButtonContent)), 1)
          ]),
          _: 1
        }),
        createVNode(unref(Dialog), {
          modelValue: unref(visible),
          "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => isRef(visible) ? visible.value = $event : null),
          title: unref(title),
          modal: true,
          width: "480px",
          "before-close": unref(cancel),
          "close-on-click-modal": true,
          "append-to-room-container": true
        }, {
          footer: withCtx(() => [
            unref(currentDialogType) === unref(DialogType).BasicDialog ? (openBlock(), createElementBlock("div", _hoisted_4, [
              unref(roomStore).isMaster ? (openBlock(), createBlock(TuiButton, {
                key: 0,
                class: "button",
                size: "default",
                onClick: dismissRoom
              }, {
                default: withCtx(() => [
                  createTextVNode(toDisplayString(unref(t)("Dismiss")), 1)
                ]),
                _: 1
              })) : createCommentVNode("", true),
              unref(isShowLeaveRoomDialog) ? (openBlock(), createBlock(TuiButton, {
                key: 1,
                class: "button",
                size: "default",
                onClick: handleEndLeaveClick
              }, {
                default: withCtx(() => [
                  createTextVNode(toDisplayString(unref(t)("Leave")), 1)
                ]),
                _: 1
              })) : createCommentVNode("", true),
              createVNode(TuiButton, {
                class: "button",
                type: "primary",
                size: "default",
                onClick: unref(cancel)
              }, {
                default: withCtx(() => [
                  createTextVNode(toDisplayString(unref(t)("Cancel")), 1)
                ]),
                _: 1
              }, 8, ["onClick"])
            ])) : createCommentVNode("", true),
            unref(currentDialogType) === unref(DialogType).TransferDialog ? (openBlock(), createElementBlock("div", _hoisted_5, [
              createVNode(TuiButton, {
                class: "button",
                size: "default",
                onClick: transferAndLeave
              }, {
                default: withCtx(() => [
                  createTextVNode(toDisplayString(unref(t)("Transfer and leave")), 1)
                ]),
                _: 1
              }),
              createVNode(TuiButton, {
                class: "button",
                size: "default",
                type: "primary",
                onClick: unref(cancel)
              }, {
                default: withCtx(() => [
                  createTextVNode(toDisplayString(unref(t)("Cancel")), 1)
                ]),
                _: 1
              }, 8, ["onClick"])
            ])) : createCommentVNode("", true)
          ]),
          default: withCtx(() => [
            unref(currentDialogType) === unref(DialogType).BasicDialog ? (openBlock(), createElementBlock("div", _hoisted_2, [
              createElementVNode("span", null, toDisplayString(unref(showEndDialogContent)), 1)
            ])) : createCommentVNode("", true),
            unref(currentDialogType) === unref(DialogType).TransferDialog ? (openBlock(), createElementBlock("div", _hoisted_3, [
              createElementVNode("div", null, toDisplayString(unref(t)("New host")), 1),
              createElementVNode("div", null, [
                createVNode(unref(Select), {
                  modelValue: unref(selectedUser),
                  "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => isRef(selectedUser) ? selectedUser.value = $event : null),
                  teleported: false,
                  "popper-append-to-body": false,
                  theme: "white"
                }, {
                  default: withCtx(() => [
                    (openBlock(true), createElementBlock(Fragment, null, renderList(unref(remoteEnteredUserList), (user) => {
                      return openBlock(), createBlock(unref(OPtion), {
                        key: user.userId,
                        value: user.userId,
                        label: user.nameCard || user.userName,
                        theme: "white"
                      }, null, 8, ["value", "label"]);
                    }), 128))
                  ]),
                  _: 1
                }, 8, ["modelValue"])
              ])
            ])) : createCommentVNode("", true)
          ]),
          _: 1
        }, 8, ["modelValue", "title", "before-close"])
      ]);
    };
  }
});
export {
  _sfc_main as default
};
