"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const TUIRoomEngine = require("@tencentcloud/tuiroom-engine-js");
const room = require("../../../../stores/room.js");
const roomService = require("../../../../services/roomService.js");
const basic = require("../../../../stores/basic.js");
const pinia = require("pinia");
const utils = require("../../../../utils/utils.js");
const _hoisted_1 = ["id"];
const _sfc_main = /* @__PURE__ */ Vue.defineComponent({
  __name: "index",
  props: {
    streamInfo: {},
    isEnlarge: { type: Boolean },
    isNeedPlayStream: { type: Boolean, default: true },
    observerViewInVisible: { type: Boolean }
  },
  setup(__props) {
    const mediaManager = roomService.roomService.getMediaManager();
    const basicStore = basic.useBasicStore();
    const roomStore = room.useRoomStore();
    const { defaultStreamType, streamInfoObj } = pinia.storeToRefs(roomStore);
    const props = __props;
    const playRegionDomRef = Vue.ref();
    const nanoId = utils.getNanoId(5);
    const playRegionDomId = Vue.computed(
      () => `${props.streamInfo.userId}_${props.streamInfo.streamType}_${nanoId}`
    );
    const streamTypeToFetch = Vue.computed(() => {
      const { streamType, userId } = props.streamInfo;
      const { userId: localUserId } = basicStore;
      if (streamType === TUIRoomEngine.TUIVideoStreamType.kScreenStream) {
        return TUIRoomEngine.TUIVideoStreamType.kScreenStream;
      }
      if (props.isEnlarge || userId === localUserId) {
        return TUIRoomEngine.TUIVideoStreamType.kCameraStream;
      }
      return defaultStreamType.value;
    });
    async function startPlayVideo() {
      if (!streamInfoObj.value[`${props.streamInfo.userId}_${props.streamInfo.streamType}`] || !playRegionDomRef.value) {
        return;
      }
      await Vue.nextTick();
      if (props.isNeedPlayStream && props.streamInfo.hasVideoStream) {
        await mediaManager.startPlayVideo({
          userId: props.streamInfo.userId,
          streamType: streamTypeToFetch.value,
          view: playRegionDomRef.value,
          observerViewInVisible: props.observerViewInVisible
        });
      }
    }
    async function stopPlayVideo() {
      await mediaManager.stopPlayVideo({
        userId: props.streamInfo.userId,
        streamType: streamTypeToFetch.value,
        view: playRegionDomRef.value
      });
    }
    Vue.watch(
      () => [
        props.streamInfo.userId,
        props.streamInfo.streamType,
        props.streamInfo.hasVideoStream,
        props.isNeedPlayStream
      ],
      async (val, oldVal) => {
        const [oldUserId, oldStreamType] = oldVal;
        if (oldUserId !== props.streamInfo.userId || oldStreamType !== props.streamInfo.streamType) {
          mediaManager.stopPlayVideo({
            userId: oldUserId,
            streamType: oldStreamType,
            view: playRegionDomRef.value
          });
        }
        if (props.isNeedPlayStream && props.streamInfo.hasVideoStream) {
          await startPlayVideo();
        } else {
          await stopPlayVideo();
        }
      }
    );
    Vue.watch(
      () => streamTypeToFetch,
      async () => {
        if (props.isNeedPlayStream && props.streamInfo.hasVideoStream) {
          await startPlayVideo();
        }
      }
    );
    Vue.onMounted(async () => {
      if (props.isNeedPlayStream && props.streamInfo.hasVideoStream) {
        await startPlayVideo();
      }
    });
    Vue.onBeforeUnmount(async () => {
      if (props.isNeedPlayStream && props.streamInfo.hasVideoStream) {
        await stopPlayVideo();
      }
    });
    return (_ctx, _cache) => {
      return Vue.openBlock(), Vue.createElementBlock("div", {
        ref_key: "playRegionDomRef",
        ref: playRegionDomRef,
        id: playRegionDomId.value,
        class: "stream-play-container"
      }, null, 8, _hoisted_1);
    };
  }
});
exports.default = _sfc_main;
