import { defineComponent, ref, watch, openBlock, createBlock, unref, withCtx, createElementBlock, Fragment, renderList } from "vue";
import { useRoomStore } from "../../stores/room.mjs";
import { storeToRefs } from "pinia";
import Select from "./base/Select/index.mjs";
import OPtion from "./base/Option/index.mjs";
import { TUIMediaDeviceType } from "@tencentcloud/tuiroom-engine-js";
import useDeviceManager from "../../hooks/useDeviceManager.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "DeviceSelect",
  props: {
    deviceType: {},
    onChange: { type: Function },
    disabled: { type: Boolean, default: false }
  },
  setup(__props) {
    const { deviceManager } = useDeviceManager();
    const roomStore = useRoomStore();
    const {
      cameraList,
      microphoneList,
      speakerList,
      currentCameraId,
      currentMicrophoneId,
      currentSpeakerId
    } = storeToRefs(roomStore);
    const deviceList = ref(getDeviceList());
    const currentDeviceId = ref(getInitDeviceId());
    function getInitDeviceId() {
      if (__props.deviceType === "camera") {
        return currentCameraId.value;
      }
      if (__props.deviceType === "microphone") {
        return currentMicrophoneId.value;
      }
      if (__props.deviceType === "speaker") {
        return currentSpeakerId.value;
      }
      return "";
    }
    if (__props.deviceType === "camera") {
      watch(
        currentCameraId,
        (val) => {
          if (currentDeviceId.value !== val) {
            currentDeviceId.value = val;
          }
        },
        { immediate: true }
      );
    }
    if (__props.deviceType === "microphone") {
      watch(
        currentMicrophoneId,
        (val) => {
          if (currentDeviceId.value !== val) {
            currentDeviceId.value = val;
          }
        },
        { immediate: true }
      );
    }
    if (__props.deviceType === "speaker") {
      watch(
        currentSpeakerId,
        (val) => {
          if (currentDeviceId.value !== val) {
            currentDeviceId.value = val;
          }
        },
        { immediate: true }
      );
    }
    function getDeviceList() {
      if (__props.deviceType === "camera") {
        return cameraList;
      }
      if (__props.deviceType === "microphone") {
        return microphoneList;
      }
      if (__props.deviceType === "speaker") {
        return speakerList;
      }
      return [];
    }
    async function handleChange(deviceId) {
      var _a, _b, _c;
      __props.onChange && __props.onChange(deviceId);
      switch (__props.deviceType) {
        case "camera":
          try {
            await ((_a = deviceManager.instance) == null ? void 0 : _a.setCurrentDevice({
              type: TUIMediaDeviceType.kMediaDeviceTypeVideoCamera,
              deviceId
            }));
            roomStore.setCurrentCameraId(deviceId);
          } catch (error) {
            if (cameraList.value.map((item) => item.deviceId).includes(currentCameraId.value)) {
              currentDeviceId.value = currentCameraId.value;
            }
          }
          break;
        case "microphone":
          try {
            await ((_b = deviceManager.instance) == null ? void 0 : _b.setCurrentDevice({
              type: TUIMediaDeviceType.kMediaDeviceTypeAudioInput,
              deviceId
            }));
            roomStore.setCurrentMicrophoneId(deviceId);
          } catch (error) {
            if (microphoneList.value.map((item) => item.deviceId).includes(currentMicrophoneId.value)) {
              currentDeviceId.value = currentMicrophoneId.value;
            }
          }
          break;
        case "speaker":
          try {
            await ((_c = deviceManager.instance) == null ? void 0 : _c.setCurrentDevice({
              type: TUIMediaDeviceType.kMediaDeviceTypeAudioOutput,
              deviceId
            }));
            roomStore.setCurrentSpeakerId(deviceId);
          } catch (error) {
            if (speakerList.value.map((item) => item.deviceId).includes(currentSpeakerId.value)) {
              currentDeviceId.value = currentSpeakerId.value;
            }
          }
          break;
      }
    }
    return (_ctx, _cache) => {
      return openBlock(), createBlock(unref(Select), {
        modelValue: currentDeviceId.value,
        "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => currentDeviceId.value = $event),
        placeholder: "placeholder",
        class: "select",
        disabled: _ctx.disabled,
        teleported: false,
        "popper-append-to-body": false,
        onChange: handleChange
      }, {
        default: withCtx(() => [
          (openBlock(true), createElementBlock(Fragment, null, renderList(deviceList.value, (item) => {
            return openBlock(), createBlock(unref(OPtion), {
              key: item.deviceId,
              label: item.deviceName,
              value: item.deviceId
            }, null, 8, ["label", "value"]);
          }), 128))
        ]),
        _: 1
      }, 8, ["modelValue", "disabled"]);
    };
  }
});
export {
  _sfc_main as default
};
