import TUIRoomEngine__default, { TUIRoomDeviceMangerEvents, TUIMediaDeviceType, TUIMediaDeviceState } from "@tencentcloud/tuiroom-engine-js";
import useGetRoomEngine from "./useRoomEngine.mjs";
import { useRoomStore } from "../stores/room.mjs";
import logger from "../utils/common/logger/index.mjs";
import { onUnmounted } from "vue";
import i18n from "../locales/index.mjs";
import MessageBox from "../components/common/base/MessageBox/index.mjs";
import { isWeChat, isElectron } from "../utils/environment.mjs";
const roomEngine = useGetRoomEngine();
const deviceManager = {
  instance: null
};
TUIRoomEngine__default.once("ready", () => {
  var _a;
  deviceManager.instance = (_a = roomEngine.instance) == null ? void 0 : _a.getMediaDeviceManager();
});
function useDeviceManager(options) {
  const roomStore = useRoomStore();
  const { t } = i18n.global;
  async function initMediaDeviceList() {
    var _a, _b, _c, _d, _e, _f;
    if (!deviceManager.instance) {
      return;
    }
    const cameraList = await ((_a = deviceManager.instance) == null ? void 0 : _a.getDevicesList({
      type: TUIMediaDeviceType.kMediaDeviceTypeVideoCamera
    }));
    const microphoneList = await ((_b = deviceManager.instance) == null ? void 0 : _b.getDevicesList({
      type: TUIMediaDeviceType.kMediaDeviceTypeAudioInput
    }));
    const speakerList = await ((_c = deviceManager.instance) == null ? void 0 : _c.getDevicesList({
      type: TUIMediaDeviceType.kMediaDeviceTypeAudioOutput
    }));
    if (!isWeChat) {
      const hasCameraDevice = cameraList && cameraList.length > 0;
      const hasMicrophoneDevice = microphoneList && microphoneList.length > 0;
      let alertMessage = "";
      if (hasCameraDevice && !hasMicrophoneDevice) {
        alertMessage = "Microphone not detected on current device";
      } else if (!hasCameraDevice && hasMicrophoneDevice) {
        alertMessage = "Camera not detected on current device";
      } else if (!hasCameraDevice && !hasMicrophoneDevice) {
        alertMessage = "Camera And Microphone not detected on current device";
      }
      if (alertMessage) {
        MessageBox({
          title: t("Note"),
          message: t(alertMessage),
          confirmButtonText: t("Sure")
        });
      }
    }
    cameraList && roomStore.setCameraList(cameraList);
    microphoneList && roomStore.setMicrophoneList(microphoneList);
    speakerList && roomStore.setSpeakerList(speakerList);
    const cameraInfo = (_d = deviceManager.instance) == null ? void 0 : _d.getCurrentDevice({
      type: TUIMediaDeviceType.kMediaDeviceTypeVideoCamera
    });
    const micInfo = (_e = deviceManager.instance) == null ? void 0 : _e.getCurrentDevice({
      type: TUIMediaDeviceType.kMediaDeviceTypeAudioInput
    });
    const speakerInfo = (_f = deviceManager.instance) == null ? void 0 : _f.getCurrentDevice({
      type: TUIMediaDeviceType.kMediaDeviceTypeAudioOutput
    });
    if (cameraInfo && cameraInfo.deviceId) {
      roomStore.setCurrentCameraId(cameraInfo.deviceId);
    }
    if (micInfo && micInfo.deviceId) {
      roomStore.setCurrentMicrophoneId(micInfo.deviceId);
    }
    if (speakerInfo && speakerInfo.deviceId) {
      roomStore.setCurrentSpeakerId(speakerInfo.deviceId);
    }
  }
  async function onDeviceChanged(eventInfo) {
    var _a, _b;
    if (!deviceManager.instance) {
      return;
    }
    const stateList = ["add", "remove", "active"];
    const deviceTypeList = ["mic", "speaker", "camera"];
    const { deviceId, type, state } = eventInfo;
    logger.log(
      `onDeviceChanged: deviceId: ${deviceId}, type: ${deviceTypeList[type]}, state: ${stateList[state]}`
    );
    const deviceList = await ((_a = deviceManager.instance) == null ? void 0 : _a.getDevicesList({ type }));
    deviceList && roomStore.setDeviceList(type, deviceList);
    if (state === TUIMediaDeviceState.kMediaDeviceStateActive) {
      roomStore.setCurrentDeviceId(type, deviceId);
    }
    if (isElectron && state === TUIMediaDeviceState.kMediaDeviceStateRemove && type === TUIMediaDeviceType.kMediaDeviceTypeVideoCamera) {
      const currentCameraInfo = (_b = deviceManager.instance) == null ? void 0 : _b.getCurrentDevice({
        type: TUIMediaDeviceType.kMediaDeviceTypeVideoCamera
      });
      if (currentCameraInfo && currentCameraInfo.deviceId) {
        roomStore.setCurrentCameraId(currentCameraInfo.deviceId);
      }
    }
  }
  TUIRoomEngine__default.once("ready", () => {
    var _a;
    if (options && options.listenForDeviceChange) {
      initMediaDeviceList();
      (_a = deviceManager.instance) == null ? void 0 : _a.on(
        TUIRoomDeviceMangerEvents.onDeviceChanged,
        onDeviceChanged
      );
    }
  });
  onUnmounted(() => {
    var _a;
    if (options && options.listenForDeviceChange) {
      (_a = deviceManager.instance) == null ? void 0 : _a.off(
        TUIRoomDeviceMangerEvents.onDeviceChanged,
        onDeviceChanged
      );
    }
  });
  return {
    deviceManager,
    initMediaDeviceList
  };
}
export {
  useDeviceManager as default
};
