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