import { defineComponent, ref, computed, withDirectives, createElementBlock, openBlock, createVNode, normalizeStyle, withCtx, createElementVNode, createBlock, createCommentVNode, unref } from "vue";
import { useUIKit, IconUnSupport, TUIToast } from "@tencentcloud/uikit-base-component-vue3";
import { useRoomModal, useRoomState, useDeviceState, useRoomParticipantState, RoomParticipantRole, DeviceStatus, DeviceError } from "tuikit-atomicx-vue3/room";
import IconButtonH5 from "../base/IconButtonH5.vue2.mjs";
import vClickOutside from "../base/vClickOutside.mjs";
import AudioIcon from "./AudioIcon.vue2.mjs";
const _hoisted_1 = { class: "audio-control-container" };
const _hoisted_2 = { class: "audio-icon-container" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  props: {
    showDescription: { type: Boolean, default: true },
    customStyle: { default: () => ({}) }
  },
  setup(__props) {
    const props = __props;
    const { t } = useUIKit();
    const { handleErrorWithModal } = useRoomModal();
    const { currentRoom } = useRoomState();
    const {
      currentMicVolume,
      testingMicVolume,
      microphoneStatus,
      microphoneLastError,
      isMicrophoneTesting,
      openLocalMicrophone,
      startMicrophoneTest,
      stopMicrophoneTest
    } = useDeviceState();
    const { localParticipant, muteMicrophone, unmuteMicrophone } = useRoomParticipantState();
    const showAudioSettingTab = ref(false);
    const isMicrophoneDisabled = 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 (microphoneStatus.value === DeviceStatus.On) {
        return false;
      }
      return (_c = currentRoom.value) == null ? void 0 : _c.isAllMicrophoneDisabled;
    });
    const isMuted = computed(() => {
      if (!(currentRoom == null ? void 0 : currentRoom.value)) {
        return !isMicrophoneTesting.value;
      }
      return microphoneStatus.value !== DeviceStatus.On;
    });
    const title = computed(() => {
      if (!props.showDescription) {
        return "";
      }
      return isMuted.value ? t("Microphone.Unmute") : t("Microphone.Mute");
    });
    const hasNotSupportError = computed(() => DeviceError.NoError !== microphoneLastError.value);
    async function handleClickIcon() {
      try {
        if (isMicrophoneDisabled.value) {
          TUIToast.warning({
            message: t("Microphone.Disabled")
          });
          return;
        }
        if (!currentRoom.value) {
          if (isMicrophoneTesting.value) {
            await stopMicrophoneTest();
          } else {
            await startMicrophoneTest({ interval: 200 });
          }
          return;
        }
        if (microphoneStatus.value === DeviceStatus.On) {
          await muteMicrophone();
        } else {
          await openLocalMicrophone();
          await unmuteMicrophone();
        }
      } catch (error) {
        handleErrorWithModal(error);
        handleErrorWithToast();
      }
    }
    function handleErrorWithToast() {
      if (microphoneLastError.value === DeviceError.NoError) {
        return;
      }
      let message = "";
      switch (microphoneLastError.value) {
        case DeviceError.NotSupportCapture:
          message = t("Microphone.NotSupportCapture");
          break;
        case DeviceError.NoSystemPermission:
          message = t("Microphone.NoSystemPermission");
          break;
        case DeviceError.OccupiedError:
          message = t("Microphone.OccupiedError");
          break;
        case DeviceError.NoDeviceDetected:
          message = t("Microphone.NoDeviceDetected");
          break;
        default:
          message = t("Microphone.UnknownError");
          break;
      }
      TUIToast.warning({
        message
      });
    }
    function handleHideAudioSettingTab() {
      if (showAudioSettingTab.value) {
        showAudioSettingTab.value = false;
      }
    }
    return (_ctx, _cache) => {
      return withDirectives((openBlock(), createElementBlock("div", _hoisted_1, [
        createVNode(IconButtonH5, {
          style: normalizeStyle(props.customStyle),
          title: title.value,
          disabled: isMicrophoneDisabled.value,
          onClick: handleClickIcon
        }, {
          default: withCtx(() => [
            createElementVNode("div", _hoisted_2, [
              createVNode(AudioIcon, {
                "audio-volume": unref(isMicrophoneTesting) ? unref(testingMicVolume) : unref(currentMicVolume),
                "is-muted": isMuted.value
              }, null, 8, ["audio-volume", "is-muted"]),
              hasNotSupportError.value ? (openBlock(), createBlock(unref(IconUnSupport), {
                key: 0,
                class: "un-support-icon",
                size: "14"
              })) : createCommentVNode("", true)
            ])
          ]),
          _: 1
        }, 8, ["style", "title", "disabled"])
      ])), [
        [unref(vClickOutside), handleHideAudioSettingTab]
      ]);
    };
  }
});
export {
  _sfc_main as default
};
