"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const TUIRoomEngine = require("@tencentcloud/tuiroom-engine-js");
const uikitBaseComponentVue3 = require("@tencentcloud/uikit-base-component-vue3");
const room = require("tuikit-atomicx-vue3/room");
const _hoisted_1 = { class: "dialog-message" };
const _hoisted_2 = { class: "button-container" };
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  __name: "WebinarLeave",
  props: {
    emitFunction: { type: Function }
  },
  setup(__props) {
    const { t } = uikitBaseComponentVue3.useUIKit();
    const props = __props;
    const {
      currentRoom,
      leaveRoom: leaveRoomStateApi,
      endRoom: endRoomStateApi
    } = room.useRoomState();
    const {
      localParticipant,
      participantList
    } = room.useRoomParticipantState();
    const showConfirmDialog = vue.ref(false);
    const isEnding = vue.ref(false);
    const otherParticipants = vue.computed(
      () => participantList.value.filter(
        (participant) => {
          var _a;
          return participant.userId !== ((_a = localParticipant.value) == null ? void 0 : _a.userId);
        }
      )
    );
    const dialogMessage = vue.computed(() => {
      if (otherParticipants.value.length === 0) {
        return t("Room.WebinarLeaveRoomTip");
      }
      return t("Room.WebinarConfirmLeaveTip");
    });
    const leaveRoom = async () => {
      try {
        return await leaveRoomStateApi();
      } catch (_error) {
        if (_error.code === TUIRoomEngine.TUIErrorCode.ERR_INVALID_PARAMETER) {
          return Promise.resolve();
        }
        throw _error;
      }
    };
    const endRoom = async () => {
      try {
        return await endRoomStateApi();
      } catch (_error) {
        if (_error.code === TUIRoomEngine.TUIErrorCode.ERR_INVALID_PARAMETER) {
          return Promise.resolve();
        }
        throw _error;
      }
    };
    const performLeave = async () => {
      var _a;
      try {
        if (!((_a = currentRoom.value) == null ? void 0 : _a.roomId)) {
          return;
        }
        await leaveRoom();
        props.emitFunction("leave");
      } catch (_error) {
        uikitBaseComponentVue3.TUIToast.error({ message: t("Room.LeaveRoomFailed") });
      }
    };
    const handleEndRoom = async () => {
      var _a;
      if (!((_a = currentRoom.value) == null ? void 0 : _a.roomId) || isEnding.value) {
        return;
      }
      try {
        isEnding.value = true;
        await endRoom();
        props.emitFunction("end");
        showConfirmDialog.value = false;
      } catch (_error) {
        uikitBaseComponentVue3.TUIToast.error({ message: t("Room.EndRoomFailed") });
      } finally {
        isEnding.value = false;
      }
    };
    const handleLeaveFromConfirmDialog = () => {
      performLeave();
    };
    const handleLeaveRoom = () => {
      var _a, _b;
      if (!((_a = currentRoom.value) == null ? void 0 : _a.roomId)) {
        return;
      }
      const isOwner = ((_b = localParticipant.value) == null ? void 0 : _b.role) === room.RoomParticipantRole.Owner;
      if (!isOwner) {
        uikitBaseComponentVue3.TUIMessageBox.confirm({
          title: t("Room.ConfirmLeaveTitle"),
          content: t("Room.ConfirmLeaveRoom"),
          callback: (action) => {
            if (action === "confirm") {
              performLeave();
            }
          }
        });
        return;
      }
      showConfirmDialog.value = true;
    };
    return (_ctx, _cache) => {
      var _a;
      return vue.openBlock(), vue.createElementBlock(vue.Fragment, null, [
        vue.createVNode(vue.unref(uikitBaseComponentVue3.TUIButton), {
          color: "red",
          disabled: !((_a = vue.unref(currentRoom)) == null ? void 0 : _a.roomId),
          onClick: handleLeaveRoom
        }, {
          default: vue.withCtx(() => [
            vue.createTextVNode(vue.toDisplayString(vue.unref(t)("Room.LeaveRoom")), 1)
          ]),
          _: 1
        }, 8, ["disabled"]),
        vue.createVNode(vue.unref(uikitBaseComponentVue3.TUIDialog), {
          visible: showConfirmDialog.value,
          "onUpdate:visible": _cache[1] || (_cache[1] = ($event) => showConfirmDialog.value = $event),
          title: vue.unref(t)("Room.ConfirmLeaveTitle")
        }, {
          footer: vue.withCtx(() => [
            vue.createElementVNode("div", _hoisted_2, [
              vue.createVNode(vue.unref(uikitBaseComponentVue3.TUIButton), {
                color: "red",
                style: { "min-width": "88px" },
                loading: isEnding.value,
                onClick: handleEndRoom
              }, {
                default: vue.withCtx(() => [
                  vue.createTextVNode(vue.toDisplayString(vue.unref(t)("Room.EndRoom")), 1)
                ]),
                _: 1
              }, 8, ["loading"]),
              vue.createVNode(vue.unref(uikitBaseComponentVue3.TUIButton), {
                type: "primary",
                style: { "min-width": "88px" },
                onClick: handleLeaveFromConfirmDialog
              }, {
                default: vue.withCtx(() => [
                  vue.createTextVNode(vue.toDisplayString(vue.unref(t)("Room.LeaveRoom")), 1)
                ]),
                _: 1
              }),
              vue.createVNode(vue.unref(uikitBaseComponentVue3.TUIButton), {
                style: { "min-width": "88px" },
                onClick: _cache[0] || (_cache[0] = ($event) => showConfirmDialog.value = false)
              }, {
                default: vue.withCtx(() => [
                  vue.createTextVNode(vue.toDisplayString(vue.unref(t)("Room.Cancel")), 1)
                ]),
                _: 1
              })
            ])
          ]),
          default: vue.withCtx(() => [
            vue.createElementVNode("div", _hoisted_1, vue.toDisplayString(dialogMessage.value), 1)
          ]),
          _: 1
        }, 8, ["visible", "title"])
      ], 64);
    };
  }
});
exports.default = _sfc_main;
