import { defineComponent, ref, computed, createElementBlock, openBlock, Fragment, createVNode, unref, withCtx, createTextVNode, toDisplayString, createElementVNode } from "vue";
import { TUIErrorCode } from "@tencentcloud/tuiroom-engine-js";
import { useUIKit, TUIButton, TUIDialog, TUIToast, TUIMessageBox } from "@tencentcloud/uikit-base-component-vue3";
import { useRoomState, useRoomParticipantState, RoomParticipantRole } from "tuikit-atomicx-vue3/room";
const _hoisted_1 = { class: "dialog-message" };
const _hoisted_2 = { class: "button-container" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "WebinarLeave",
  props: {
    emitFunction: { type: Function }
  },
  setup(__props) {
    const { t } = useUIKit();
    const props = __props;
    const {
      currentRoom,
      leaveRoom: leaveRoomStateApi,
      endRoom: endRoomStateApi
    } = useRoomState();
    const {
      localParticipant,
      participantList
    } = useRoomParticipantState();
    const showConfirmDialog = ref(false);
    const isEnding = ref(false);
    const otherParticipants = computed(
      () => participantList.value.filter(
        (participant) => {
          var _a;
          return participant.userId !== ((_a = localParticipant.value) == null ? void 0 : _a.userId);
        }
      )
    );
    const dialogMessage = 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 === TUIErrorCode.ERR_INVALID_PARAMETER) {
          return Promise.resolve();
        }
        throw _error;
      }
    };
    const endRoom = async () => {
      try {
        return await endRoomStateApi();
      } catch (_error) {
        if (_error.code === 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) {
        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) {
        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) === RoomParticipantRole.Owner;
      if (!isOwner) {
        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 openBlock(), createElementBlock(Fragment, null, [
        createVNode(unref(TUIButton), {
          color: "red",
          disabled: !((_a = unref(currentRoom)) == null ? void 0 : _a.roomId),
          onClick: handleLeaveRoom
        }, {
          default: withCtx(() => [
            createTextVNode(toDisplayString(unref(t)("Room.LeaveRoom")), 1)
          ]),
          _: 1
        }, 8, ["disabled"]),
        createVNode(unref(TUIDialog), {
          visible: showConfirmDialog.value,
          "onUpdate:visible": _cache[1] || (_cache[1] = ($event) => showConfirmDialog.value = $event),
          title: unref(t)("Room.ConfirmLeaveTitle")
        }, {
          footer: withCtx(() => [
            createElementVNode("div", _hoisted_2, [
              createVNode(unref(TUIButton), {
                color: "red",
                style: { "min-width": "88px" },
                loading: isEnding.value,
                onClick: handleEndRoom
              }, {
                default: withCtx(() => [
                  createTextVNode(toDisplayString(unref(t)("Room.EndRoom")), 1)
                ]),
                _: 1
              }, 8, ["loading"]),
              createVNode(unref(TUIButton), {
                type: "primary",
                style: { "min-width": "88px" },
                onClick: handleLeaveFromConfirmDialog
              }, {
                default: withCtx(() => [
                  createTextVNode(toDisplayString(unref(t)("Room.LeaveRoom")), 1)
                ]),
                _: 1
              }),
              createVNode(unref(TUIButton), {
                style: { "min-width": "88px" },
                onClick: _cache[0] || (_cache[0] = ($event) => showConfirmDialog.value = false)
              }, {
                default: withCtx(() => [
                  createTextVNode(toDisplayString(unref(t)("Room.Cancel")), 1)
                ]),
                _: 1
              })
            ])
          ]),
          default: withCtx(() => [
            createElementVNode("div", _hoisted_1, toDisplayString(dialogMessage.value), 1)
          ]),
          _: 1
        }, 8, ["visible", "title"])
      ], 64);
    };
  }
});
export {
  _sfc_main as default
};
