"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const uikitBaseComponentVue3 = require("@tencentcloud/uikit-base-component-vue3");
const room = require("tuikit-atomicx-vue3/room");
const IconButton = require("../base/IconButton.vue2.js");
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  __name: "index",
  setup(__props) {
    const { currentRoom } = room.useRoomState();
    const { localParticipant, participantWithScreen } = room.useRoomParticipantState();
    const { screenStatus, screenLastError, startScreenShare, stopScreenShare } = room.useDeviceState();
    const { t } = uikitBaseComponentVue3.useUIKit();
    const isLocalScreenShareUser = vue.computed(() => {
      var _a;
      return participantWithScreen.value && participantWithScreen.value.userId === ((_a = localParticipant.value) == null ? void 0 : _a.userId);
    });
    const isScreenShareDisabled = vue.computed(() => {
      var _a, _b;
      return ((_a = currentRoom.value) == null ? void 0 : _a.isAllScreenShareDisabled) && ((_b = localParticipant.value) == null ? void 0 : _b.role) === room.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;
        }
        uikitBaseComponentVue3.TUIToast.warning({
          message
        });
      }
    }
    async function handleScreenShare() {
      if (screenStatus.value === room.DeviceStatus.On) {
        uikitBaseComponentVue3.TUIMessageBox.confirm({
          title: t("ScreenShare.EndSharing"),
          content: t("ScreenShare.StopSharingConfirm"),
          callback: async (action) => {
            if (action === "confirm") {
              await stopScreenShare();
            }
          }
        });
      } else {
        if (isScreenShareDisabled.value) {
          uikitBaseComponentVue3.TUIToast.warning({
            message: t("ScreenShare.NotAllowedToShareScreen")
          });
          return;
        }
        if (participantWithScreen.value) {
          uikitBaseComponentVue3.TUIToast.warning({
            message: t("ScreenShare.AnotherIsSharingTheScreen")
          });
          return;
        }
        if (screenLastError.value === room.DeviceError.NotSupportCapture) {
          uikitBaseComponentVue3.TUIToast.warning({
            message: t("ScreenShare.BrowserDoesNotSupportScreenSharing")
          });
          return;
        }
        uikitBaseComponentVue3.TUIMessageBox.confirm({
          title: t("ScreenShare.StartSharing"),
          content: t("ScreenShare.StartSharingConfirm"),
          callback: async (action) => {
            if (action === "confirm") {
              await handleStartScreenShare();
            }
          }
        });
      }
    }
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createBlock(IconButton.default, {
        title: vue.unref(t)("ScreenShare.Title"),
        disabled: isScreenShareDisabled.value,
        onClickIcon: handleScreenShare
      }, {
        default: vue.withCtx(() => [
          vue.unref(screenStatus) === vue.unref(room.DeviceStatus).On ? (vue.openBlock(), vue.createBlock(vue.unref(uikitBaseComponentVue3.IconStopScreenShare), {
            key: 0,
            size: "24"
          })) : vue.createCommentVNode("", true),
          vue.unref(screenStatus) === vue.unref(room.DeviceStatus).Off ? (vue.openBlock(), vue.createBlock(vue.unref(uikitBaseComponentVue3.IconScreenShare), {
            key: 1,
            size: "24"
          })) : vue.createCommentVNode("", true),
          vue.unref(screenLastError) !== vue.unref(room.DeviceError).NoError ? (vue.openBlock(), vue.createBlock(vue.unref(uikitBaseComponentVue3.IconUnSupport), {
            key: 2,
            class: "un-support-icon",
            size: "14"
          })) : vue.createCommentVNode("", true)
        ]),
        _: 1
      }, 8, ["title", "disabled"]);
    };
  }
});
exports.default = _sfc_main;
