"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const room = require("../../../stores/room.js");
const index = require("../../../locales/index.js");
const basic = require("../../../stores/basic.js");
const pinia = require("pinia");
const utils = require("../../../utils/utils.js");
const render = require("../../../constants/render.js");
function useStreamContainer() {
  const basicStore = basic.useBasicStore();
  const roomStore = room.useRoomStore();
  const { layout } = pinia.storeToRefs(basicStore);
  const { localStream } = pinia.storeToRefs(roomStore);
  const { t } = index.useI18n();
  const currentRemoteSpeakerUserId = Vue.ref("");
  const currentSpeakerUserId = Vue.ref("");
  function handleUserVoiceVolume(userVolumeList) {
    const localUserVolume = {
      userId: basicStore.userId,
      volume: 0
    };
    const largestRemoteUserVolume = {
      userId: "",
      volume: 0
    };
    userVolumeList.forEach((item) => {
      var _a;
      if (item.userId === basicStore.userId && localStream.value.hasAudioStream) {
        localUserVolume.volume = item.volume;
      } else if (item.userId !== basicStore.userId && ((_a = roomStore.userInfoObj[item.userId]) == null ? void 0 : _a.hasAudioStream)) {
        const { userId, volume } = item;
        if (volume > largestRemoteUserVolume.volume) {
          largestRemoteUserVolume.userId = userId;
          largestRemoteUserVolume.volume = volume;
        }
      }
    });
    const largestUserVolume = localUserVolume.volume > largestRemoteUserVolume.volume ? localUserVolume : largestRemoteUserVolume;
    if (currentRemoteSpeakerUserId.value) {
      const lastRemoteSpeakerUserVolumeInfo = userVolumeList.find(
        (item) => item.userId === currentRemoteSpeakerUserId.value
      );
      if (!lastRemoteSpeakerUserVolumeInfo || lastRemoteSpeakerUserVolumeInfo.volume === 0) {
        if (largestRemoteUserVolume.volume > 0) {
          currentRemoteSpeakerUserId.value = largestRemoteUserVolume.userId;
        }
      }
    } else {
      if (largestRemoteUserVolume.volume > 0) {
        currentRemoteSpeakerUserId.value = largestRemoteUserVolume.userId;
      }
    }
    if (currentSpeakerUserId.value) {
      const lastSpeakerUserVolumeInfo = userVolumeList.find(
        (item) => item.userId === currentSpeakerUserId.value
      );
      if (!lastSpeakerUserVolumeInfo || lastSpeakerUserVolumeInfo.volume === 0) {
        if (largestUserVolume.volume > 0) {
          currentSpeakerUserId.value = largestUserVolume.userId;
        }
      }
    } else {
      if (largestUserVolume.volume > 0) {
        currentSpeakerUserId.value = largestUserVolume.userId;
      }
    }
  }
  const handleUserVoiceVolumeThrottle = utils.throttle(handleUserVoiceVolume, 1e3);
  const onUserVoiceVolumeChanged = (eventInfo) => {
    const { userVolumeList } = eventInfo;
    if (layout.value === render.LAYOUT.LARGE_SMALL_WINDOW) {
      handleUserVoiceVolumeThrottle(userVolumeList);
    }
  };
  const isSameStream = (stream1, stream2) => `${stream1 == null ? void 0 : stream1.userId}_${stream1 == null ? void 0 : stream1.streamType}` === `${stream2 == null ? void 0 : stream2.userId}_${stream2 == null ? void 0 : stream2.streamType}`;
  const getStreamKey = (stream) => `${stream == null ? void 0 : stream.userId}_${stream == null ? void 0 : stream.streamType}`;
  return {
    currentRemoteSpeakerUserId,
    currentSpeakerUserId,
    onUserVoiceVolumeChanged,
    isSameStream,
    getStreamKey,
    t
  };
}
exports.default = useStreamContainer;
