import { defineComponent, computed, createBlock, openBlock, unref, withCtx, createCommentVNode } from "vue";
import { useUIKit, IconStopScreenShare, IconScreenShare, IconUnSupport, TUIMessageBox, TUIToast } from "@tencentcloud/uikit-base-component-vue3";
import { useRoomState, useRoomParticipantState, useDeviceState, RoomParticipantRole, DeviceStatus, DeviceError } from "tuikit-atomicx-vue3/room";
import IconButton from "../base/IconButton.vue2.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  setup(__props) {
    const { currentRoom } = useRoomState();
    const { localParticipant, participantWithScreen } = useRoomParticipantState();
    const { screenStatus, screenLastError, startScreenShare, stopScreenShare } = useDeviceState();
    const { t } = useUIKit();
    const isLocalScreenShareUser = computed(() => {
      var _a;
      return participantWithScreen.value && participantWithScreen.value.userId === ((_a = localParticipant.value) == null ? void 0 : _a.userId);
    });
    const isScreenShareDisabled = computed(() => {
      var _a, _b;
      return ((_a = currentRoom.value) == null ? void 0 : _a.isAllScreenShareDisabled) && ((_b = localParticipant.value) == null ? void 0 : _b.role) === RoomParticipantRole.GeneralUser && !isLocalScreenShareUser.value;
    });
    async function handleStartScreenShare() {
      try {
        await startScreenShare({ screenAudio: true });
      } catch (error) {
        let message = "";
        switch (error.name) {
          case "NotReadableError":
            message = t("ScreenShare.SystemProhibitsAccessScreenContent");
            break;
          case "NotAllowedError":
            if (error.message.includes("Permission denied by system")) {
              message = t("ScreenShare.SystemProhibitsAccessScreenContent");
            } else {
              message = t("ScreenShare.UserCanceledScreenSharing");
            }
            break;
          default:
            message = t("ScreenShare.UnknownErrorOccurredWhileSharing");
            break;
        }
        TUIToast.warning({
          message
        });
      }
    }
    async function handleScreenShare() {
      if (screenStatus.value === DeviceStatus.On) {
        TUIMessageBox.confirm({
          title: t("ScreenShare.EndSharing"),
          content: t("ScreenShare.StopSharingConfirm"),
          callback: async (action) => {
            if (action === "confirm") {
              await stopScreenShare();
            }
          }
        });
      } else {
        if (isScreenShareDisabled.value) {
          TUIToast.warning({
            message: t("ScreenShare.NotAllowedToShareScreen")
          });
          return;
        }
        if (participantWithScreen.value) {
          TUIToast.warning({
            message: t("ScreenShare.AnotherIsSharingTheScreen")
          });
          return;
        }
        if (screenLastError.value === DeviceError.NotSupportCapture) {
          TUIToast.warning({
            message: t("ScreenShare.BrowserDoesNotSupportScreenSharing")
          });
          return;
        }
        TUIMessageBox.confirm({
          title: t("ScreenShare.StartSharing"),
          content: t("ScreenShare.StartSharingConfirm"),
          callback: async (action) => {
            if (action === "confirm") {
              await handleStartScreenShare();
            }
          }
        });
      }
    }
    return (_ctx, _cache) => {
      return openBlock(), createBlock(IconButton, {
        title: unref(t)("ScreenShare.Title"),
        disabled: isScreenShareDisabled.value,
        onClickIcon: handleScreenShare
      }, {
        default: withCtx(() => [
          unref(screenStatus) === unref(DeviceStatus).On ? (openBlock(), createBlock(unref(IconStopScreenShare), {
            key: 0,
            size: "24"
          })) : createCommentVNode("", true),
          unref(screenStatus) === unref(DeviceStatus).Off ? (openBlock(), createBlock(unref(IconScreenShare), {
            key: 1,
            size: "24"
          })) : createCommentVNode("", true),
          unref(screenLastError) !== unref(DeviceError).NoError ? (openBlock(), createBlock(unref(IconUnSupport), {
            key: 2,
            class: "un-support-icon",
            size: "14"
          })) : createCommentVNode("", true)
        ]),
        _: 1
      }, 8, ["title", "disabled"]);
    };
  }
});
export {
  _sfc_main as default
};
