"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 IconButtonH5 = require("../base/IconButtonH5.vue2.js");
const vClickOutside = require("../base/vClickOutside.js");
const _hoisted_1 = { class: "video-control-container" };
const _hoisted_2 = { class: "video-icon-container" };
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  __name: "index",
  props: {
    cameraTestContainer: {},
    showDescription: { type: Boolean, default: true },
    customStyle: { default: () => ({}) }
  },
  setup(__props) {
    const props = __props;
    const { t } = uikitBaseComponentVue3.useUIKit();
    const { handleErrorWithModal } = room.useRoomModal();
    const { cameraStatus, cameraLastError, isCameraTesting, startCameraTest, stopCameraTest, openLocalCamera, closeLocalCamera } = room.useDeviceState();
    const { currentRoom } = room.useRoomState();
    const { localParticipant } = room.useRoomParticipantState();
    const showVideoSettingTab = vue.ref(false);
    const title = vue.computed(() => {
      if (!props.showDescription) {
        return "";
      }
      if (!currentRoom.value) {
        return isCameraTesting.value ? t("Camera.Stop") : t("Camera.Start");
      }
      return cameraStatus.value === room.DeviceStatus.On ? t("Camera.Stop") : t("Camera.Start");
    });
    const isCameraDisabled = vue.computed(() => {
      var _a, _b, _c;
      if (((_a = localParticipant.value) == null ? void 0 : _a.role) === room.RoomParticipantRole.Owner || ((_b = localParticipant.value) == null ? void 0 : _b.role) === room.RoomParticipantRole.Admin) {
        return false;
      }
      if (cameraStatus.value === room.DeviceStatus.On) {
        return false;
      }
      return (_c = currentRoom.value) == null ? void 0 : _c.isAllCameraDisabled;
    });
    const hasNotSupportError = vue.computed(() => cameraLastError.value !== room.DeviceError.NoError);
    async function handleClickIcon() {
      var _a;
      try {
        if (isCameraDisabled.value) {
          uikitBaseComponentVue3.TUIToast.warning({
            message: t("Camera.Disabled")
          });
          return;
        }
        if (!currentRoom.value && props.cameraTestContainer) {
          if (isCameraTesting.value) {
            await stopCameraTest();
          } else {
            await startCameraTest({ view: props.cameraTestContainer });
          }
          return;
        }
        if (((_a = localParticipant.value) == null ? void 0 : _a.cameraStatus) === room.DeviceStatus.On) {
          await closeLocalCamera();
        } else {
          await openLocalCamera();
        }
      } catch (error) {
        handleErrorWithModal(error);
        handleErrorWithToast();
      }
    }
    function handleErrorWithToast() {
      if (cameraLastError.value === room.DeviceError.NoError) {
        return;
      }
      let message = "";
      switch (cameraLastError.value) {
        case room.DeviceError.NotSupportCapture:
          message = t("Camera.NotSupportCapture");
          break;
        case room.DeviceError.NoSystemPermission:
          message = t("Camera.NoSystemPermission");
          break;
        case room.DeviceError.OccupiedError:
          message = t("Camera.OccupiedError");
          break;
        case room.DeviceError.NoDeviceDetected:
          message = t("Camera.NoDeviceDetected");
          break;
        default:
          message = t("Camera.UnknownError");
          break;
      }
      uikitBaseComponentVue3.TUIToast.warning({
        message
      });
    }
    function handleHideVideoSettingTab() {
      if (showVideoSettingTab.value) {
        showVideoSettingTab.value = false;
      }
    }
    return (_ctx, _cache) => {
      return vue.withDirectives((vue.openBlock(), vue.createElementBlock("div", _hoisted_1, [
        vue.createVNode(IconButtonH5.default, {
          style: vue.normalizeStyle(props.customStyle),
          title: title.value,
          "has-more": true,
          disabled: isCameraDisabled.value,
          onClick: handleClickIcon
        }, {
          default: vue.withCtx(() => [
            vue.createElementVNode("div", _hoisted_2, [
              !vue.unref(currentRoom) ? (vue.openBlock(), vue.createElementBlock(vue.Fragment, { key: 0 }, [
                vue.unref(isCameraTesting) ? (vue.openBlock(), vue.createBlock(vue.unref(uikitBaseComponentVue3.IconCameraOn), {
                  key: 0,
                  size: "24"
                })) : (vue.openBlock(), vue.createBlock(vue.unref(uikitBaseComponentVue3.IconCameraOff), {
                  key: 1,
                  size: "24"
                }))
              ], 64)) : (vue.openBlock(), vue.createElementBlock(vue.Fragment, { key: 1 }, [
                vue.unref(cameraStatus) === vue.unref(room.DeviceStatus).On ? (vue.openBlock(), vue.createBlock(vue.unref(uikitBaseComponentVue3.IconCameraOn), {
                  key: 0,
                  size: "24"
                })) : (vue.openBlock(), vue.createBlock(vue.unref(uikitBaseComponentVue3.IconCameraOff), {
                  key: 1,
                  size: "24"
                }))
              ], 64)),
              hasNotSupportError.value ? (vue.openBlock(), vue.createBlock(vue.unref(uikitBaseComponentVue3.IconUnSupport), {
                key: 2,
                class: "un-support-icon",
                size: "14"
              })) : vue.createCommentVNode("", true)
            ])
          ]),
          _: 1
        }, 8, ["style", "title", "disabled"])
      ])), [
        [vue.unref(vClickOutside.default), handleHideVideoSettingTab]
      ]);
    };
  }
});
exports.default = _sfc_main;
