import { defineComponent, computed, ref, onBeforeUnmount, openBlock, createElementBlock, normalizeClass, createElementVNode, toDisplayString, unref, createVNode, createBlock, withCtx, createTextVNode, createCommentVNode, Fragment, renderList } from "vue";
import DeviceSelect from "./DeviceSelect.vue.mjs";
import { useRoomStore } from "../../stores/room.mjs";
import { SettingMode } from "../../constants/render.mjs";
import { useI18n } from "../../locales/index.mjs";
import { storeToRefs } from "pinia";
import { useBasicStore } from "../../stores/basic.mjs";
import { isElectron } from "../../utils/environment.mjs";
import useGetRoomEngine from "../../hooks/useRoomEngine.mjs";
import TuiButton from "./base/Button.vue.mjs";
const _hoisted_1 = { class: "item-setting" };
const _hoisted_2 = { class: "title" };
const _hoisted_3 = { class: "flex" };
const _hoisted_4 = { class: "item-setting" };
const _hoisted_5 = { class: "title" };
const _hoisted_6 = { class: "mic-bar-container" };
const _hoisted_7 = {
  key: 0,
  class: "item-setting"
};
const _hoisted_8 = { class: "title" };
const _hoisted_9 = { class: "flex" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "AudioSettingTab",
  props: {
    mode: {},
    audioVolume: {},
    theme: {}
  },
  setup(__props) {
    var _a;
    const props = __props;
    const settingMode = props.mode || SettingMode.SIMPLE;
    const isSampleMode = computed(() => settingMode === SettingMode.SIMPLE);
    const isDetailMode = computed(() => settingMode === SettingMode.DETAIL);
    const basicStore = useBasicStore();
    const { userId } = storeToRefs(basicStore);
    const roomStore = useRoomStore();
    const { speakerList, userVolumeObj, currentSpeakerId } = storeToRefs(roomStore);
    const roomEngine = useGetRoomEngine();
    const trtcCloud = (_a = roomEngine.instance) == null ? void 0 : _a.getTRTCCloud();
    const themeClass = computed(
      () => props.theme ? `tui-theme-${props.theme}` : ""
    );
    const volumeTotalNum = computed(() => isDetailMode.value ? 36 : 28);
    const volumeNum = computed(() => {
      const volume = props.audioVolume || userVolumeObj.value[userId.value] || 0;
      return volume * volumeTotalNum.value / 100;
    });
    const showVolume = computed(
      () => isSampleMode.value || isDetailMode.value && isTestingMicrophone.value
    );
    const isTestingMicrophone = ref(false);
    function handleMicrophoneTest() {
      isTestingMicrophone.value = !isTestingMicrophone.value;
    }
    const isTestingSpeaker = ref(false);
    const audioPlayer = document == null ? void 0 : document.createElement("audio");
    audioPlayer.loop = true;
    const { t } = useI18n();
    async function handleSpeakerTest() {
      const SPEAKER_TEST_URL = "https://web.sdk.qcloud.com/trtc/electron/download/resources/media/TestSpeaker.mp3";
      isTestingSpeaker.value = !isTestingSpeaker.value;
      const isStartSpeakerTest = isTestingSpeaker.value;
      if (isElectron) {
        if (isStartSpeakerTest) {
          trtcCloud == null ? void 0 : trtcCloud.startSpeakerDeviceTest(SPEAKER_TEST_URL);
        } else {
          trtcCloud == null ? void 0 : trtcCloud.stopSpeakerDeviceTest();
        }
        return;
      }
      if (!audioPlayer) {
        return;
      }
      if (isStartSpeakerTest) {
        await (audioPlayer == null ? void 0 : audioPlayer.setSinkId(currentSpeakerId.value));
        audioPlayer.src = SPEAKER_TEST_URL;
        audioPlayer.play();
      } else {
        audioPlayer.pause();
        audioPlayer.currentTime = 0;
      }
    }
    onBeforeUnmount(() => {
      if (isElectron) {
        trtcCloud == null ? void 0 : trtcCloud.stopSpeakerDeviceTest();
        return;
      }
      audioPlayer && audioPlayer.pause();
    });
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", {
        class: normalizeClass(["audio-setting-tab", themeClass.value])
      }, [
        createElementVNode("div", _hoisted_1, [
          createElementVNode("span", _hoisted_2, toDisplayString(unref(t)("Mic")), 1),
          createElementVNode("div", _hoisted_3, [
            createVNode(DeviceSelect, {
              class: "select",
              "device-type": "microphone"
            }),
            isDetailMode.value ? (openBlock(), createBlock(TuiButton, {
              key: 0,
              class: "button",
              type: "primary",
              onClick: handleMicrophoneTest
            }, {
              default: withCtx(() => [
                createTextVNode(toDisplayString(isTestingMicrophone.value ? unref(t)("Stop") : unref(t)("Test")), 1)
              ]),
              _: 1
            })) : createCommentVNode("", true)
          ])
        ]),
        createElementVNode("div", _hoisted_4, [
          createElementVNode("span", _hoisted_5, toDisplayString(unref(t)("Output")), 1),
          createElementVNode("div", _hoisted_6, [
            (openBlock(true), createElementBlock(Fragment, null, renderList(new Array(volumeTotalNum.value).fill(""), (item, index) => {
              return openBlock(), createElementBlock("div", {
                key: index,
                class: normalizeClass([
                  "mic-bar",
                  `${showVolume.value && volumeNum.value > index ? "active" : ""}`
                ])
              }, null, 2);
            }), 128))
          ])
        ]),
        unref(speakerList).length > 0 ? (openBlock(), createElementBlock("div", _hoisted_7, [
          createElementVNode("span", _hoisted_8, toDisplayString(unref(t)("Speaker")), 1),
          createElementVNode("div", _hoisted_9, [
            createVNode(DeviceSelect, {
              class: "select",
              "device-type": "speaker",
              disabled: _ctx.mode === unref(SettingMode).DETAIL
            }, null, 8, ["disabled"]),
            isDetailMode.value ? (openBlock(), createBlock(TuiButton, {
              key: 0,
              class: "button",
              type: "primary",
              onClick: handleSpeakerTest
            }, {
              default: withCtx(() => [
                createTextVNode(toDisplayString(isTestingSpeaker.value ? unref(t)("Stop") : unref(t)("Test")), 1)
              ]),
              _: 1
            })) : createCommentVNode("", true)
          ])
        ])) : createCommentVNode("", true)
      ], 2);
    };
  }
});
export {
  _sfc_main as default
};
