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