// import { saveBlobToDisk } from "@/utils/fileUtils";
import { $httpWithMsg } from "@/plugins/axios";
import { audioToImageNode } from "../utils";
import { QUESTION_API } from "@/constants/constants";

function saveAudioApi(data) {
  console.log(data);
  let formData = new FormData();
  Object.keys(data).forEach((k) => {
    formData.append(k, data[k]);
  });
  const url = `${QUESTION_API}/question/audio/upload`;
  return $httpWithMsg.post(url, formData);
}

/**
 * 对粘贴事件进行处理：
 * 1. text类型直接粘贴
 * 2. 文件类型，判断图片
 *
 * @this {Object} VEditor 因为VEditor会带很多选项，所以绑定this传过来
 * @param {Event} event
 */
export async function audioHandle(event) {
  this.$refs.editor.focus();
  /** @type {File} */
  const file = event.target.files[0];
  event.target.value = "";
  if (file.size > this.maxAudioSize) {
    this.$message(
      `音频大小超过限制！最大不超过 ${this.maxAudioSize / 1024 / 1024} MB.`
    );
    return;
  }

  // 上传音频文件
  const res = await saveAudioApi({ file });
  console.log(res.data);

  const imageNode = await audioToImageNode(res.data);
  console.log(imageNode);
  // console.log({ relativeFilePath, imageNode });
  // console.log(imageNode.outerHTML);
  document.execCommand("insertHTML", false, imageNode.outerHTML);
  this.$emit("audio-added");
}
