import { getVideoAuth, getVideoSource } from "@/request/api.js";
import { ElMessage } from "element-plus";
import { ref } from "vue";

export default async function useAliYunVideoPreview(data) {
  const src = ref("");
  const vid = ref("");
  const playAuth = ref("");
  const visible = ref(false);
  src.value = "";
  vid.value = "";
  playAuth.value = "";
  if (data.VIDEOCOURSEWARE_ID_REMOTE) {
    const resData = await getVideoSource({
      VIDEOCOURSEWARE_ID: data.VIDEOCOURSEWARE_ID_REMOTE,
      CURRICULUM_ID: data.CURRICULUM_ID_REMOTE,
    });
    const video = {};
    if (!resData.responseBody) {
      ElMessage.error("该课件暂无视频资源");
      return;
    }
    resData.responseBody?.videoList?.forEach((item) => {
      video[item.definition] = item.playURL;
    });
    src.value = JSON.stringify(video);
    visible.value = true;
  } else {
    const resData = await getVideoAuth({
      videoId: data.VIDEOFILES,
    });
    playAuth.value = resData.playAuth;
    vid.value = data.VIDEOFILES;
    visible.value = true;
  }
  return {
    src,
    vid,
    playAuth,
    visible,
  };
}
