"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const Button = require("../../common/base/Button.vue.js");
const index$1 = require("../../common/base/Select/index.js");
const index$2 = require("../../common/base/Option/index.js");
const index = require("../../common/base/Dialog/index.js");
const TUIRoomEngine = require("@tencentcloud/tuiroom-engine-js");
const useEndControlHooks = require("./useEndControlHooks.js");
const index$3 = require("../../../utils/common/logger/index.js");
require("../../../services/main.js");
const roomService = require("../../../services/roomService.js");
require("../../../locales/index.js");
require("mitt");
require("../../../services/manager/roomActionManager.js");
require("@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__ */ Vue.defineComponent({
  __name: "EndControlPC",
  setup(__props) {
    const {
      t,
      isShowLeaveRoomDialog,
      roomStore,
      roomEngine,
      stopMeeting,
      cancel,
      selectedUser,
      DialogType,
      showEndDialogContent,
      logPrefix,
      title,
      showEndButtonContent,
      currentDialogType,
      visible,
      resetState,
      isMasterWithOneRemoteUser,
      isMasterWithRemoteUser,
      remoteEnteredUserList
    } = useEndControlHooks.default();
    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 {
        index$3.default.log(`${logPrefix}dismissRoom: enter`);
        resetState();
        await roomService.roomService.dismissRoom();
      } catch (error) {
        index$3.default.error(`${logPrefix}dismissRoom error:`, error);
      }
    }
    async function leaveRoom() {
      try {
        resetState();
        await roomService.roomService.leaveRoom();
      } catch (error) {
        index$3.default.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: TUIRoomEngine.TUIRole.kRoomOwner
        }));
        index$3.default.log(`${logPrefix}transferAndLeave:`, changeUserRoleResponse);
        resetState();
        await roomService.roomService.leaveRoom();
      } catch (error) {
        index$3.default.error(`${logPrefix}transferAndLeave error:`, error);
      }
    }
    return (_ctx, _cache) => {
      return Vue.openBlock(), Vue.createElementBlock("div", _hoisted_1, [
        Vue.createVNode(Button.default, {
          type: "primary",
          class: "end-button",
          onClick: handleEndBtnClick
        }, {
          default: Vue.withCtx(() => [
            Vue.createTextVNode(Vue.toDisplayString(Vue.unref(showEndButtonContent)), 1)
          ]),
          _: 1
        }),
        Vue.createVNode(Vue.unref(index.default), {
          modelValue: Vue.unref(visible),
          "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => Vue.isRef(visible) ? visible.value = $event : null),
          title: Vue.unref(title),
          modal: true,
          width: "480px",
          "before-close": Vue.unref(cancel),
          "close-on-click-modal": true,
          "append-to-room-container": true
        }, {
          footer: Vue.withCtx(() => [
            Vue.unref(currentDialogType) === Vue.unref(DialogType).BasicDialog ? (Vue.openBlock(), Vue.createElementBlock("div", _hoisted_4, [
              Vue.unref(roomStore).isMaster ? (Vue.openBlock(), Vue.createBlock(Button.default, {
                key: 0,
                class: "button",
                size: "default",
                onClick: dismissRoom
              }, {
                default: Vue.withCtx(() => [
                  Vue.createTextVNode(Vue.toDisplayString(Vue.unref(t)("Dismiss")), 1)
                ]),
                _: 1
              })) : Vue.createCommentVNode("", true),
              Vue.unref(isShowLeaveRoomDialog) ? (Vue.openBlock(), Vue.createBlock(Button.default, {
                key: 1,
                class: "button",
                size: "default",
                onClick: handleEndLeaveClick
              }, {
                default: Vue.withCtx(() => [
                  Vue.createTextVNode(Vue.toDisplayString(Vue.unref(t)("Leave")), 1)
                ]),
                _: 1
              })) : Vue.createCommentVNode("", true),
              Vue.createVNode(Button.default, {
                class: "button",
                type: "primary",
                size: "default",
                onClick: Vue.unref(cancel)
              }, {
                default: Vue.withCtx(() => [
                  Vue.createTextVNode(Vue.toDisplayString(Vue.unref(t)("Cancel")), 1)
                ]),
                _: 1
              }, 8, ["onClick"])
            ])) : Vue.createCommentVNode("", true),
            Vue.unref(currentDialogType) === Vue.unref(DialogType).TransferDialog ? (Vue.openBlock(), Vue.createElementBlock("div", _hoisted_5, [
              Vue.createVNode(Button.default, {
                class: "button",
                size: "default",
                onClick: transferAndLeave
              }, {
                default: Vue.withCtx(() => [
                  Vue.createTextVNode(Vue.toDisplayString(Vue.unref(t)("Transfer and leave")), 1)
                ]),
                _: 1
              }),
              Vue.createVNode(Button.default, {
                class: "button",
                size: "default",
                type: "primary",
                onClick: Vue.unref(cancel)
              }, {
                default: Vue.withCtx(() => [
                  Vue.createTextVNode(Vue.toDisplayString(Vue.unref(t)("Cancel")), 1)
                ]),
                _: 1
              }, 8, ["onClick"])
            ])) : Vue.createCommentVNode("", true)
          ]),
          default: Vue.withCtx(() => [
            Vue.unref(currentDialogType) === Vue.unref(DialogType).BasicDialog ? (Vue.openBlock(), Vue.createElementBlock("div", _hoisted_2, [
              Vue.createElementVNode("span", null, Vue.toDisplayString(Vue.unref(showEndDialogContent)), 1)
            ])) : Vue.createCommentVNode("", true),
            Vue.unref(currentDialogType) === Vue.unref(DialogType).TransferDialog ? (Vue.openBlock(), Vue.createElementBlock("div", _hoisted_3, [
              Vue.createElementVNode("div", null, Vue.toDisplayString(Vue.unref(t)("New host")), 1),
              Vue.createElementVNode("div", null, [
                Vue.createVNode(Vue.unref(index$1.default), {
                  modelValue: Vue.unref(selectedUser),
                  "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => Vue.isRef(selectedUser) ? selectedUser.value = $event : null),
                  teleported: false,
                  "popper-append-to-body": false,
                  theme: "white"
                }, {
                  default: Vue.withCtx(() => [
                    (Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(Vue.unref(remoteEnteredUserList), (user) => {
                      return Vue.openBlock(), Vue.createBlock(Vue.unref(index$2.default), {
                        key: user.userId,
                        value: user.userId,
                        label: user.nameCard || user.userName,
                        theme: "white"
                      }, null, 8, ["value", "label"]);
                    }), 128))
                  ]),
                  _: 1
                }, 8, ["modelValue"])
              ])
            ])) : Vue.createCommentVNode("", true)
          ]),
          _: 1
        }, 8, ["modelValue", "title", "before-close"])
      ]);
    };
  }
});
exports.default = _sfc_main;
